4

需要知道如何在相应字段中添加缺少空数据的日期

**05/28/2012    NULL
05/29/2012  NULL
05/30/2012  NULL
05/30/2012  Break In
05/30/2012  Break Out
05/31/2012  NULL**

06/03/2012  NULL
06/03/2012  Break In
06/03/2012  Break Out
06/03/2012  In Duty
06/03/2012  Out Duty
06/04/2012  NULL
06/04/2012  In Duty
06/04/2012  Out Duty
06/05/2012  NULL
06/05/2012  Break In
06/05/2012  Break Out
06/06/2012  NULL
06/06/2012  Break In
06/06/2012  Break Out
06/06/2012  In Duty
06/06/2012  Out Duty
06/07/2012  NULL
06/07/2012  In Duty
06/07/2012  Out Duty

06/10/2012  NULL
06/10/2012  Break Out
06/10/2012  In Duty
06/10/2012  Out Duty
06/11/2012  NULL
06/11/2012  In Duty
06/11/2012  Out Duty
06/12/2012  NULL
06/13/2012  NULL
06/14/2012  NULL

我需要的结果是:

05/28/2012  NULL
05/29/2012  NULL
05/30/2012  NULL
05/30/2012  Break In
05/30/2012  Break Out
05/31/2012  NULL
06/01/2012      null
06/02/2012      null
06/03/2012  NULL
06/03/2012  Break In
06/03/2012  Break Out
06/03/2012  In Duty
06/03/2012  Out Duty
06/04/2012  NULL
06/04/2012  In Duty
06/04/2012  Out Duty
06/05/2012  NULL
06/05/2012  Break In
06/05/2012  Break Out
06/06/2012  NULL
06/06/2012  Break In
06/06/2012  Break Out
06/06/2012  In Duty
06/06/2012  Out Duty
06/07/2012  NULL
06/07/2012  In Duty
06/07/2012  Out Duty
06/08/2012      null
06/09/2012      null
06/10/2012  NULL
06/10/2012  Break Out
06/10/2012  In Duty
06/10/2012  Out Duty
06/11/2012  NULL
06/11/2012  In Duty
06/11/2012  Out Duty
06/12/2012  NULL
06/13/2012  NULL
06/14/2012  NULL
4

3 回答 3

4

最好的选择是保留一个日历表,其中包含您要计算的几年的所有日期,然后加入该表

select date,col1
from calender_table c
left join 
your_table t
on c.[date]=t.[date]

你可以很容易地创建一个日历表。网络上有很多可用的脚本。 点击查看示例

于 2012-09-26T08:03:18.597 回答
2
Declare @stDate datetime='05/28/2012'
declare @eddate datetime='06/14/2012'
select DATEADD(day,number,@stdate) from master..spt_values where type='P'
and DATEADD(day,number,@stdate) <= @eddate

在这里只需将此结果与日期列上的表格连接起来即可获取缺失的日期

于 2012-09-26T08:07:33.977 回答
2

形成具有开始和结束日期范围的日期日历,并与您的表执行左连接以获得所需的结果。

例如

DECLARE @t TABLE(Dt Datetime, Value VARCHAR(20) NULL)
INSERT INTO @t VALUES
('05/28/2012',NULL),
('05/29/2012',NULL),
('05/30/2012',NULL),('05/30/2012','Break In'),('05/30/2012','Break Out'),
('05/31/2012',NULL),
('06/03/2012',NULL),('06/03/2012','Break In'),('06/03/2012','Break Out'),('06/03/2012','In Duty'),('06/03/2012','Out Duty'),
('06/04/2012',NULL),('06/04/2012','In Duty'),('06/04/2012','Out Duty'),
('06/05/2012',NULL),('06/05/2012','Break In'),('06/05/2012','Break Out'),
('06/06/2012',NULL),('06/06/2012','Break In'),('06/06/2012','Break Out'),('06/06/2012','In Duty'),('06/06/2012','Out Duty'),
('06/07/2012',NULL),('06/07/2012','In Duty'),('06/07/2012','Out Duty'),
('06/10/2012',NULL),('06/10/2012','Break Out'),('06/10/2012','In Duty'),('06/10/2012','Out Duty'),
('06/11/2012',NULL),('06/11/2012','In Duty'),('06/11/2012','Out Duty'),
('06/12/2012',NULL),
('06/13/2012',NULL),
('06/14/2012',NULL)


DECLARE @startDate DATETIME, @endDate DATETIME
SELECT @startDate = '2012-05-28', @endDate = '2012-06-14' --yyyy-mm-dd
;WITH Calender AS (
    SELECT @startDate AS CalanderDate
    UNION ALL
    SELECT CalanderDate + 1 FROM Calender
    WHERE CalanderDate + 1 <= @endDate
)
SELECT 
    [Date] = Convert(VARCHAR(10),CalanderDate,101)
    ,Value
FROM Calender c
LEFT JOIN @t t 
ON t.Dt = c.CalanderDate

结果

Date    Value
05/28/2012  NULL
05/29/2012  NULL
05/30/2012  NULL
05/30/2012  Break In
05/30/2012  Break Out
05/31/2012  NULL
06/01/2012  NULL
06/02/2012  NULL
06/03/2012  NULL
06/03/2012  Break In
06/03/2012  Break Out
06/03/2012  In Duty
06/03/2012  Out Duty
06/04/2012  NULL
06/04/2012  In Duty
06/04/2012  Out Duty
06/05/2012  NULL
06/05/2012  Break In
06/05/2012  Break Out
06/06/2012  NULL
06/06/2012  Break In
06/06/2012  Break Out
06/06/2012  In Duty
06/06/2012  Out Duty
06/07/2012  NULL
06/07/2012  In Duty
06/07/2012  Out Duty
06/08/2012  NULL
06/09/2012  NULL
06/10/2012  NULL
06/10/2012  Break Out
06/10/2012  In Duty
06/10/2012  Out Duty
06/11/2012  NULL
06/11/2012  In Duty
06/11/2012  Out Duty
06/12/2012  NULL
06/13/2012  NULL
06/14/2012  NULL

希望这可以帮助

于 2012-09-26T08:13:58.787 回答