Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CSS
         Date                       Event  Time

2014-11-22 00:00:00.000        2014-11-22 12:14:06.000
2014-11-22 00:00:00.000        2014-11-22 14:15:48.000
2014-11-22 00:00:00.000        2014-11-22 14:19:49.000
2014-11-22 00:00:00.000        2014-11-22 14:22:44.000


In event time column i want to separate first time and last time against single date and display first time as IN Time column and last time as OUT Time column and then calculate time and display total time, how can i do this
Posted

1 solution

Tested in SQL 2008 R2 and worked fine. Change the table and columns matching to yours. You could use CTE to make it better if you suffer from performance.

SQL
select distinct cast(eventdate as date) ,
   MIN(eventdate) OVER(PARTITION BY (cast(eventdate as date))) as intime,
   MAX(eventdate) OVER(PARTITION BY (cast(eventdate as date))) as outtime,
   cast((MAX(eventdate) OVER(PARTITION BY (cast(eventdate as date))) -  MIN(eventdate) OVER(PARTITION BY (cast(eventdate as date)))) as time) as totaltime

from eventdates
 
Share this answer
 
v2
Comments
John C Rayan 19-Mar-15 6:04am    
To make it better you could use CTE.

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