Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have table in this format

code Bscid st_time status
101 100010 01-June-2014 11:00 start
101 100010 01-June-2014 11:02 end
101 100010 01-June-2014 11:04 start
101 100010 01-June-2014 11:06 end
101 100010 01-June-2014 11:08 start


Out Put format


code Bscid st_time end_time status
101 100010 01-June-2014 11:00 01-June-2014 11:02 end
101 100010 01-June-2014 11:04 01-June-2014 11:06 end
101 100010 01-June-2014 11:08 01-June-2014 11:08 start
Posted

1 solution

SQL
SELECT 
	Code,Bscid, max(st_time) st_time,isnull(max(edtime),max(st_time)) as ed_time,min([status]) [status]
FROM
(
	SELECT row_number() OVER(ORDER by Code,Bscid, st_time) as srno, Code,Bscid, st_time,NULL as edtime,[status] FROM A
	WHERE [status]='start'
	
	UNION 
	
	SELECT row_number() OVER(ORDER by Code,Bscid, st_time) as srno,Code,Bscid, NULL st_time,st_time as edtime,[status] FROM A
	WHERE [status]='end'
)
as temp
group by srno,Code,Bscid

Happy Coding!
:)
 
Share this answer
 

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