我的查询为:
select
location_id,
max(created) over (partition by location_id) as "created"
from
device
group by
location_id, created
order by
location_id
这导致每个grp重复最大日期。
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-26T00:00:00.000Z 0
22 2015-01-26T00:00:00.000Z 0
22 2015-03-12T00:00:00.000Z 45
22 2015-10-18T00:00:00.000Z 265
为什么我在第一张唱片和第二张唱片之间以及第三张唱片之间有所区别为什么不只是通过组重复max-min之间的差异?
希望我对这个问题很清楚。
答案 0 :(得分:2)
您的第二个查询解析如下:
select . . .
(max(date(created)) -
min(date(created)) over (partition by location_id)
) as "created"
. . .
也就是说,窗口子句仅适用于min()
。我想你打算:
select . . .
(max(date(created) over (partition by location_id) -
min(date(created)) over (partition by location_id)
) as "created"
. . .
答案 1 :(得分:0)
在您的查询中,您按位置进行分区 - 因此基本上将min和max视为各个位置的上下文...这可以解释为什么每行有最小和最大分别。
答案 2 :(得分:0)
我认为这是因为您在第二次查询时没有使用Max的窗口函数。
尝试:
max(date(created)) over (partition by location_id) - min(date(created)) over (partition by location_id) as "created"
如果我猜错了,我认为如果你能提供样本数据和示例结果会有帮助