Winston Madiano Winston Madiano - 1 year ago 76
SQL Question

How To Convert Rows Into Column In Single Select Query SQL Server 2008 R2

How to return the following result on single select query.


Rn TicketNo CreateDate Assigned
1 xxx 2016-11-17 03:45:46.000 Tom
2 xxx 2016-11-18 03:45:46.000 Jerry
1 yyy 2016-11-17 03:45:46.000 Tom
2 yyy 2016-11-18 03:45:46.000 Jerry

Expected Result

TicketNo AssignedToTom AssignedToJerry
xxx 2016-11-17 03:45:46.000 2016-11-18 03:45:46.000
yyy 2016-11-17 03:45:46.000 2016-11-18 03:45:46.000

Many thanks in advance

Answer Source

Try This: replace with name of your table

select a.ticketno
        ,a.CreateDate as [AssignedToTom]
        ,b.CreateDate as [AssignedToJerry]
         from <tablename> a join <tablename> b 
         on a.assigned='Tom' and b.assigned='Jerry' 
         and a.ticketno=b.ticketno
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download