TSQL Pivot抛出语法错误

时间:2014-04-14 15:26:20

标签: sql sql-server tsql

我想PIVOT以下查询结果显示每个项目状态代码的列。

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

我想要做的是转动查询以为每个Project Stage Code创建列以匹配以下结果:

enter image description here

基本上,我需要为每个唯一Change Date添加一行,并将targetDate列值填入相应的新旋转数字ProjectStageCode列。

1 个答案:

答案 0 :(得分: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;