# Pivot and SUM with two columns

Is it possible to pivot and sum 2 columns..something like this:

``````	....
PIVOT (
SUM(Score)

FOR [Question]
IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11],
[12], [13], [14], [15], [16], [17], [18], [19], [20])

) AS pvt

PIVOT (
SUM(Score2) FOR [Question]
IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11],
[12], [13], [14], [15], [16], [17], [18], [19], [20])

) as pvt2
``````

To achieve this:

Thanks
Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Senior DeveloperCommented:
Yes and no.. e.g.

``````DECLARE @NotNormalized TABLE
(
QuestionID INT ,
Score INT ,
Score2 INT
);

INSERT INTO @NotNormalized ( QuestionID ,
Score ,
Score2 )
VALUES ( 1, 1, 2 ) ,
( 2, 3, 4 ) ,
( 3, 5, 6 ) ,
( 4, 7, 8 ) ,
( 5, 9, 0 );

WITH Normalized
AS ( SELECT CAST(U.QuestionID AS VARCHAR(255)) + U.ScoreType AS ScoreType ,
U.ScoreValue
FROM   @NotNormalized NN
UNPIVOT (   ScoreValue
FOR ScoreType IN ( Score, Score2 )) U )
SELECT *
FROM   Normalized N
PIVOT (   SUM(ScoreValue)
FOR ScoreType IN ( [1Score], [1Score2], [2Score], [2Score2], [3Score], [3Score2], [4Score], [4Score2], [5Score], [5Score2] )) P;
``````
Topic AdvisorCommented:
Yep, because Score and Score2 are both the same 'type' you can unpivot to make the pivot work...

Assume the 1,2,3,4,5 are the question numbers ?

Have a look at :

``````;with cte as
( select question, Score, Score2
from yourTable
), upv as
( select colvalues,cast(question as varchar)+colnames as colnames
from cte
unpivot (ColValues for Colnames in ([Score],[Score2])) U
) select *
from upv
Pivot (max(colvalues) for colnames in ([1Score],[2Score],[3Score],[4Score],[5Score],
[1Score2],[2Score2],[3Score2],[4Score2],[5Score2]))P
``````

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Topic AdvisorCommented:
Hahaha... When I started typing, no one had entered a comment - my slow typing is evident :)
Senior DeveloperCommented:
Happens to all of us sometimes ;)
Author Commented:
Thanks guys. I'll have a go when I'm in the computer.
Author Commented:
Thanks so much
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.