2014-04-14 195 views
0

我想PIVOT以下查詢結果顯示每個項目狀態碼的列。TSQL樞軸拋出語法錯誤

WITH r AS (
    SELECT ROW_NUMBER() OVER (ORDER BY ph.InsertedDateTime) rownum, 
     CAST(ph.InsertedDateTime AS DATE) InsertedDate, ph.Gate_1_TargetDate, ph.Gate_2_TargetDate, ph.Gate_3_TargetDate 
    FROM PROJECT_HIST ph 
    JOIN (
     SELECT ProjectID, MAX(InsertedDateTime) InsertedDateTime 
     FROM PROJECT_HIST 
     GROUP BY ProjectID, CAST(InsertedDateTime AS DATE) 
    ) ph_distinct_date ON ph_distinct_date.InsertedDateTime = ph.InsertedDateTime 
     AND ph_distinct_date.ProjectID = ph.ProjectID 
    WHERE ph.projectid = 100957 
     AND NOT (
      ph.Gate_1_TargetDate IS NULL 
       AND ph.Gate_2_TargetDate IS NULL 
       AND ph.Gate_3_TargetDate IS NULL 
     ) 
), 
fubar AS (
    SELECT rownum, InsertedDate, 0 gateName, NULL targetDate FROM r 
    UNION ALL 
    SELECT rownum, InsertedDate, 1, Gate_1_TargetDate FROM r 
    UNION ALL 
    SELECT rownum, InsertedDate, 2, Gate_2_TargetDate FROM r 
    UNION ALL 
    SELECT rownum, InsertedDate, 3, Gate_3_TargetDate FROM r 
) 
SELECT f1.InsertedDate 'Change Date', f1.gateName 'ProjectStageCode', f1.targetDate 
FROM fubar f1 
LEFT JOIN fubar f2 ON f2.rownum = f1.rownum - 1 
    AND f2.gateName = f1.gateName 
PIVOT(min(f1.InsertedDate) FOR f1.gateName IN ([0],[1],[2],[3])) AS p 
WHERE f1.rownum = 1 
    OR f1.targetDate <> f2.targetDate 
ORDER BY f1.InsertedDate 
; 

Pivot errors

沒有支點的嘗試,該查詢返回當前這個結果對於這個特定的項目:

enter image description here

我想這樣做是轉動的查詢來創建列爲每個項目階段代碼匹配以下結果:

enter image description here

實質上,我需要爲每個唯一的Change Date設置一行,並將targetDate列值填入相應的新轉軸數字ProjectStageCode列。

回答

1

從它的外觀看,你似乎只需要使用一個子查詢,然後再嘗試PIVOT數據。您還需要彙總targetDate而不是InsertedDate

WITH r AS 
(
    SELECT ROW_NUMBER() OVER (ORDER BY ph.InsertedDateTime) rownum, 
     CAST(ph.InsertedDateTime AS DATE) InsertedDate, ph.Gate_1_TargetDate, ph.Gate_2_TargetDate, ph.Gate_3_TargetDate 
    FROM PROJECT_HIST ph 
    JOIN 
    (
     SELECT ProjectID, MAX(InsertedDateTime) InsertedDateTime 
     FROM PROJECT_HIST 
     GROUP BY ProjectID, CAST(InsertedDateTime AS DATE) 
    ) ph_distinct_date 
     ON ph_distinct_date.InsertedDateTime = ph.InsertedDateTime 
     AND ph_distinct_date.ProjectID = ph.ProjectID 
    WHERE ph.projectid = 100957 
     AND NOT (ph.Gate_1_TargetDate IS NULL 
        AND ph.Gate_2_TargetDate IS NULL 
        AND ph.Gate_3_TargetDate IS NULL) 
), 
fubar AS 
(
    SELECT rownum, InsertedDate, 0 gateName, NULL targetDate FROM r 
    UNION ALL 
    SELECT rownum, InsertedDate, 1, Gate_1_TargetDate FROM r 
    UNION ALL 
    SELECT rownum, InsertedDate, 2, Gate_2_TargetDate FROM r 
    UNION ALL 
    SELECT rownum, InsertedDate, 3, Gate_3_TargetDate FROM r 
) 
SELECT ChangeDate, [0],[1],[2],[3] 
FROM 
(
    SELECT f1.InsertedDate ChangeDate, f1.gateName, f1.targetDate 
    FROM fubar f1 
    LEFT JOIN fubar f2 
     ON f2.rownum = f1.rownum - 1 
     AND f2.gateName = f1.gateName 
    WHERE f1.rownum = 1 
     OR f1.targetDate <> f2.targetDate 
) d 
PIVOT 
(
    min(targetDate) 
    FOR gateName IN ([0],[1],[2],[3]) 
) AS p;