Author |
Topic |
Noejfah
Starting Member
5 Posts |
Posted - 2013-05-28 : 03:25:45
|
Hi Can anyone help I need code o transpose in a loop. THis is what the data looks like:acc_key vin mth123 0 201205456 0 201205456 0 201206123 0 201206456 0 201207123 NULL 201207789 0 201208456 0 201208123 NULL 201208123 0 201209456 1 201209789 0 201209456 0 201210789 1 201210123 0 201210456 0 201211123 0 201211789 0 201211456 0 201212789 1 201212123 0 201212123 0 201301456 1 201301456 0 201302123 0 201302123 0 201303456 0 201303456 0 201304123 0 201304789 0 201304/******************/And this is the output I need:acc_key 201205 201206 201207 201208 201209123 0 0 0 0456 0 0 NULL 0 1789 0 0 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-28 : 04:00:57
|
[code]DECLARE @mthlist varchar(2000)SET @mthlist = STUFF((SELECT DISTINCT ',[' + CAST(mth AS varchar(6)) + ']' FROM table ORDER BY '[' + CAST(mth AS varchar(6)) FOR XML PATH('')),1,1,'')SET @SQL='SELECT *FROM (SELECT acc_key, COALESCE(vin,0) AS vin, mth FROM table)tPIVOT (SUM(vin) FOR mth IN (' + @mthlist + '))p'--PRINT (@SQL)EXEC (@SQL)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Noejfah
Starting Member
5 Posts |
Posted - 2013-05-28 : 04:30:55
|
Hi Visakh16 I get the following error when I run the code:ORDER BY items must appear in the select list if SELECT DISTINCT is specified |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-28 : 05:04:46
|
that was a typoDECLARE @mthlist varchar(2000)SET @mthlist = STUFF((SELECT DISTINCT ',[' + CAST(mth AS varchar(6)) + ']' FROM table ORDER BY ',[' + CAST(mth AS varchar(6)) ']' FOR XML PATH('')),1,1,'')SET @SQL='SELECT *FROM (SELECT acc_key, COALESCE(vin,0) AS vin, mth FROM table)tPIVOT (SUM(vin) FOR mth IN (' + @mthlist + '))p'--PRINT (@SQL)EXEC (@SQL) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-05-28 : 05:05:17
|
SET @mthlist = STUFF((SELECT ',[' + CAST(mth AS varchar(6)) + ']' FROM (SELECT DISTINCT mth FROM table)T1 ORDER BY '[' + CAST(mth AS varchar(6)) FOR XML PATH('')),1,1,'')--Chandu |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-05-28 : 05:18:23
|
Small typo fixedquote: Originally posted by visakh16 that was a typoDECLARE @mthlist varchar(2000)SET @mthlist = STUFF((SELECT DISTINCT ',[' + CAST(mth AS varchar(6)) + ']' FROM table ORDER BY ',[' + CAST(mth AS varchar(6)) +']' FOR XML PATH('')),1,1,'')SET @SQL='SELECT *FROM (SELECT acc_key, COALESCE(vin,0) AS vin, mth FROM table)tPIVOT (SUM(vin) FOR mth IN (' + @mthlist + '))p'--PRINT (@SQL)EXEC (@SQL) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
--Chandu |
|
|
Noejfah
Starting Member
5 Posts |
Posted - 2013-05-28 : 05:26:11
|
Thank you Visakh it works. I had to tweek it a little bit BUt it works now THANKS YOU!!! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-28 : 06:02:01
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|