Solved

SQL Query show date in and date out on table

Posted on 2015-01-29
8
196 Views
Last Modified: 2015-01-30
Hi I have a table at the moment as follows

Name             Date
john                01-01-2000 09:00
john                01-01-2000 11:00
john                01-01-2000 11:15
john                01-01-2000 13:00
john                01-01-2000 13:30
john                01-01-2000 17:30

What I want to do is convert this to
Name             In                                     Out                                Time In
john                01-01-2000 09:00         01-01-2000 11:00        01:30
john                01-01-2000 11:15         01-01-2000 13:00        01:45
john                01-01-2000 13:30         01-01-2000 17:30        04:00

There are loads of people and they can lots of in and outs per day, please advise on how I could do this in SQL
0
Comment
Question by:taz8020
8 Comments
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 40577435
How do you know if is coming in or out? Don't exists a column with that information?
0
 
LVL 1

Expert Comment

by:echobridge
ID: 40577506
Having been down this road before, I'd might suggest restructuring your table if at all possible to record this information in the in/out format you're looking for. Makes it a lot easier when you get issues like missing information. In my situation it was people logging into a time clock, so missing information was common.
0
 
LVL 3

Author Comment

by:taz8020
ID: 40577669
No but the first record of the day is always in. Can it not have a query that numbers the logs per user and on a daily basis then odds would be in and evens would be out?
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40577672
SELECT Name, MIN(Date) AS [In], MAX(Date) AS [Out],
    LEFT(CONVERT(varchar(10), DATEADD(MINUTE, DATEDIFF(MINUTE, MIN(DATE), MAX(Date)), 0), 8), 5) AS [Time In]
FROM (
    SELECT Name, Date,
        ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Date) - 1 AS row_num
    FROM table_name
) AS derived
GROUP BY Name, row_num / 2
0
 
LVL 3

Author Comment

by:taz8020
ID: 40577954
Scott that is great, is there a way to start it each day. Just the way it is, if there is a duplicate clock in one day the following day it look like they have been in since they clocked out. hope you know what I mean
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40578010
Yeah, assuming you don't have shifts that cross days :-) :
...
ROW_NUMBER() OVER(PARTITION BY Name, DATEADD(DAY, DATEDIFF(DAY, 0, Date), 0) ORDER BY Date) - 1 AS row_num
...
0
 
LVL 3

Author Comment

by:taz8020
ID: 40579297
It now starts each day per user with a 0 but when I put it together it selects a different day for the out Max date. Is there something I am doing wrong?
SELECT Name, MIN(Date) AS [In], MAX(Date) AS [Out],
     LEFT(CONVERT(varchar(10), DATEADD(MINUTE, DATEDIFF(MINUTE, MIN(DATE), MAX(Date)), 0), 8), 5) AS [Time In]
 FROM (
     SELECT Name, Date,
         ROW_NUMBER() OVER(PARTITION BY Name, DATEADD(DAY, DATEDIFF(DAY, 0, Date), 0) ORDER BY Date) - 1 AS row_num
     FROM table_name
 ) AS derived
 GROUP BY Name, row_num / 2 

Open in new window

0
 
LVL 3

Author Closing Comment

by:taz8020
ID: 40579299
Sorry being dull, got it thank you so much
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question