Pivot and SUM with two columns

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

                                                         PIVOT (
														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

Open in new window

To achieve this:

Adrian CrossAsked:
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.

ste5anSenior 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 ,
     FROM   @NotNormalized NN
         UNPIVOT (   ScoreValue
                     FOR ScoreType IN ( Score, Score2 )) U )
FROM   Normalized N
    PIVOT (   SUM(ScoreValue)
              FOR ScoreType IN ( [1Score], [1Score2], [2Score], [2Score2], [3Score], [3Score2], [4Score], [4Score2], [5Score], [5Score2] )) P;

Open in new window

Mark WillsTopic 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],

Open in new window


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.

Start your 7-day free trial
Mark WillsTopic AdvisorCommented:
Hahaha... When I started typing, no one had entered a comment - my slow typing is evident :)
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

ste5anSenior DeveloperCommented:
Happens to all of us sometimes ;)
Adrian CrossAuthor Commented:
Thanks guys. I'll have a go when I'm in the computer.
Adrian CrossAuthor 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.