[英]How to group consecutive rows together in SQL by multiple columns
我在查詢中有返回以下內容的行:
Date User Time Location Service Count
1/1/2018 Nick 12:00 Location A X 1
1/1/2018 Nick 12:01 Location A Y 1
1/1/2018 John 12:02 Location B Z 1
1/1/2018 Harry 12:03 Location A X 1
1/1/2018 Harry 12:04 Location A X 1
1/1/2018 Harry 12:05 Location B Y 1
1/1/2018 Harry 12:06 Location B X 1
1/1/2018 Nick 12:07 Location A X 1
1/1/2018 Nick 12:08 Location A Y 1
查詢返回用戶訪問過的位置以及該位置進行的選擇計數。 結果按用戶和時間遞增排序。 我需要將其分組到用戶和位置相同的CONSECUTIVE行與Count列的SUM和Service Column中以逗號分隔的唯一值列表分組的地方,最終結果返回如下內容:
Date User Start Time End Time Location Service Count
1/1/2018 Nick 12:00 12:01 Location A X,Y 2
1/1/2018 John 12:02 12:02 Location B Z 1
1/1/2018 Harry 12:03 12:04 Location A X 2
1/1/2018 Harry 12:05 12:06 Location B X,Y 2
1/1/2018 Nick 12:07 12:08 Location A X,Y 2
我不確定從哪里開始。 也許滯后或分區條款? 希望SQL專家可以在這里提供幫助...
這是一個懸而未決的問題。 解決它的一種方法是使用row_number()
:
select Date, User, min(Time) as start_time, max(time) as end_time,
Location,
listagg(Service, ',') within group (order by service),
count(*) as cnt
from (select t.*,
row_number() over (date order by time) as seqnum,
row_number() over (partition by user, date, location order by time) as seqnum_2
from t
) t
group by Date, User, Location, (seqnum - seqnum_2);
解釋它是如何工作的有點棘手。 我的建議是運行子查詢,您將看到行號的差異如何定義您要查找的組。
使用lag
獲取上一行的用戶和位置值。 然后,只要用戶和位置發生變化,就使用一個總和生成一個新的組。 最后匯總分類的組,用戶,位置和日期。
select Date, User, min(Time) as start_time,max(time) as end_time, Location,
listagg(Service, ',') within group (order by Service),
count(*) as cnt
from (select Date, User, Time, Location,
sum(case when prev_location=location and prev_user=user then 0 else 1 end) over(order by date,time) as grp
from (select Date, User, Time, Location,
lag(Location) over(order by date,time) as prev_location,
lag(User) over(order by date,time) as prev_user,
from t
) t
) t
group by Date, User, Location, grp;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.