Solved

SQL Query show date in and date out on table

Posted on 2015-01-29
8
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 50

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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Convert rows to columns 5 34
What is GIS method of Geometry data type? 6 36
SQL Server - Multiple Conditions on Left or RIGHT Joins 2 30
sql server string_split 4 28
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

739 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