Rick Savoy
Rick Savoy

Reputation: 341

T SQL Pivot still one row per pivot column

I am trying to create a pivot table in SQL 2008R2. I'm trying to reproduce and Access Pivot table in SQL. When I run the following script, I get one record for each pivot column instead of one record with two populated pivoted columns.

SELECT * FROM
(SELECT DataView.MAEID
, DataView.MoYr
, DataView.ChemicalName
, TblCategories.CatDesc
, DataView.[CAS#]
, DataView.HAP
, Sum([SpecAE]/2000) AS [Total SpecAE Tons]
, SUM([SpecAE]) AS [SpecAE]
FROM TblCategories 
INNER JOIN DataView 
ON TblCategories.CatID = DataView.Category
GROUP BY DataView.MAEID
, DataView.MoYr
, DataView.ChemicalName
, DataView.[CAS#]
, DataView.HAP
, TblCategories.CatDesc) TBL
PIVOT (
 Sum([SpecAE])
 FOR CatDesc IN ([INCIINERABLE LIQUIDS], [Supplemental Fuels])
)pvt

Any thoughts?

Upvotes: 0

Views: 937

Answers (1)

ZLK
ZLK

Reputation: 2884

You haven't provided sample data, so I'll explain your issue with an example. Let's say I have a very simple table with some very simple values:

DECLARE @T TABLE
(
    MainColumn INT NOT NULL,
    PivotColumn INT NOT NULL,
    SumColumn INT NOT NULL
);

INSERT @T VALUES
(1, 1, 10), (1, 1, 20), (1, 1, 30),
(1, 2, 60),
(1, 3, 50),
(2, 1, 10), (2, 1, 15),
(2, 2, 20),
(3, 1, 10),
(3, 2, 10),
(4, 1, 150);

If I perform the following query:

SELECT MainColumn,
       PivotColumn,
       PivotValue = SUM(SumColumn),
       OtherSum = SUM(SumColumn / 5)
FROM @T
GROUP BY MainColumn, PivotColumn
ORDER BY MainColumn, PivotColumn

I get:

+------------+-------------+------------+----------+
| MainColumn | PivotColumn | PivotValue | OtherSum |
+------------+-------------+------------+----------+
|          1 |           1 |         60 |       12 |
|          1 |           2 |         60 |       12 |
|          1 |           3 |         50 |       10 |
|          2 |           1 |         25 |        5 |
|          2 |           2 |         20 |        4 |
|          3 |           1 |         10 |        2 |
|          3 |           2 |         10 |        2 |
|          4 |           1 |        150 |       30 |
+------------+-------------+------------+----------+

Now if I use a PIVOT to pivot the PivotValue for each PivotColumn, it's going to group by MainColumn AND OtherSum column. A pivot groups by every column that isn't part of the pivot.

So my result set will be split into (MainColumn=1, OtherSum=12), (MainColumn=1, OtherSum=10), (MainColumn=2, OtherSum=5), (MainColumn=2, OtherSum=4), etc... I will get a new line for each of these values. If the OtherSum value was unique for each line, I'd expect 8 lines with a pivot.

If I remove OtherSum from my result set, my result set is just going to group by MainColumn alone, so it'll all be on one line for each distinct MainColumn value, since that's the only column the pivot would group by.

If getting the other sum value is important, I can do something like the following:

SELECT P.MainColumn,
       Val1A = P.[1],
       Val1B = P.[1] / 5,
       Val2A = P.[2],
       Val2B = P.[2] / 5,
       Val3A = P.[3],
       Val3B = P.[3] / 5
FROM
(
    SELECT MainColumn,
           PivotColumn,
           PivotValue = SUM(SumColumn)
    FROM @T
    GROUP BY MainColumn, PivotColumn
) AS T
PIVOT
(
    SUM(PivotValue) FOR PivotColumn IN ([1], [2], [3])
) AS P;

Upvotes: 1

Related Questions