我有一個查詢爲:被反覆對每一個GRPSQL窗口函數:MAX(日)在PARTITION BY子句
select
location_id,
max(created) over (partition by location_id) as "created"
from
device
group by
location_id, created
order by
location_id
這將導致最大的日期。
21 2015-01-26T18:25:00.000Z
22 2015-10-18T13:21:32.000Z
22 2015-10-18T13:21:32.000Z
22 2015-10- 18T13:21:32.000Z
現在,如果我計算日期之間的差值:
select
location_id,
date(created),
max(date(created)) - min(date(created)) over (partition by location_id) as "created"
from
device
group by
location_id, created
order by
location_id
21 2015-01-26T0 0:00:0 00.000Z
22 2015-01-26T00:00:0 00.000Z
22 2015-03-12T00:00:45 00.000Z
22 2015-10-18T00:00:00.000Z 265
爲什麼我會得到第一個記錄和第二個,然後第三個之間的區別?爲什麼不只是通過組重複最大 - 最小的差異?
希望我對這個問題很清楚。
哪個RDBMS是爲了這個?請添加一個標籤來指定您是使用'mysql','postgresql','sql-server','oracle'還是'db2' - 或者其他的東西。 –