0

我有一个带有数据字段成为列的数据透视表的存储过程。目前返回的数据有一些冗余,想看看是否有办法纠正这个问题。存储过程实际上是在提供.rdlc报告,当前输出如下所示:

Period | No Of Interim | Excellent | Very Good | Good | Satisfactory | Unsatisfactory 
-------------------------------------------------------------------------------------
1                                                    1 
12                                            1 
18                   1
18                               1 
18                                                                  1 
19                                                                                  1
19                                                   1 
2                    1 
2                                1 

这就是它需要的样子:

Period | No Of Interim | Excellent | Very Good | Good | Satisfactory | Unsatisfactory 
-------------------------------------------------------------------------------------
1                                                    1 
2                    1           1
12                                            1 
18                   1           1                                  1 
19                                                   1                              1

周期列需要按升序排序,并且需要将重复实例添加到同一行。

负责输出的存储过程和对应的视图如下:

BEGIN
DECLARE @cols NVARCHAR(MAX)
DECLARE @query NVARCHAR(MAX)

SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT @cols = STUFF((SELECT '],[' + [Description] 
    FROM vQualScoringGrade 
    GROUP BY [Description] 
    ORDER BY MAX([orderby]), [Description] 
    FOR XML PATH('')), 1,2,'') + ']'

SET @query = N'SELECT Period, ' + @cols + ' FROM 
(SELECT Period, Description, Value, OrderBy FROM 
vQualScoringGrade) p
PIVOT (SUM([Value]) for [Description] IN ( ' + @cols + ' )) AS pvt ORDER BY Period'

execute(@query)
end

SELECT TOP (100) PERCENT Period, Description, GradeCount AS Value, OrderBy
FROM (SELECT CASE CHARINDEX('.', Number) WHEN 0 THEN Number ELSE 
     REPLACE(LEFT(Number, CHARINDEX('.', Number)), '.', '') 
     END AS Period, 'NoOfInterim' AS Description, COUNT(Number) AS 
        GradeCount, 1 AS OrderBy
FROM vQualScoringExcellent AS vQualScoringExcellent
     GROUP BY Number, Description
UNION
SELECT CASE CHARINDEX('.', Number) WHEN 0 THEN Number ELSE 
     REPLACE(LEFT(Number, CHARINDEX('.', Number)), '.', '') 
     END AS Period, 'Excellent' AS Description, COUNT(Description) AS 
        GradeCount, 2 AS OrderBy
FROM vQualScoringExcellent AS vQualScoringExcellent_1
     GROUP BY Number, Description
UNION
SELECT CASE CHARINDEX('.', Number) WHEN 0 THEN Number ELSE 
     REPLACE(LEFT(Number, CHARINDEX('.', Number)), '.', '') 
     END AS Period, 'VeryGood' AS Description, COUNT(Description) AS 
        GradeCount, 3 AS OrderBy
FROM vQualScoringVeryGood AS vQualScoringVeryGood
     GROUP BY Number, Description
UNION
SELECT CASE CHARINDEX('.', Number) WHEN 0 THEN Number ELSE 
      REPLACE(LEFT(Number, CHARINDEX('.', Number)), '.', '') 
      END AS Period, 'Good' AS Description, COUNT(Description) AS 
         GradeCount, 4 AS OrderBy
FROM vQualScoringGood AS vQualScoringGood
      GROUP BY Number, Description
UNION
SELECT CASE CHARINDEX('.', Number) WHEN 0 THEN Number ELSE 
     REPLACE(LEFT(Number, CHARINDEX('.', Number)), '.', '') 
     END AS Period, 'Satisfactory' AS Description, COUNT(Description) AS 
        GradeCount, 5 AS OrderBy
FROM vQualScoringSatisfactory AS vQualScoringSatisfactory
     GROUP BY Number, Description
UNION
SELECT CASE CHARINDEX('.', Number) WHEN 0 THEN Number ELSE 
     REPLACE(LEFT(Number, CHARINDEX('.', Number)), '.', '') 
     END AS Period, 'Unsatisfactory' AS Description, COUNT(Description) AS 
        GradeCount, 6 AS OrderBy
FROM vQualScoringUnsatisfactory AS vQualScoringUnsatisfactory
     GROUP BY Number, Description) AS QualScoringGrade
ORDER BY OrderBy
4

1 回答 1

1

我猜这个问题是由于OrderBy您的 PIVOT 查询中的列造成的。此行是您的问题的原因:

SELECT Period, Description, Value, OrderBy

我会将查询更改为:

SELECT @cols = STUFF((SELECT '],[' + [Description] 
    FROM vQualScoringGrade 
    GROUP BY [Description] 
    ORDER BY MAX([orderby]), [Description] 
    FOR XML PATH('')), 1,2,'') + ']'

SET @query = N'SELECT Period, ' + @cols + ' 
               FROM 
               (
                  SELECT Period, Description, Value
                  FROM vQualScoringGrade
               ) p
              PIVOT 
              (
                 SUM([Value]) 
                 for [Description] IN ( ' + @cols + ' )
              ) AS pvt 
              ORDER BY Period'

execute(@query);

OrderBy列未在外部选择或 PIVOT 中使用,但您将其包含在子查询中。此列用于 PIVOT 期间发生的分组。如果值不同,那么您将获得多行。

OrderBy您可以通过在最终选择列表中包含 来轻松测试这一点。

于 2013-04-29T22:03:16.560 回答