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)
 SQL result to be oriented vertically

Author  Topic 

Rocko
Starting Member

26 Posts

Posted - 2007-12-27 : 15:13:16
Hello Friends,

Normally each row from the result set represents a row of a table.
If a table has 2 rows representing number of orders per day:

SystemID | OwnerID | Notes
------------------------------------------------
1 | 10 | blah1
2 | 11 | blah2
2 | 12 | blah3

I would like to orient this row horizontal like this:

SystemID | 1 | 2 | 3
-------------------------------------
OwnerID | 10 | 11 |12
Notes | blah1 | blah2 | blah3

What techniques should I use in order to achieve such a result?

The number of the systems that will display each time may vary. Something like this site: http://www.wikimatrix.org/compare/%40Wiki+MediaWiki+ScrewTurn-Wiki

Thanks in advance!
Rocco

PurpleSun
Yak Posting Veteran

50 Posts

Posted - 2007-12-27 : 16:40:47
try these:
[url]http://www.codecomments.com/message550596.html[/url]
[url]http://ryanfarley.com/blog/archive/2005/02/17/1712.aspx[/url]
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-12-27 : 22:02:56
use the PIVOT operator


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

Go to Top of Page

matty
Posting Yak Master

161 Posts

Posted - 2007-12-28 : 01:22:40
declare @sample table
(SystemID int,OwnerID int,Notes varchar(100))

insert @sample
select 1 ,10 ,'blah1'
union all
select 2 , 11 ,'blah2'
union all
select 3 , 12 ,'blah3'



SELECT 'OwnerID' AS SystemID,[1],[2],[3]
FROM (SELECT SystemID,CAST(OwnerID AS VARCHAR(100)) AS OwnerID FROM @sample) AS p
PIVOT
(
MAX(OwnerID)
FOR p.SystemID in ([1],[2],[3])
) AS pvt
UNION ALL
SELECT 'Notes' AS SystemID,[1],[2],[3]
FROM (SELECT SystemID,Notes FROM @sample) AS p1
PIVOT
(
MAX(Notes)
FOR p1.SystemID in ([1],[2],[3])
) AS pvt1
Go to Top of Page
   

- Advertisement -