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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to query this complex query, rows to columns?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

TomHand
Starting Member

Australia
8 Posts

Posted - 11/15/2012 :  01:07:07  Show Profile  Reply with Quote
I got this table with 4 columns:
Col1 Col2 Col3 Col4
T A a1 6
T B a1 7
T A b1 5
T B b1 8

How to make a query so that when user search for 'T', the query will show the result like this:
T a1 b1
A 6 5
B 7 8

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 11/15/2012 :  05:27:55  Show Profile  Reply with Quote
[code]
SELECT Col2 AS [T],[a1],[b1]
FROM (SELECT Col2,Col3,Col4
FROM Table
WHERE Col1 = @YourValue
)t
PIVOT (MAX(Col4) FOR Col3 IN ([a1],[b1]))p
[code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next 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.02 seconds. Powered By: Snitz Forums 2000