如何为此示例在SQL SERVER中添加空行?

时间:2018-01-25 16:03:43

标签: sql-server sql-server-2012 days

Sql Fiddle Example

我有这个表结构:

    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

我想在表中填写未出现在寄存器中的日子

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

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

我怎么能得到这个?,谢谢

4 个答案:

答案 0 :(得分:3)

我会创建一个empy基表并执行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

答案 1 :(得分:3)

如果您不想创建新表

,可以使用此选项
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

答案 2 :(得分:1)

您可以使用公用表expressin生成星期日历“表格”:

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

答案 3 :(得分:1)

或者,创建几天的参考表,然后右键加入您的选择查询。

thead

结果:

            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