Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have problem in fetching value in the table as like below.

Table : TestTable

SQL
ID            Name             Dept              ExpiryDate
1            Rajesh              IT                 23-Jan-16 
2            Keerthi             CSE                23-Jan-16
3            Rajesh              Mech               24-Jan-16
4            Venkat              IT                 28-Jan-16
5            VenKat              IT                 29-Jan-16
6            Priya               CSE                30-Jan-16 


Output :

SQL
Name           IT               CSE                 MECH

Rajesh        23-Jan-16                          24-Jan-16

Keerthi                      23-Jan-16

Venkat        29-Jan-16     

Priya         30-Jan-16


I need Name into Row and Dept into Columns. ExpiryDate as values.


Thanks,
Suresh
Posted
Updated 2-Feb-16 4:56am

1 solution

Try:
SQL
SELECT *
FROM (SELECT Name, Dept, ExpiryDate FROM TestTable) a
PIVOT (
   MAX(ExpiryDate) 
   FOR Dept in ([IT], [CSE],[Mech]) 
) AS data
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900