Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Column to Row - UNPIVOT HELP!

Author  Topic 

OKsoccer24
Starting Member

8 Posts

Posted - 2008-08-13 : 11:23:53
I have a table that is

XX XX XX XX XX XX XX

and I need it to be

XX
XX
XX
XX

Does anyone know how this is possible using SQL?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-13 : 11:27:23
UNPIVOT.

Read Books Online for more information.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

OKsoccer24
Starting Member

8 Posts

Posted - 2008-08-13 : 14:11:47
OK I have research and find my situation from most unpivot examples.

select sum(cnt_200806) as "06/08",
sum(cnt_200805) as "05/08",
sum(cnt_200804) as "04/08",
sum(cnt_200803) as "03/08",
sum(cnt_200802) as "02/08",
sum(cnt_200801) as "01/08",
sum(cnt_200712) as "12/07",
sum(cnt_200711) as "11/07",
sum(cnt_200710) as "10/07",
sum(cnt_200709) as "09/07",
sum(cnt_200708) as "08/07",
sum(cnt_200707) as "07/07"
from tbl_metrics_users


Here is my sql that post 1 row with multiple columns.. Obviously
Can some please type the sql required the will put that in one Column.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-13 : 14:15:26
it will be like this
SELECT *
FROM
(
SELECT *
FROM YourTable)m
UNPIVOT (Col FOR Values IN ([06/08],[05/08],...))p
Go to Top of Page

OKsoccer24
Starting Member

8 Posts

Posted - 2008-08-13 : 14:29:06
I need to rephrase myself. The first post shall say that I have results from a table that equal the first post. The table tbl_metrics_users is a huge table each column having numbers that I need summed and posted in a column. The sql stmt I posted takes all the data and puts it into one row and I need one column.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-13 : 14:31:59
Try putting your column headers names in unpivot query and see
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-13 : 14:36:15
Then post some proper sample data! And table layout.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-08-13 : 23:33:38
Have you take a look at this ?
http://msdn.microsoft.com/en-us/library/ms177410.aspx?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -