理货表在两个日期之间插入缺失的日期? SQL

Posted

技术标签:

【中文标题】理货表在两个日期之间插入缺失的日期? SQL【英文标题】:Tally Table to insert missing dates between two dates? SQL 【发布时间】:2016-07-18 04:59:45 【问题描述】:

我将以下代码从网上的研究和我自己的 SQL 知识(不是最好的)拼凑在一起。

Table_One 表包含员工及其工作日的数据,我想做的是 INSERT 在每个员工的两个指定日期之间缺少日期(非工作日)的行。

据我所知,我不知道如何检查该行是否丢失,如果是,则插入新的row 以及日期和相应的员工数据。

SET NOCOUNT ON;
IF object_id('dbo.Tally') is not null drop table dbo.tally
GO
SELECT TOP 10000 IDENTITY(int,1,1) as ID
   INTO dbo.Tally FROM master.dbo.SysColumns
   ALTER table dbo.Tally
   add constraint PK_ID primary key clustered(ID)
GO
select * from dbo.Tally

--Generate Date Range
DECLARE @StartDate  datetime
DECLARE @EndDate datetime
SET @StartDate = '2016/6/1' 
SET @EndDate= '2016/7/1'
SELECT dateadd(DD,ID-1,@StartDate) as [DATE]
   FROM dbo.Tally
   WHERE dateadd(DD,ID-1,@StartDate)<=@EndDate

表格是这样的,

Staff_ID  |  Date      | Year   | Mon | Day |  First_Name   |  Last_Name  | Section  | Time_Worked
1001      |  2016/6/1  |  2016  |  6  |  1  |  Bill         |  Price      | Level 1  | 2016/6/1 8:30:00.000
1001      |  2016/6/5  |  2016  |  6  |  1  |   Bill        |  Price      | Level 1  | 2016/6/5 8:30:00.000
1001      |  2016/6/9  |  2016  |  6  |  1  |   Bill        |  Price      | Level 1  | 2016/6/9 8:30:00.000
1001      |  2016/6/12 |  2016  |  6  |  1  |   Bill        |  Price      | Level 1  | 2016/6/12 8:30:00.000
1002      |  2016/6/1  |  2016  |  6  |  1  |   Mary        |  Somers     | Level 1  | 2016/6/1 8:30:00.000
1002      |  2016/6/5  |  2016  |  6  |  1  |   Mary        |  Somers     | Level 1  | 2016/6/5 8:30:00.000
1002      |  2016/6/8  |  2016  |  6  |  1  |   Mary        |  Somers     | Level 1  | 2016/6/8 8:30:00.000
1003      |  2016/6/3  |  2016  |  6  |  1  |   Mark        |  Jones      | Level 1  | 2016/6/3 8:30:00.000
1003      |  2016/6/5  |  2016  |  6  |  1  |   Mark        |  Jones      | Level 1  | 2016/6/5 8:30:00.000

位于工作人员两个日期之间的第一行数据将能够用于填充除日期列之外的列。而且每个员工的第一行数据不一定是同一天。

例如。 该员工第一天是查询中SET @StartDate = '2016/6/1'之后的两天,

Staff_ID  |  Date      |  First_Name  |  Last_Name  | Section  | Time_Worked
1003      |  2016/6/3  |  Mark        |  Jones      | Level 1  | 2016/6/3 8:30:00.000

但是,其他列将能够用于填充新行数据。

这是上表中一名工作人员的结果,在这种情况下,工作人员编号 1001 命名为 Bill

