2012-09-26 76 views
4

需要知道如何添加日期用零數據丟失的相應字段查詢如何添加缺少的日期在SQL

**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 
+0

[你嘗試過什麼?](http://mattgemmell.com/2008/12/08/what-have-you -//請發佈一個工作查詢,無所謂是不正確的,至少是一個起點,也許你離解決方案不遠 – Yaroslav

回答

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 

希望這有助於

+0

先生,如果有一個更多的coloum與名稱事件,並與案件我能夠添加事件,但無法添加新增加的數據 – Sameer

+0

並感謝您的日期解決方案工作精細 – Sameer

3

最好的選擇是保留一個日曆表,其中包含您想要計算的某些年份的所有日期,然後與該表格左加入

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

你可以很容易地創建一個日曆表。網上有很多腳本可用。 click for examples

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 

這裏剛剛加入這個結果與你的表上的日期欄來獲得丟失的日期

+0

謝謝大家得到的結果。 – Sameer

+0

然後接受幫助你的答案 –