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)
 Display rows into columns

Author  Topic 

sqlpal2007
Posting Yak Master

200 Posts

Posted - 2012-11-27 : 10:34:39
Hello All,

Below result set I need to display in rows as columns.
The result is coming from 3 different replciation servers and I need to compare the record counts between them.

Server Database Table Rows
Srv1 DB1 Tbl1 2000
Srv1 DB1 Tbl2 1000
Srv1 DB2 Tbl1 100
Srv2 DB1 Tbl1 2000
Srv2 DB1 Tbl2 1000
Srv2 DB2 Tbl1 100
Srv3 DB1 Tbl1 2000
Srv3 DB1 Tbl2 1000
Srv3 DB2 Tbl1 100

Result should look like this
Database Table Server1 Rows Server2 Rows Server3 Rows
DB1 Tbl1 Srv1 2000 Srv2 2000 Srv3 2000
DB1 Tbl2 Srv1 1000 Srv2 1000 Srv3 1000
DB2 Tbl1 Srv1 100 Srv2 100 Srv3 100

Can anyone please help with the query?

Thanks,
-Pal

Elizabeth B. Darcy
Starting Member

39 Posts

Posted - 2012-11-27 : 11:32:54
If you do not need the (in my view the superfluous - it does not add any additional information) Server1,Server2,Server3 columns, you can easily use the PIVOT operator like so
SELECT
*
FROM
tbl
PIVOT
( MAX(ROWS) FOR [Server] IN ([Srv1],[Srv2],[Srv3]) )P



________________________________________
-- Yes, I am indeed a fictional character.
Go to Top of Page
   

- Advertisement -