Staff_ID  |  Date       |  Year  |  Mon|  Day|  First_Name |  Last_Name  | Section  | Time_Worked
1001      |  2016/6/1   |  2016  |  6  |  1  | Bill        |  Price      | Level 1  | 2016/6/1 8:30:00.000
1001      |  2016/6/2   |  2016  |  6  |  2  | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/3   |  2016  |  6  |  3  | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/4   |  2016  |  6  |  4  | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/5   |  2016  |  6  |  5  | Bill        |  Price      | Level 1  | 2016/6/5 8:30:00.000
1001      |  2016/6/6   |  2016  |  6  |  6  | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/7   |  2016  |  6  |  7  | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/8   |  2016  |  6  |  8  | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/9   |  2016  |  6  |  9  | Bill        |  Price      | Level 1  | 2016/6/9 8:30:00.000
1001      |  2016/6/10  |  2016  |  6  |  10 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/11  |  2016  |  6  |  11 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/12  |  2016  |  6  |  12 | Bill        |  Price      | Level 1  | 2016/6/12 8:30:00.000
1001      |  2016/6/13  |  2016  |  6  |  13 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/14  |  2016  |  6  |  14 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/15  |  2016  |  6  |  15 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/16  |  2016  |  6  |  16 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/17  |  2016  |  6  |  17 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/18  |  2016  |  6  |  18 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/19  |  2016  |  6  |  19 |  Bill       |  Price      | Level 1  | NULL
1001      |  2016/6/20  |  2016  |  6  |  20 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/21  |  2016  |  6  |  21 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/22  |  2016  |  6  |  22 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/23  |  2016  |  6  |  23 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/24  |  2016  |  6  |  24 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/25  |  2016  |  6  |  25 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/26  |  2016  |  6  |  26 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/27  |  2016  |  6  |  27 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/28  |  2016  |  6  |  28 | Bill        |  Price      | Level 1  | NULL
1001      |  2016/6/29  |  2016  |  6  |  29  | Bill       |  Price      | Level 1  | NULL
1001      |  2016/6/30  |  2016  |  6  |  30  | Bill       |  Price      | Level 1  | NULL

我有一个 While Loop 正在为我工​​作并暂时更新丢失的记录,但性能很糟糕。

谢谢

【问题讨论】:

“工作循环”是什么意思? 我有一个使用while loop 的查询,该查询当前正在更新缺少日期和数据的数据库。但是我必须在每个月的每一天循环遍历每个员工,并且有超过 10,000 条记录,这花费了太长时间。 您已经展示了您的数据的样子。这很棒。请向我们展示最终结果的样子。另外,请添加带有 SQL Server 版本的标签。 @VladimirBaranov,谢谢,我已经更新以显示结果需要是什么样的。 【参考方案1】:

样本数据

DECLARE @StartDate date = '2016-06-01';
DECLARE @EndDate   date = '2016-07-01';

DECLARE @Table_One TABLE (
    Staff_ID int, 
    dt date, 
    First_Name nvarchar(50), 
    Last_Name nvarchar(50), 
    Section  nvarchar(50), 
    Time_Worked datetime);

INSERT INTO @Table_One(Staff_ID, dt, First_Name, Last_Name, Section, Time_Worked) 
VALUES
(1001, '2016-06-01', 'Bill', 'Price ', 'Level 1', '2016-06-01 8:30:00.000'),
(1001, '2016-06-05', 'Bill', 'Price ', 'Level 1', '2016-06-05 8:30:00.000'),
(1001, '2016-06-09', 'Bill', 'Price ', 'Level 1', '2016-06-09 8:30:00.000'),
(1001, '2016-06-12', 'Bill', 'Price ', 'Level 1', '2016-06-12 8:30:00.000'),
(1002, '2016-06-01', 'Mary', 'Somers', 'Level 1', '2016-06-01 8:30:00.000'),
(1002, '2016-06-05', 'Mary', 'Somers', 'Level 1', '2016-06-05 8:30:00.000'),
(1002, '2016-06-08', 'Mary', 'Somers', 'Level 1', '2016-06-08 8:30:00.000'),
(1003, '2016-06-03', 'Mark', 'Jones ', 'Level 1', '2016-06-03 8:30:00.000'),
(1003, '2016-06-05', 'Mark', 'Jones ', 'Level 1', '2016-06-05 8:30:00.000');

查询

