2014-10-10 101 views
0

我已經例如以下數據:多次填補缺失0值

shift date  value 
---------------------- 
    A 2014-07-01 5 
    A 2014-07-02 8 
    A 2014-07-03 2 
    B 2014-07-03 1 
    C 2014-07-03 9 

如何創建視圖,其中將在每一天的所有位移(A,B,C)?

shift date  value 
---------------------- 
    A 2014-07-01 5 
    B 2014-07-01 0 // add 0 value for B to 1.7.2014 
    C 2014-07-01 0 // add 0 value for C to 1.7.2014 
    A 2014-07-02 8 
    B 2014-07-02 0 // add 0 value for B to 2.7.2014 
    C 2014-07-02 0 // add 0 value for C to 2.7.2014 
    A 2014-07-03 2 
    B 2014-07-03 1 
    C 2014-07-03 9 

我需要所有三個生產班次(A,B,C)充滿每一天,其中至少一個換檔報道了一些工作

+0

是從同一張表中獲取的數據?或2個不同的表?你可以提供數據庫架構PLZ嗎? – 2014-10-10 11:25:00

+0

這只是一張桌子。我需要全部填滿每一天,其中至少一個換檔報道了一些工作 – gaffcz 2014-10-10 11:26:25

+1

很難知道表架構的三個生產班次(A,B,C)。至少提供您的查詢返回第一個結果 – 2014-10-10 11:30:35

回答

3

這裏是舉例來說,如果你不想把缺少的日期......

DECLARE @table TABLE (shift CHAR(1), date Date, Value INT) 

INSERT INTO @table SELECT 'A', '2014-07-01', 5 
INSERT INTO @table SELECT 'A', '2014-07-02', 8 
INSERT INTO @table SELECT 'A', '2014-07-03', 2 
INSERT INTO @table SELECT 'B', '2014-07-03', 1 
INSERT INTO @table SELECT 'C', '2014-07-03', 9 


;WITH shifts AS (
    SELECT DISTINCT Shift 
    FROM @table 
), allDates AS (
    SELECT DISTINCT date 
    FROM @table 
) 
SELECT S.Shift, AD.date, ISNULL(T.Value, 0) AS Value 
FROM allDates AS AD 
CROSS JOIN shifts AS S 
LEFT JOIN @table AS T 
    ON T.Shift = S.Shift 
    AND T.Date = AD.Date 
ORDER BY AD.date, S.Shift 

結果:

Shift| date |Value 
A |2014-07-01|5 
B |2014-07-01|0 
C |2014-07-01|0 
A |2014-07-02|8 
B |2014-07-02|0 
C |2014-07-02|0 
A |2014-07-03|2 
B |2014-07-03|1 
C |2014-07-03|9 
1

第一交叉聯接與日期以獲得所有combinat的轉變離子。然後,將您桌子上已有的所有組合加入。

select shift.shift, dates.date, coalesce(mytable.value,0) 
from (select distinct shift from mytable) shifts 
cross join (select distinct date from mytable) dates 
left join mytable on mytable.shift = shifts.shift and mytable.date = dates.date; 
+0

這是行得通,謝謝! – gaffcz 2014-10-10 11:48:40

1

注意這也將工作,如果你丟失了一些日期

測試表中的數據

declare @t table(shift char(1), date date, value int) 

insert @t values 
('A','2014-07-01',5), 
('A','2014-07-02',8), 
('A','2014-07-03',2), 
('B','2014-07-03',1), 
('C','2014-07-03',9) 

查詢:

;WITH CTE1 as 
(
    SELECT min(date) MinDate, max(date) MaxDate 
    FROM @t 
), CTE2 as 
(
    SELECT MinDate as date, MaxDate 
    FROM CTE1 
    UNION ALL 
    SELECT DATEADD(day, 1, date), Maxdate 
    FROM CTE2 
    WHERE date < Maxdate 
), CTE3 as 
(
    SELECT distinct shift 
    FROM @t 
) 
SELECT 
    CTE3.shift, 
    CTE2.date, 
    coalesce(t.value, 0) value 
FROM CTE2 
CROSS JOIN CTE3 
LEFT JOIN @t t 
ON 
    CTE3.shift = t.shift and 
    CTE2.date = t.date 
ORDER BY 
    date, 
    shift 

結果:

shift date  value 
A  2014-07-01 5 
B  2014-07-01 0 
C  2014-07-01 0 
A  2014-07-02 8 
B  2014-07-02 0 
C  2014-07-02 0 
A  2014-07-03 2 
B  2014-07-03 1 
C  2014-07-03 9 
1

這裏你去

SELECT SHIFTS,DATES, 0 AS VALUE FROM 
(SELECT DISTINCT Z.A AS SHIFTS,Z.DATES AS DATES FROM 
(SELECT B.A ,DATES,SHIFT,VALUE FROM #A 
CROSS JOIN (SELECT 'A'AS A UNION SELECT 'B' B UNION SELECT 'C' AS C) B)Z 
EXCEPT 
SELECT SHIFT,DATES FROM #A)R 
UNION 
SELECT SHIFT,DATES,VALUE FROM #A 
ORDER BY DATES,SHIFTS