我有一个情况,我花了很长时间才弄清楚请帮帮我
这是表格
5555 Alex 10:47 2013-11-09
6666 Joel 00:09 2013-11-10
6666 Joel 22:10 2013-11-11
5555 Alex 01:00 2013-11-11
像这样,它将有一周的日期我需要创建一个连续7天的视图 假设
day1 date is 2013-11-09
day2 date is 2013-11-10
day3 date is 2013-11-11
我需要这个
Emplid Name Day1 Day2 Day3 Day4 Day5 Day6 Day7
555 Alex 10.47 -- 01.00 -- -- -- --
666 Joel -- 00.09 22.10 -- -- -- --
如果有人能提供除此之外的非常有用的解决方案
谢谢
我收到此错误
Msg 4109,Level 15,State 1,Line 1
窗口函数不能在另一个窗口函数或聚合的上下文中使用。
答案 0 :(得分:1)
你需要这样的东西:
SELECT Emplid,
Name,
max(day1) day1,
max(day2) day2,
max(day3) day3,
max(day4) day4,
max(day5) day5,
max(day6) day6,
max(day7) day7
FROM (
SELECT Emplid,
Name,
CASE WHEN rank() over(partition by Emplid, Name order by date) = 1 THEN time ELSE NULL END Day1,
CASE WHEN rank() over(partition by Emplid, Name order by date) = 2 THEN time ELSE NULL END Day2,
CASE WHEN rank() over(partition by Emplid, Name order by date) = 3 THEN time ELSE NULL END Day3,
CASE WHEN rank() over(partition by Emplid, Name order by date) = 4 THEN time ELSE NULL END Day4,
CASE WHEN rank() over(partition by Emplid, Name order by date) = 5 THEN time ELSE NULL END Day5,
CASE WHEN rank() over(partition by Emplid, Name order by date) = 6 THEN time ELSE NULL END Day6,
CASE WHEN rank() over(partition by Emplid, Name order by date) = 7 THEN time ELSE NULL END Day7
FROM your_table
) t
GROUP BY Emplid,
Name
您可以查看demo on SQLFiddle