Добро пожаловать в форум, Guest  >>   Войти | Регистрация | Поиск | Правила | В избранное | Подписаться
Все форумы / Microsoft SQL Server Новый топик    Ответить
 Непредсказуемое поведение MAX OVER  [new]
serega063
Member

Откуда:
Сообщений: 176
запрос!
Хочу чтоб группировка MAX использовалась по максимальной дате!
в результате появляется дубль

select *, max(cmd_id) OVER(partition by dt  order by bdt desc) max_id from(

select cast('27.06.2018 13:13:00'AS DATETIME) as dt, 1 as n, 23045 as cmd_id, cast('26.06.2018 10:20:00' AS DATETIME) as bdt, cast('26.06.2018 11:00:00'AS DATETIME) as edt union all
select cast('27.06.2018 13:13:00'AS DATETIME) as dt, 1 as n, 23046 as cmd_id, cast('27.06.2018 12:00:00' AS DATETIME) as bdt, cast('27.06.2018 13:00:00'AS DATETIME) as edt union all
select cast('27.06.2018 13:13:00'AS DATETIME) as dt, 1 as n, 23059 as cmd_id, cast('27.06.2018 12:48:00' AS DATETIME) as bdt, cast('27.06.2018 13:00:00'AS DATETIME) as edt union all
select cast('27.06.2018 13:14:00'AS DATETIME) as dt, 2 as n, 23045 as cmd_id, cast('26.06.2018 10:20:00' AS DATETIME) as bdt, cast('26.06.2018 11:00:00'AS DATETIME) as edt union all
select cast('27.06.2018 13:14:00'AS DATETIME) as dt, 2 as n,23046 as cmd_id, cast('27.06.2018 12:00:00' AS DATETIME) as bdt, cast('27.06.2018 13:00:00'AS DATETIME) as edt union all
select cast('27.06.2018 13:14:00'AS DATETIME) as dt, 2 as n,23059 as cmd_id, cast('27.06.2018 12:48:00' AS DATETIME) as bdt, cast('27.06.2018 13:00:00'AS DATETIME) as edt union all
select cast('27.06.2018 13:14:00'AS DATETIME) as dt, 2 as n,23056 as cmd_id, cast('27.06.2018 13:13:00' AS DATETIME) as bdt, cast('27.06.2018 13:30:00'AS DATETIME) as edt
)s1

dt	n	cmd_id	bdt	edt	max_id
2018-06-27 13:13:00.000 1 23059 2018-06-27 12:48:00.000 2018-06-27 13:00:00.000 23059
2018-06-27 13:13:00.000 1 23046 2018-06-27 12:00:00.000 2018-06-27 13:00:00.000 23059
2018-06-27 13:13:00.000 1 23045 2018-06-26 10:20:00.000 2018-06-26 11:00:00.000 23059
2018-06-27 13:14:00.000 2 23056 2018-06-27 13:13:00.000 2018-06-27 13:30:00.000 23056 *****ОТКУДА
2018-06-27 13:14:00.000 2 23059 2018-06-27 12:48:00.000 2018-06-27 13:00:00.000 23059
2018-06-27 13:14:00.000 2 23046 2018-06-27 12:00:00.000 2018-06-27 13:00:00.000 23059
2018-06-27 13:14:00.000 2 23045 2018-06-26 10:20:00.000 2018-06-26 11:00:00.000 23059



Что я делаю не так? заранее благодарен
29 июн 18, 12:28    [21531002]     Ответить | Цитировать Сообщить модератору
 Re: Непредсказуемое поведение MAX OVER  [new]
msLex
Member

Откуда:
Сообщений: 7730
serega063,

а order by bdt desc вы с какой целью написали?
эти вы "превратили" свой over в
OVER(partition by dt order by bdt desc RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW )
29 июн 18, 12:59    [21531115]     Ответить | Цитировать Сообщить модератору
 Re: Непредсказуемое поведение MAX OVER  [new]
Kopelly
Member

Откуда: Красноярск
Сообщений: 289
Select *, max(case When dt = max_dt Then cmd_id End) over () as Max_ID From (
select *, max(dt) OVER() max_dt from(

select cast('27.06.2018 13:13:00'AS DATETIME) as dt, 1 as n, 23045 as cmd_id, cast('26.06.2018 10:20:00' AS DATETIME) as bdt, cast('26.06.2018 11:00:00'AS DATETIME) as edt union all
select cast('27.06.2018 13:13:00'AS DATETIME) as dt, 1 as n, 23046 as cmd_id, cast('27.06.2018 12:00:00' AS DATETIME) as bdt, cast('27.06.2018 13:00:00'AS DATETIME) as edt union all
select cast('27.06.2018 13:13:00'AS DATETIME) as dt, 1 as n, 23059 as cmd_id, cast('27.06.2018 12:48:00' AS DATETIME) as bdt, cast('27.06.2018 13:00:00'AS DATETIME) as edt union all
select cast('27.06.2018 13:14:00'AS DATETIME) as dt, 2 as n, 23045 as cmd_id, cast('26.06.2018 10:20:00' AS DATETIME) as bdt, cast('26.06.2018 11:00:00'AS DATETIME) as edt union all
select cast('27.06.2018 13:14:00'AS DATETIME) as dt, 2 as n,23046 as cmd_id, cast('27.06.2018 12:00:00' AS DATETIME) as bdt, cast('27.06.2018 13:00:00'AS DATETIME) as edt union all
select cast('27.06.2018 13:14:00'AS DATETIME) as dt, 2 as n,23059 as cmd_id, cast('27.06.2018 12:48:00' AS DATETIME) as bdt, cast('27.06.2018 13:00:00'AS DATETIME) as edt union all
select cast('27.06.2018 13:14:00'AS DATETIME) as dt, 2 as n,23056 as cmd_id, cast('27.06.2018 13:13:00' AS DATETIME) as bdt, cast('27.06.2018 13:30:00'AS DATETIME) as edt
)s1 ) s2
29 июн 18, 13:14    [21531169]     Ответить | Цитировать Сообщить модератору
 Re: Непредсказуемое поведение MAX OVER  [new]
TaPaK
Member

Откуда: Kiev
Сообщений: 6794
msLex
serega063,

а order by bdt desc вы с какой целью написали?
эти вы "превратили" свой over в
OVER(partition by dt order by bdt desc RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW )

фантастика
2 июл 18, 10:21    [21536025]     Ответить | Цитировать Сообщить модератору
 Re: Непредсказуемое поведение MAX OVER  [new]
TaPaK
Member

Откуда: Kiev
Сообщений: 6794
на всякий случай
автор
If ORDER BY is not specified entire partition is used for a window frame. This applies only to functions that do not require ORDER BY clause. If ROWS/RANGE is not specified but ORDER BY is specified, RANGE UNBOUNDED PRECEDING AND CURRENT ROW is used as default for window frame. This applies only to functions that have can accept optional ROWS/RANGE specification. For example, ranking functions cannot accept ROWS/RANGE, therefore this window frame is not applied even though ORDER BY is present and ROWS/RANGE is not.
2 июл 18, 10:43    [21536105]     Ответить | Цитировать Сообщить модератору
Все форумы / Microsoft SQL Server Ответить