How to return the following result on single select query.
Table
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
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
Try Below Pivot Query. Simply replace table name '#temp' to Your 'table name'
select distinct TicketNo,Tom as AssignedToTom,Jerry as AssignedToJerry
from
(
select distinct TicketNo,CreateDate,Assigned
from #temp
) src
pivot
(
max(CreateDate)
for Assigned in (Tom, Jerry)
) piv
select ticketno,
MAX(case when Assigned='Tom'
then createddate else null END) as AssignedToTom,
MAX(case when Assigned='Jerry'
then createddate else null END) as AssignedToJerry
from yourtab
group by ticketno
Select
TicketNo
,(Select CreateDate from TicketDtl where TicketNo= A.TicketNo and Assigned='Tom')as AssignedToTom
,(Select CreateDate from TicketDtl where TicketNo= A.TicketNo and Assigned='Jerry') as AssignedToJerry
From TicketDtl as A
Group by TicketNo
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.