2

我有一个情况,我花了很长时间来弄清楚请帮助我

这是桌子

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  --     --   --   --

如果有人可以提供除此之外的好的解决方案,那将非常有帮助

谢谢

我收到此错误

消息 4109,级别 15,状态 1,行 1
窗口函数不能在另一个窗口函数或聚合的上下文中使用。

4

1 回答 1

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

您可以在 SQLFiddle 上查看此演示

于 2013-11-13T00:12:04.373 回答