以下是我的数据。如何根据ID和会议室汇总数据,但如果会议室重复,则不应汇总到首次出现?
应该像下面的结果集一样进行汇总。
数据集:
ID Room Length AD
-----------------------------------
1001 MM 2 2018-06-26
1001 MM 3 2018-06-26
1001 MM 0.5 2018-06-26
1001 MM 3 2018-06-28
1001 A5 3.5 2018-06-29
1001 MM 4.4 2018-06-29
1001 MM 3.2 2018-06-30
1001 A5 2.1 2018-07-02
1001 A5 1.7 2018-07-03
预期结果:
ID Room Sum_Length
-----------------------
1001 MM 8.5
1001 A5 3.5
1001 MM 7.6
1001 A5 3.8
答案 0 :(得分:2)
似乎您需要row_number()
的区别:
select id, room, sum(Length)
from (select t.*, row_number() over (partition by id order by ad) seq1,
row_number() over (partition by id, room order by ad) seq2
from table t
) t
group by id, room, (seq1 - seq2);