簡體   English   中英

LINQ to SQL 按一行中的多條記錄分組

[英]LINQ to SQL Group By Multiple Records in One Row

對不起,我不知道如何用語言來形容,這就是為什么標題不好。

我有一個表如下:

UserID Date Description
1 17/11/2020 09:00 Sign in
2 17/11/2020 09:00 Sign in
2 17/11/2020 12:00 Sign out
2 17/11/2020 13:00 Sign in
2 17/11/2020 16:00 Sign out
1 17/11/2020 17:00 Sign out

我正在根據這些數據創建報告。 我需要按如下方式顯示數據:

Date UserID SignIn SignOut
1 17/11/2020 09:00 17:00
2 17/11/2020 09:00 12:00
2 17/11/2020 13:00 16:00

我知道我需要按日期和用戶分組,但我需要幫助獲取同一行中的登錄/退出時間。

一種方法是使用row_number()條件聚合:

select userid,
       max(case when description = 'Sign in' then date end) as signin,
       max(case when description = 'Sign out' then date end) as signout
from (select t.*, 
             row_number() over (partition by userid, description order by date) as seqnum
      from t
     ) t
group by userid, seqnum

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM