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
 General SQL Server Forums
 New to SQL Server Programming
 How to query this complex query, rows to columns?

Author  Topic 

TomHand
Starting Member

8 Posts

Posted - 2012-11-15 : 01:07:07
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

52326 Posts

Posted - 2012-11-15 : 05:27:55
[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
   

- Advertisement -