Reputation: 151
I have following a table called LOPList:
I want results to be :
LOPID | EMP ID | TIME START (LOPStatus = 'Y') | TIME END (LOPStatus = 'N')
2 | 6 | 2016-03-24T20:05:27+00:00 | 2016-03-24T20:14:41+00:00
5 | 6 | 2016-03-24T20:07:59+00:00 | 2016-03-24T20:13:11+00:00
Upvotes: 0
Views: 124
Reputation: 127
SELECT list.LOPID,list.EmpID as "EMP ID", listY.TimeUpdated as "TIME START (LOPStatus = 'Y')",listN.TimeUpdated as "TIME END (LOPStatus = 'N')" from (SELECT DISTINCT LOPID,EmpID FROM [TestDB].[dbo].[LOPList]) AS list , (Select LOPID,LOPStatus,TimeUpdated from [TestDB].[dbo].[LOPList] where LOPStatus = 'Y') listY, (Select LOPID,LOPStatus,TimeUpdated from [TestDB].[dbo].[LOPList] where LOPStatus = 'N') listN WHERE listY.LOPID = list.LOPID and listN.LOPID = list.LOPID;
You should know some sql language besic knowledge,here you should use 'DISTINCT ' statement, thanks, hope can take you some help
Upvotes: 0
Reputation: 4751
Try:
select LOPID,EmpId,
max(case when LOPStatus='Y' then TimeUpdated end) as time_start,
max(case when LOPStatus='N' then TimeUpdated end) as time_end
from LOPList where JobID= 22 group by LOPID;
Upvotes: 1