当日期有间隔时,查询使用CROSS APPLY“插入”行。它使用您的 Tally 数字表根据需要多次复制当前行。

@StartDate 早于第一行的日期时,有一个特殊处理。这就是为什么有两个SELECTs 联合在一起的原因。

CTE.PrevDate IS NULL 仅过滤此类行,它们会根据需要重复多次。

WITH
CTE
AS
(
    SELECT *
        ,LAG(dt) OVER (PARTITION BY Staff_ID ORDER BY dt) AS PrevDate
        ,LEAD(dt) OVER (PARTITION BY Staff_ID ORDER BY dt) AS NextDate
    FROM @Table_One AS T
)
SELECT
    Staff_ID
    ,NewDate
    ,First_Name
    ,Last_Name
    ,Section
    ,CASE WHEN NewDate = dt THEN Time_Worked ELSE NULL END AS Time_Worked
FROM
    CTE
    CROSS APPLY
    (
        SELECT DATEADD(day, Tally.ID - 1, CTE.dt) AS NewDate
        FROM dbo.Tally
        WHERE Tally.ID <= DATEDIFF(day, CTE.dt, ISNULL(CTE.NextDate, @EndDate))
    ) AS CA_Next

UNION ALL

SELECT
    Staff_ID
    ,NewDate
    ,First_Name
    ,Last_Name
    ,Section
    ,CASE WHEN NewDate = dt THEN Time_Worked ELSE NULL END AS Time_Worked
FROM
    CTE
    CROSS APPLY
    (
        SELECT DATEADD(day, - Tally.ID, CTE.dt) AS NewDate
        FROM dbo.Tally
        WHERE Tally.ID <= DATEDIFF(day, @StartDate, CTE.dt)
    ) AS CA_Prev
WHERE 
    CTE.PrevDate IS NULL

ORDER BY Staff_ID, NewDate;

结果

+----------+------------+------------+-----------+---------+-------------------------+
| Staff_ID |  NewDate   | First_Name | Last_Name | Section |       Time_Worked       |
+----------+------------+------------+-----------+---------+-------------------------+
|     1001 | 2016-06-01 | Bill       | Price     | Level 1 | 2016-06-01 08:30:00.000 |
|     1001 | 2016-06-02 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-03 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-04 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-05 | Bill       | Price     | Level 1 | 2016-06-05 08:30:00.000 |
|     1001 | 2016-06-06 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-07 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-08 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-09 | Bill       | Price     | Level 1 | 2016-06-09 08:30:00.000 |
|     1001 | 2016-06-10 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-11 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-12 | Bill       | Price     | Level 1 | 2016-06-12 08:30:00.000 |
|     1001 | 2016-06-13 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-14 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-15 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-16 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-17 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-18 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-19 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-20 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-21 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-22 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-23 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-24 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-25 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-26 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-27 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-28 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-29 | Bill       | Price     | Level 1 | NULL                    |
|     1001 | 2016-06-30 | Bill       | Price     | Level 1 | NULL                    |
|     1002 | 2016-06-01 | Mary       | Somers    | Level 1 | 2016-06-01 08:30:00.000 |
|     1002 | 2016-06-02 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-03 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-04 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-05 | Mary       | Somers    | Level 1 | 2016-06-05 08:30:00.000 |
|     1002 | 2016-06-06 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-07 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-08 | Mary       | Somers    | Level 1 | 2016-06-08 08:30:00.000 |
|     1002 | 2016-06-09 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-10 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-11 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-12 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-13 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-14 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-15 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-16 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-17 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-18 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-19 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-20 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-21 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-22 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-23 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-24 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-25 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-26 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-27 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-28 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-29 | Mary       | Somers    | Level 1 | NULL                    |
|     1002 | 2016-06-30 | Mary       | Somers    | Level 1 | NULL                    |
|     1003 | 2016-06-01 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-02 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-03 | Mark       | Jones     | Level 1 | 2016-06-03 08:30:00.000 |
|     1003 | 2016-06-04 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-05 | Mark       | Jones     | Level 1 | 2016-06-05 08:30:00.000 |
|     1003 | 2016-06-06 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-07 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-08 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-09 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-10 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-11 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-12 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-13 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-14 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-15 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-16 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-17 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-18 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-19 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-20 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-21 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-22 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-23 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-24 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-25 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-26 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-27 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-28 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-29 | Mark       | Jones     | Level 1 | NULL                    |
|     1003 | 2016-06-30 | Mark       | Jones     | Level 1 | NULL                    |
+----------+------------+------------+-----------+---------+-------------------------+

