SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Display rows into columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sqlpal2007
Posting Yak Master

184 Posts

Posted - 11/27/2012 :  10:34:39  Show Profile  Reply with Quote
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

United Kingdom
39 Posts

Posted - 11/27/2012 :  11:32:54  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000