透视查询生成重复行

时间:2013-11-07 05:44:48

标签: sql sql-server

我的透视查询正在生成重复的行。任何人都可以解决这个问题吗?

DECLARE @SQL AS VARCHAR(MAX)
DECLARE @Columns AS VARCHAR (MAX)
DECLARE @ColumnsRollup AS VARCHAR (MAX)

SELECT @Columns =
COALESCE(@Columns + ', ','')+ QUOTENAME(ProjectId)
FROM
(
    SELECT distinct ProjectId 
    FROM tblCPS where MWM=0
) AS B
ORDER BY B.ProjectId

SELECT @ColumnsRollup =
COALESCE(@ColumnsRollup + ', Sum(','Sum(')+ QUOTENAME(cast(ProjectId as varchar(10)))+') as Project'+cast(ProjectId as varchar(10))
FROM
(
    SELECT distinct ProjectId 
    FROM tblCPS where MWM=0
) AS B
ORDER BY B.ProjectId

SET @SQL = '
            WITH PivotData AS
            (
               SELECT  ContractorId, ProjectId, Amount,SUM(Amount) OVER (PARTITION BY ContractorId )/2 as ''Total''
                FROM tblCps 
            )
            SELECT case when ContractorId is not null then cast(ContractorId as varchar(10))
                else ''Totals'' end ContractorId, ' + @ColumnsRollup+ ',Total
            FROM
            (
                SELECT ContractorId, ' + @Columns + ',Total
                FROM PivotData
                PIVOT
                (
                    SUM(Amount)
                    FOR ProjectId IN (' + @Columns + ')
                ) AS PivotResult
            ) src 
            GROUP BY ContractorId,Total with rollup'

EXEC (@SQL)

这是我得到的结果

1   10.25   20.35   NULL    NULL    NULL    NULL    NULL    NULL    NULL    30.60
1   10.25   20.35   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
10  NULL    NULL    NULL    NULL    NULL    NULL    303.15  313.25  323.35  939.75
10  NULL    NULL    NULL    NULL    NULL    NULL    303.15  313.25  323.35  NULL
2   40.55   NULL    30.45   50.65   NULL    NULL    NULL    NULL    NULL    121.65
2   40.55   NULL    30.45   50.65   NULL    NULL    NULL    NULL    NULL    NULL
3   70.85   60.75   NULL    NULL    172.00  NULL    NULL    NULL    NULL    303.60
3   70.85   60.75   NULL    NULL    172.00  NULL    NULL    NULL    NULL    NULL
4   101.15  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    101.15
4   101.15  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
5   111.25  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    111.25
5   111.25  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
6   394.35  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    394.35
6   394.35  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
7   151.65  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    151.65
7   151.65  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
8   161.75  171.85  181.95  192.05  202.15  212.25  222.35  232.45  242.55  1819.35
8   161.75  171.85  181.95  192.05  202.15  212.25  222.35  232.45  242.55  NULL
9   NULL    252.65  262.75  272.85  NULL    282.95  293.05  NULL    NULL    1364.25
9   NULL    252.65  262.75  272.85  NULL    282.95  293.05  NULL    NULL    NULL
Totals  1041.80 505.60  475.15  515.55  374.15  495.20  818.55  545.70  565.90  NULL

1 个答案:

答案 0 :(得分:2)

ROLLUP operator生成一个结果集,显示所选列中值的层次结构的聚合。对于GROUP BY子句右侧列中的每个值,ROLLUP操作报告列中值的组合或左侧的列。

这里Pivot没有生成重复的行,但它是Roll up运算符。您认为重复的第二行实际上会报告ContractorId维度中值为“Total”的所有行的小计。为“总计”维度返回值null,以显示该行报告的聚合包含具有“总计”维度的任何值的行。

以下查询应该有效:

SET @SQL = '
WITH PivotData AS
(
SELECT  ContractorId, ProjectId, Amount,SUM(Amount) OVER (PARTITION BY ContractorId )/2 as ''Total''
FROM tblCps 
)
SELECT case when ContractorId is not null then cast(ContractorId as varchar(10))
else ''Totals'' end ContractorId, ' + @ColumnsRollup+ ',sum(Total)
FROM
(
SELECT ContractorId, ' + @Columns + ',Total
FROM PivotData
PIVOT
(
SUM(Amount)
FOR ProjectId IN (' + @Columns + ')
) AS PivotResult
) src 
GROUP BY ContractorId with rollup'-- generate a report considering values 
                                  -- for contractor id only and not Total
EXEC (@SQL);

希望这有帮助!!!