对于此示例,如何在 SQL SERVER 中添加空行?

How can I add empty rows in SQL SERVER for this example?

Sql Fiddle Example

我有这个 table 结构:

    CREATE TABLE Schedule
    ([Day] varchar(13), [Starts] varchar(57), [Ends] varchar(57))
;

INSERT INTO Schedule
    ([Day], [Starts], [Ends])
VALUES
    ('2', '09:00', '15:00'),
   ('5', '10:00', '12:00'),
  ('3', '09:00', '18:00')
;

这个简单的查询显示当前行:

   SELECT
 DATENAME(weekday,  Day - 1) as days,
  Starts,
  Ends  
FROM
  Schedule
  order by Day

最后一次查询得到这个结果:

Day        Starts    Ends
---        -------   ----
Tuesday    09:00     15:00
Wednesday  09:00     18:00
Friday     10:00     12:00

我想在 table 中填写未出现在寄存器中的日期

这是我想要得到的最终结果:

    Day        Starts    Ends
    ---        -------   ----
    Monday     NULL      NULL
    Tuesday    09:00     15:00
    Wednesday  09:00     18:00
    Thursday   NULL      NULL
    Friday     10:00     12:00

我怎样才能得到这个?,谢谢

我会创建一个空基 table 并进行 LEFT JOIN:

CREATE TABLE Schedule
(Day VARCHAR(13), Starts VARCHAR(57), Ends   VARCHAR(57));

INSERT INTO Schedule (Day, Starts,Ends)

VALUES
('2', '09:00','15:00'),
('5', '10:00','12:00'),
('3', '09:00','18:00');


CREATE TABLE ScheduleBase
(Day    VARCHAR(13), Starts VARCHAR(57), Ends VARCHAR(57));


INSERT INTO ScheduleBase
(Day, Starts, Ends)
VALUES
('1', NULL, NULL),
('2', NULL, NULL),
('3', NULL, NULL),
('4', NULL, NULL),
('5', NULL, NULL)

SELECT SB.Day, S.Starts, S.Ends FROM ScheduleBase AS SB LEFT JOIN Schedule 
AS S ON SB.Day = S.Day

如果你不想创建新的,你可以使用这个table

select
    DATENAME(weekday,  Day - 1) as days, max(Starts), max(Ends)
from (
    SELECT
        Day, Starts, Ends  
    FROM
        Schedule
    union all 
    select 
        *, null, null 
    from (values (1),(2),(3),(4),(5),(6),(7)) t(d)
) t
group by Day

您可以使用常见的 table 表达式即时生成周历 "table":

with range (num) as (
  select 1 union all select 2 union all select 3
  union all select 4 union all select 5 union all select 6
  union all select 7
 )
SELECT
 DATENAME(weekday,  range.num - 1),
  Starts,
  Ends  
FROM
  Schedule
  right outer join range on range.num = Schedule.day
  order by range.num

Fiddle

或者,创建参考 table 天,然后右连接到您的 select 查询。

            SELECT  DATENAME(WEEKDAY, TBL.days - 1) AS days ,
                    starts ,
                    ends
            FROM    ( SELECT    Day days ,
                                Starts ,
                                Ends
                      FROM      Schedule
                    ) T
                    RIGHT JOIN ( SELECT [days]
                                 FROM   ( SELECT    1 [days]
                                          UNION 
                                          SELECT    2
                                          UNION 
                                          SELECT    3
                                          UNION 
                                          SELECT    4
                                          UNION 
                                          SELECT    5
                                          UNION 
                                          SELECT    6
                                          UNION 
                                          SELECT    7
                                        ) S
                               ) TBL ON TBL.days = t.days

结果:

        days         starts    ends
        ------------ --------- ------------
        Monday       NULL      NULL
        Tuesday      09:00     15:00
        Wednesday    09:00     18:00
        Thursday     NULL      NULL
        Friday       10:00     12:00
        Saturday     NULL      NULL
        Sunday       NULL      NULL

        (7 row(s) affected)