将生成的行重新插入到原始表中

一开始我并没有意识到你要改变原来的表,所以我写了一个SELECT 查询返回一个需要的结果集。很容易将其调整为 INSERT 查询,该查询会将新行添加到原始表中。

我所做的只是添加了一个过滤器WHERE NewDate &lt;&gt; dt,它确保只插入以前不存在的新行。

WITH
CTE
AS
(
    SELECT
        Staff_ID
        ,dt
        ,First_Name
        ,Last_Name
        ,Section
        ,Time_Worked
        ,LAG(dt) OVER (PARTITION BY Staff_ID ORDER BY dt) AS PrevDate
        ,LEAD(dt) OVER (PARTITION BY Staff_ID ORDER BY dt) AS NextDate
    FROM @Table_One AS T
)
INSERT INTO @Table_One(Staff_ID, dt, First_Name, Last_Name, Section, Time_Worked) 
SELECT
    Staff_ID
    ,NewDate
    ,First_Name
    ,Last_Name
    ,Section
    ,NULL AS Time_Worked
FROM
    CTE
    CROSS APPLY
    (
        SELECT DATEADD(day, Tally.ID - 1, CTE.dt) AS NewDate
        FROM dbo.Tally
        WHERE Tally.ID <= DATEDIFF(day, CTE.dt, ISNULL(CTE.NextDate, @EndDate))
    ) AS CA_Next
WHERE
    NewDate <> dt

UNION ALL

SELECT
    Staff_ID
    ,NewDate
    ,First_Name
    ,Last_Name
    ,Section
    ,NULL AS Time_Worked
FROM
    CTE
    CROSS APPLY
    (
        SELECT DATEADD(day, - Tally.ID, CTE.dt) AS NewDate
        FROM dbo.Tally
        WHERE Tally.ID <= DATEDIFF(day, @StartDate, CTE.dt)
    ) AS CA_Prev
WHERE 
    CTE.PrevDate IS NULL

ORDER BY Staff_ID, NewDate;

结果

要检查结果,只需 SELECT 原始表中的所有内容。

SELECT * FROM @Table_One ORDER BY Staff_ID, dt;

结果如上所示。

【讨论】:

是否可以将Time_Worked 列作为NULL 用于新行? @KyloRen,是的,只需添加 CASE 即可比较日期。我更新了答案。 太棒了,投赞成票给了很大的帮助。让我测试一切,但这看起来像是答案。一旦我测试过,我将标记为答案。再次,非常感谢所有的帮助!!!! 顺便说一句,这是使用我的Tally 表吗? 我无法让它工作。查询通过,但没有任何改变???【参考方案2】:

另一个选项:(更新 [再次]) 创建并填充示例表(在您的下一个问题中保存我们这一步)

DECLARE @T as TABLE
(
    Staff_ID    int,
    [Date]      date, 
    [Year]      int,
    Mon         int,
    [Day]       int,
    First_Name  varchar(10),
    Last_Name   varchar(10),
    Section     varchar(10), 
    Time_Worked datetime
)

