Solved

SQL Query show date in and date out on table

Posted on 2015-01-29
8
193 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 48

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

809 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