SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
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?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

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

Very Important crosS Applying yaK Herder

52326 Posts

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

SQL Server MVP

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