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 2000 Forums
 SQL Server Development (2000)
 Values as columns

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-10-29 : 08:30:48
Adam writes "Let us say that i have made a query with joins. The result from this query looks somthing like this:


Value column NameColumn
------------ -------------
1 A
2 A
3 A
4 A
5 A
6 B
7 B
8 B
9 B
10 B
. .
. .
. .

I would like to display this data "turned 90 degrees" where the name columns groups (A,B,...) are columns in a table and the value columns data are the rows, like this:


A B .
------ ------- ------
1 6 .
2 7 .
3 8 .
4 9 .
5 10 .




Is there a way to do this in one sql statement?


I would appreciate any help in this matter.

Regards

Adam Bervenstahl"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-10-29 : 08:31:13
Cross tabs to the rescue!

http://www.sqlteam.com/SearchResults.asp?SearchTerms=cross+tab
Go to Top of Page

MakeYourDaddyProud

184 Posts

Posted - 2003-10-29 : 08:43:14
In order to do that, you are transforming elements of metadata (column names) from data in a table. Some kind of dynamically built SQL string using EXEC(string) would be needed. Nasty!

Anyway, In the transform, what would you expect the display to look like if, say B did not have '10' as a value? Would there be a null?

Furthermore, how did your design process allow this to happen?



Daniel Small MIAP
www.danielsmall.com IT Factoring
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-10-29 : 21:52:44
Not nasty at all, this does it all rather nicely:

http://www.sqlteam.com/item.asp?ItemID=2955
Go to Top of Page
   

- Advertisement -