INSERT INTO @T VALUES
(1001, '2016-06-01', 2016, 6, 1, 'Bill', 'Price', 'Level 1', '2016-06-01 8:30:00.000'),
(1001, '2016-06-05', 2016, 6, 5, 'Bill', 'Price', 'Level 1', '2016-06-05 8:30:00.000'),
(1001, '2016-06-09', 2016, 6, 9, 'Bill', 'Price', 'Level 1', '2016-06-09 8:30:00.000'),
(1001, '2016-07-05', 2016, 7, 5, 'Bill', 'Price', 'Level 2', '2016-06-12 8:30:00.000'), -- Different month
(1002, '2016-06-01', 2016, 6, 1, 'Mary', 'Somers', 'Level 1', '2016-06-01 8:30:00.000'),
(1002, '2016-06-05', 2016, 6, 5, 'Mary', 'Somers', 'Level 1', '2016-06-05 8:30:00.000'),
(1002, '2016-06-08', 2016, 6, 8, 'Mary', 'Somers', 'Level 1', '2016-06-08 8:30:00.000'),
(1003, '2016-06-03', 2016, 6, 3, 'Mark', 'Jones', 'Level 1', '2016-06-03 8:30:00.000'),
(1003, '2016-06-04', 2016, 6, 4, 'Mark', 'Jones', 'Level 1', '2016-06-05 8:30:00.000')

声明并填充@StartDate@EndDate

DECLARE @StartDate  datetime = '2016-06-01',
        @EndDate datetime = '2016-08-01'

INSERT...SELECT 声明:(我选择使用 cte,这样我就不必多次写 dateadd(DD,ID-1,@StartDate)

;WITH Calendar AS
(
    SELECT dateadd(DD,ID-1,@StartDate) as [Date]
    FROM dbo.Tally
    WHERE dateadd(DD,ID-1,@StartDate) < @EndDate
)

INSERT INTO @T (Staff_ID, [Date], [Year], Mon, [Day], First_Name, Last_Name, Section)
SELECT DISTINCT Staff_ID, C.[Date], Year(C.[Date]), MONTH(C.[Date]), DAY(C.[Date]), First_Name, Last_Name, Section
FROM @T T
CROSS APPLY 
(
    SELECT Cal.[Date]
    FROM Calendar Cal
    WHERE MONTH(Cal.[Date]) = MONTH(T.[Date])
    AND YEAR(Cal.[Date]) = YEAR(T.[Date])
    AND NOT EXISTS
    (
        SELECT 1
        FROM @T T2
        WHERE T.Staff_ID = T2.Staff_ID 
        AND T2.[Date] = Cal.[Date]
    )
) C

验证插入:

SELECT Staff_ID, [Date], [Year], Mon, [Day], First_Name, Last_Name, Section, Time_Worked 
FROM @T 
ORDER BY Staff_ID, [Date]

结果:

Staff_ID    Date       Year        Mon         Day         First_Name Last_Name  Section    Time_Worked
----------- ---------- ----------- ----------- ----------- ---------- ---------- ---------- -----------------------
1001        2016-06-01 2016        6           1           Bill       Price      Level 1    2016-06-01 08:30:00.000
1001        2016-06-02 2016        6           2           Bill       Price      Level 1    NULL
1001        2016-06-03 2016        6           3           Bill       Price      Level 1    NULL
1001        2016-06-04 2016        6           4           Bill       Price      Level 1    NULL
1001        2016-06-05 2016        6           5           Bill       Price      Level 1    2016-06-05 08:30:00.000
1001        2016-06-06 2016        6           6           Bill       Price      Level 1    NULL
1001        2016-06-07 2016        6           7           Bill       Price      Level 1    NULL
1001        2016-06-08 2016        6           8           Bill       Price      Level 1    NULL
1001        2016-06-09 2016        6           9           Bill       Price      Level 1    2016-06-09 08:30:00.000
1001        2016-06-10 2016        6           10          Bill       Price      Level 1    NULL
1001        2016-06-11 2016        6           11          Bill       Price      Level 1    NULL
1001        2016-06-12 2016        6           12          Bill       Price      Level 1    NULL
1001        2016-06-13 2016        6           13          Bill       Price      Level 1    NULL
1001        2016-06-14 2016        6           14          Bill       Price      Level 1    NULL
1001        2016-06-15 2016        6           15          Bill       Price      Level 1    NULL
1001        2016-06-16 2016        6           16          Bill       Price      Level 1    NULL
1001        2016-06-17 2016        6           17          Bill       Price      Level 1    NULL
1001        2016-06-18 2016        6           18          Bill       Price      Level 1    NULL
1001        2016-06-19 2016        6           19          Bill       Price      Level 1    NULL
1001        2016-06-20 2016        6           20          Bill       Price      Level 1    NULL
1001        2016-06-21 2016        6           21          Bill       Price      Level 1    NULL
1001        2016-06-22 2016        6           22          Bill       Price      Level 1    NULL
1001        2016-06-23 2016        6           23          Bill       Price      Level 1    NULL
1001        2016-06-24 2016        6           24          Bill       Price      Level 1    NULL
1001        2016-06-25 2016        6           25          Bill       Price      Level 1    NULL
1001        2016-06-26 2016        6           26          Bill       Price      Level 1    NULL
1001        2016-06-27 2016        6           27          Bill       Price      Level 1    NULL
1001        2016-06-28 2016        6           28          Bill       Price      Level 1    NULL
1001        2016-06-29 2016        6           29          Bill       Price      Level 1    NULL
1001        2016-06-30 2016        6           30          Bill       Price      Level 1    NULL
1001        2016-07-01 2016        7           1           Bill       Price      Level 2    NULL
1001        2016-07-02 2016        7           2           Bill       Price      Level 2    NULL
1001        2016-07-03 2016        7           3           Bill       Price      Level 2    NULL
1001        2016-07-04 2016        7           4           Bill       Price      Level 2    NULL
1001        2016-07-05 2016        7           5           Bill       Price      Level 2    2016-06-12 08:30:00.000
1001        2016-07-06 2016        7           6           Bill       Price      Level 2    NULL
1001        2016-07-07 2016        7           7           Bill       Price      Level 2    NULL
1001        2016-07-08 2016        7           8           Bill       Price      Level 2    NULL
1001        2016-07-09 2016        7           9           Bill       Price      Level 2    NULL
1001        2016-07-10 2016        7           10          Bill       Price      Level 2    NULL
1001        2016-07-11 2016        7           11          Bill       Price      Level 2    NULL
1001        2016-07-12 2016        7           12          Bill       Price      Level 2    NULL
1001        2016-07-13 2016        7           13          Bill       Price      Level 2    NULL
1001        2016-07-14 2016        7           14          Bill       Price      Level 2    NULL
1001        2016-07-15 2016        7           15          Bill       Price      Level 2    NULL
1001        2016-07-16 2016        7           16          Bill       Price      Level 2    NULL
1001        2016-07-17 2016        7           17          Bill       Price      Level 2    NULL
1001        2016-07-18 2016        7           18          Bill       Price      Level 2    NULL
1001        2016-07-19 2016        7           19          Bill       Price      Level 2    NULL
1001        2016-07-20 2016        7           20          Bill       Price      Level 2    NULL
1001        2016-07-21 2016        7           21          Bill       Price      Level 2    NULL
1001        2016-07-22 2016        7           22          Bill       Price      Level 2    NULL
1001        2016-07-23 2016        7           23          Bill       Price      Level 2    NULL
1001        2016-07-24 2016        7           24          Bill       Price      Level 2    NULL
1001        2016-07-25 2016        7           25          Bill       Price      Level 2    NULL
1001        2016-07-26 2016        7           26          Bill       Price      Level 2    NULL
1001        2016-07-27 2016        7           27          Bill       Price      Level 2    NULL
1001        2016-07-28 2016        7           28          Bill       Price      Level 2    NULL
1001        2016-07-29 2016        7           29          Bill       Price      Level 2    NULL
1001        2016-07-30 2016        7           30          Bill       Price      Level 2    NULL
1001        2016-07-31 2016        7           31          Bill       Price      Level 2    NULL
1002        2016-06-01 2016        6           1           Mary       Somers     Level 1    2016-06-01 08:30:00.000
1002        2016-06-02 2016        6           2           Mary       Somers     Level 1    NULL
1002        2016-06-03 2016        6           3           Mary       Somers     Level 1    NULL
1002        2016-06-04 2016        6           4           Mary       Somers     Level 1    NULL
1002        2016-06-05 2016        6           5           Mary       Somers     Level 1    2016-06-05 08:30:00.000
1002        2016-06-06 2016        6           6           Mary       Somers     Level 1    NULL
1002        2016-06-07 2016        6           7           Mary       Somers     Level 1    NULL
1002        2016-06-08 2016        6           8           Mary       Somers     Level 1    2016-06-08 08:30:00.000
1002        2016-06-09 2016        6           9           Mary       Somers     Level 1    NULL
1002        2016-06-10 2016        6           10          Mary       Somers     Level 1    NULL
1002        2016-06-11 2016        6           11          Mary       Somers     Level 1    NULL
1002        2016-06-12 2016        6           12          Mary       Somers     Level 1    NULL
1002        2016-06-13 2016        6           13          Mary       Somers     Level 1    NULL
1002        2016-06-14 2016        6           14          Mary       Somers     Level 1    NULL
1002        2016-06-15 2016        6           15          Mary       Somers     Level 1    NULL
1002        2016-06-16 2016        6           16          Mary       Somers     Level 1    NULL
1002        2016-06-17 2016        6           17          Mary       Somers     Level 1    NULL
1002        2016-06-18 2016        6           18          Mary       Somers     Level 1    NULL
1002        2016-06-19 2016        6           19          Mary       Somers     Level 1    NULL
1002        2016-06-20 2016        6           20          Mary       Somers     Level 1    NULL
1002        2016-06-21 2016        6           21          Mary       Somers     Level 1    NULL
1002        2016-06-22 2016        6           22          Mary       Somers     Level 1    NULL
1002        2016-06-23 2016        6           23          Mary       Somers     Level 1    NULL
1002        2016-06-24 2016        6           24          Mary       Somers     Level 1    NULL
1002        2016-06-25 2016        6           25          Mary       Somers     Level 1    NULL
1002        2016-06-26 2016        6           26          Mary       Somers     Level 1    NULL
1002        2016-06-27 2016        6           27          Mary       Somers     Level 1    NULL
1002        2016-06-28 2016        6           28          Mary       Somers     Level 1    NULL
1002        2016-06-29 2016        6           29          Mary       Somers     Level 1    NULL
1002        2016-06-30 2016        6           30          Mary       Somers     Level 1    NULL
1003        2016-06-01 2016        6           1           Mark       Jones      Level 1    NULL
1003        2016-06-02 2016        6           2           Mark       Jones      Level 1    NULL
1003        2016-06-03 2016        6           3           Mark       Jones      Level 1    2016-06-03 08:30:00.000
1003        2016-06-04 2016        6           4           Mark       Jones      Level 1    2016-06-05 08:30:00.000
1003        2016-06-05 2016        6           5           Mark       Jones      Level 1    NULL
1003        2016-06-06 2016        6           6           Mark       Jones      Level 1    NULL
1003        2016-06-07 2016        6           7           Mark       Jones      Level 1    NULL
1003        2016-06-08 2016        6           8           Mark       Jones      Level 1    NULL
1003        2016-06-09 2016        6           9           Mark       Jones      Level 1    NULL
1003        2016-06-10 2016        6           10          Mark       Jones      Level 1    NULL
1003        2016-06-11 2016        6           11          Mark       Jones      Level 1    NULL
1003        2016-06-12 2016        6           12          Mark       Jones      Level 1    NULL
1003        2016-06-13 2016        6           13          Mark       Jones      Level 1    NULL
1003        2016-06-14 2016        6           14          Mark       Jones      Level 1    NULL
1003        2016-06-15 2016        6           15          Mark       Jones      Level 1    NULL
1003        2016-06-16 2016        6           16          Mark       Jones      Level 1    NULL
1003        2016-06-17 2016        6           17          Mark       Jones      Level 1    NULL
1003        2016-06-18 2016        6           18          Mark       Jones      Level 1    NULL
1003        2016-06-19 2016        6           19          Mark       Jones      Level 1    NULL
1003        2016-06-20 2016        6           20          Mark       Jones      Level 1    NULL
1003        2016-06-21 2016        6           21          Mark       Jones      Level 1    NULL
1003        2016-06-22 2016        6           22          Mark       Jones      Level 1    NULL
1003        2016-06-23 2016        6           23          Mark       Jones      Level 1    NULL
1003        2016-06-24 2016        6           24          Mark       Jones      Level 1    NULL
1003        2016-06-25 2016        6           25          Mark       Jones      Level 1    NULL
1003        2016-06-26 2016        6           26          Mark       Jones      Level 1    NULL
1003        2016-06-27 2016        6           27          Mark       Jones      Level 1    NULL
1003        2016-06-28 2016        6           28          Mark       Jones      Level 1    NULL
1003        2016-06-29 2016        6           29          Mark       Jones      Level 1    NULL
1003        2016-06-30 2016        6           30          Mark       Jones      Level 1    NULL

【讨论】:

我不确定您所说的“验证插入”是什么意思,您是否将该代码直接放在查询窗口中的 The INSERT...SELECT statement 下方? 没关系,我想通了。非常感谢您的帮助,下次我会确保制作一个示例数据表。为伟大的帮助投了赞成票 这只是一个选择语句,以确保将值正确插入到表中。您实际上并不需要它,它只是证明插入按预期工作。 "我会确保下次制作样本数据表。" - 你让我今天很开心。严重地。您不知道我评论的问题数量“请为示例数据添加相关表 ddl 和 dml”... 我不知道这是需要的,你告诉我它是需要的,这让我很开心。祝你有美好的一天!【参考方案3】:

尝试以下查询

DECLARE @Id INT = 1
DECLARE @StartDate DATETIME = '2016.06.01'
DECLARE @EndDate DATETIME = '2016.06.30'

;WITH Dates (Id, [Date])
AS(

    SELECT @Id AS Id,  @StartDate AS [Date]

    UNION ALL

    SELECT Id + 1 AS Id , DATEADD(DAY, (Id), @StartDate) AS [Date]
    FROM Dates
    WHERE [Date] < @EndDate
)

SELECT
    Result.Staff_ID,
    Result.Date,
    Result.First_Name,
    Result.Last_Name,
    Result.Section,
    MT.Time_Worked
FROM
(       
    SELECT
        D.[Date],
        A.Staff_ID ,
        A.First_Name ,
        A.Last_Name ,
        A.Section
    FROM 
        Dates D CROSS JOIN 
        (
            SELECT DISTINCT 
                IMT.Staff_ID ,                
                IMT.First_Name ,
                IMT.Last_Name ,
                IMT.Section                 
            FROM 
                MainTable IMT
        ) A
) Result LEFT JOIN  
MainTable MT ON Result.[Date] = MT.dt AND Result.Staff_ID = MT.Staff_ID 
ORDER BY
    Result.Staff_ID,
    Result.[Date]

【讨论】:

如果我没记错的话,这只适用于一名员工吗?我需要它来为大约 5000 多名员工工作 是的,我以为你想要这个。

以上是关于理货表在两个日期之间插入缺失的日期? SQL的主要内容,如果未能解决你的问题,请参考以下文章

SQL 自动为表中的缺失日期从前一个日期生成缺失的日期和价格

插入缺失日期/时间的行

MySQL:查找日期范围之间的缺失日期

如何获取日期范围内的缺失值?

SQL:添加缺失的日期和值

SQL Server:填写每个实体具有不同日期范围的缺失日期