select hour, sum(count(*)) over (partition by day order by hour)
from (select day, id, min(hour) as hour
from t
group by day, id
) t
group by hour
order by hour;
注意:这假设您真的希望在给定的一天内有一个小时。 也可以表示为:
select day, hour, sum(cnt) over (partition by day order by hour)
from (select day, hour, count(*) as cnt
from (select day, id, min(hour) as hour
from t
group by day, id
) t
group by hour
) h
order by hour;
以上不包括一小时,除非该小时内有新的id。对于所有时间,您都可以改用窗口功能:
select hour,
sum(sum( (seqnum = 1)::int ) over (partition by day order by hour)
from (select day, id,
row_number() over (partition by day, id order by hour) as seqnum
from t
) t
group by hour
order by hour;
也可以表示为:
select day, hour, sum(cnt) over (partition by day order by hour)
from (select day, hour, sum( (seqnum = 1)::int ) as cnt
from (select day, id,
row_number() over (partition by day, id order by hour) as seqnum
from t
) t
group by hour, day
) dh
order by hour;
2条答案
按热度按时间sz81bmfz1#
对于每个
id
,记录id第一次出现的时间,然后进行累加:注意:这假设您真的希望在给定的一天内有一个小时。
也可以表示为:
以上不包括一小时,除非该小时内有新的id。对于所有时间,您都可以改用窗口功能:
也可以表示为:
mrzz3bfm2#
我每小时有不同的计数。
输出