这是我的表:
Day CheckIn CheckOut Employee Id
------------------------------------
Sun 09:00 10:00 1
Sun 11:00 13:00 1
Mon 08:00 11:00 1
Mon 14:00 16:00 1
我正在寻找以下输出:
SunCheckIn SunCheckOut MonCheckIn MonCheckOut
-----------------------------------------------------
09:00 10:00 08:00 11:00
11:00 13:00 14:00 16:00
我尝试使用连接但没有任何帮助。有人可以帮我这个吗?
答案 0 :(得分:1)
您想要的输出不适合SQL。您可以生成它,但您在列中基本上有两个列表。换句话说,这些行并不意味着什么。
您可以使用Unknown provider: groupByFilterProvider <- groupByFilter
枚举行然后使用条件聚合来获取它:
row_number()
答案 1 :(得分:1)
你可以写成:
;with CTE as
(select Day,CheckIn,CheckOut, EmployeeId,
Row_Number() over ( partition by Day,EmployeeId order by CheckIn,CheckOut) as rownum
from test
)
select T.EmployeeId,
T.CheckIn as 'SunCheckIn' ,
T.CheckOut as 'SunCheckOut' ,
T1.CheckIn as 'MonCheckIn',
T1.CheckOut as 'MonCheckOut'
from CTE as T
Left join CTE as T1 on T.EmployeeId = T1.EmployeeId
and T.rownum = T1.rownum and T.Day <> T1.Day and T1.Day = 'Mon'
where T.Day = 'Sun'