PIVOT question

I have a table for which the data looks like below:
Name                       Date                   Count1                  Count2
TEST                        Nov 2007           1000                      2000
TEST                        Dec 2007           1010                      2040
TEST                        Jan 2008            1090                      2090
TEST2                       Nov 2007           3100                      5000
TEST2                       Dec 2007           3510                      5140
TEST2                       Jan 2008            3090                      5590

What's the script to show the data in the following format:
Name                        Nov 2007                        Dec 2007                         Jan 2008
TEST                        Count1, Count2                Count1, Count2               Count1, Count2
TEST2                      Count1, Count2                Count1, Count2               Count1, Count2

Thanks.
sandya_116Asked:
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.

sandya_116Author Commented:
Sorry I need the data as follows:
Name                        Nov 2007                        Dec 2007                         Jan 2008
TEST                        Count1 = Value               Count1= Value                Count1= Value
TEST                        Count2 = Value               Count2= Value                Count2= Value
TEST2                      Count1= Value                Count1= Value                Count1= Value
TEST2                      Count2 = Value               Count2= Value                Count2= Value
0
brejkCommented:
This may look a little bit crazy but may work :-) If you have multiple values in Dat column and you do not know them all / you do not want to write them all into the query, use the dynamic pivot (I gave some example here on EE last week).
SELECT 
  Name,
  'Count1 = ' + 
  CAST(SUM(CASE WHEN Date = 'Nov 2007' THEN Count1 ELSE 0 END) AS varchar(100)) AS 'Nov 2007',
  'Count1 = ' + 
  CAST(SUM(CASE WHEN Date = 'Dec 2007' THEN Count1 ELSE 0 END) AS varchar(100)) AS 'Dec 2007',
  'Count1 = ' + 
  CAST(SUM(CASE WHEN Date = 'Jan 2008' THEN Count1 ELSE 0 END) AS varchar(100)) AS 'Jan 2008'
FROM Table1
UNION ALL
SELECT 
  Name,
  'Count2 = ' + CAST(SUM(CASE WHEN Date = 'Nov 2007' THEN Count2 ELSE 0 END) AS varchar(100)), 
  'Count2 = ' + CAST(SUM(CASE WHEN Date = 'Dec 2007' THEN Count2 ELSE 0 END) AS varchar(100)),
  'Count2 = ' + CAST(SUM(CASE WHEN Date = 'Jan 2008' THEN Count2 ELSE 0 END) AS varchar(100))
FROM Table1
ORDER BY Name, 'Nov 2007'

Open in new window

0
brejkCommented:
Oh, sorry I think I've made a mistake in my script. Replace zeros with NULL value in CASE statements (after ELSE keywords) to have the correct results.
0
brejkCommented:
Sorry once again. Forget about the NULLs. I had a very similar sample with COUNT aggregate function. Leave zeros intact.
0
brejkCommented:
Ok, I should be more careful in writing scripts from my memories. I forgot GROUP BY clauses... See the snippet.
SELECT 
  Name,
  'Count1 = ' + 
  CAST(SUM(CASE WHEN Date = 'Nov 2007' THEN Count1 ELSE 0 END) AS varchar(100)) AS 'Nov 2007',
  'Count1 = ' + 
  CAST(SUM(CASE WHEN Date = 'Dec 2007' THEN Count1 ELSE 0 END) AS varchar(100)) AS 'Dec 2007',
  'Count1 = ' + 
  CAST(SUM(CASE WHEN Date = 'Jan 2008' THEN Count1 ELSE 0 END) AS varchar(100)) AS 'Jan 2008'
FROM Table1
GROUP BY Name
UNION ALL
SELECT 
  Name,
  'Count2 = ' + CAST(SUM(CASE WHEN Date = 'Nov 2007' THEN Count2 ELSE 0 END) AS varchar(100)), 
  'Count2 = ' + CAST(SUM(CASE WHEN Date = 'Dec 2007' THEN Count2 ELSE 0 END) AS varchar(100)),
  'Count2 = ' + CAST(SUM(CASE WHEN Date = 'Jan 2008' THEN Count2 ELSE 0 END) AS varchar(100))
FROM Table1
GROUP BY Name
ORDER BY Name, 'Nov 2007'

Open in new window

0

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
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 2005

From novice to tech pro — start learning today.