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 に答える