|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 6:09 AM
Points: 42,
Visits: 99
|
|
I need to work out the total monthly time an emplyee is working given the signon and signoff times. All I need to use is the time the employee first signed on in the morning and the last time the employee signedoff. I don't need the time inbetween.
Table is:
ResourceID (int), Site (int), DateKey(yyyymmdd), TimeKey(hh:mm:ss), EventType(int).
Event type of 2 indicates signon ,3 signoff and 4 idle
This is the sample data:
ResourceId Site DateKey TimeKey EventType 1 1 20100901 80010 2 1 1 20100901 113001 3 1 1 20100901 122014 2 1 1 20100901 153223 3 1 1 20100901 153814 2 1 1 20100901 161503 3 1 1 20100902 80010 2 1 1 20100902 113001 3 1 1 20100902 122014 2 1 1 20100902 153223 3 1 1 20100902 153814 2 1 1 20100902 161503 3 2 1 20100901 80010 2 2 1 20100901 113001 3 2 1 20100901 122014 2 2 1 20100901 153223 3 2 1 20100901 153814 2 2 1 20100901 161503 3 2 1 20100902 80010 2 2 1 20100902 113001 3 2 1 20100902 122014 2 2 1 20100902 153223 3 2 1 20100902 153814 2 2 1 20100902 161503 3
Is there a simpler way to do this without using temporary tables?
Thanks
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 8:32 PM
Points: 5,672,
Visits: 6,113
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 6:09 AM
Points: 42,
Visits: 99
|
|
Thanks. Here it is:
if (object_id('tempdb..#Astime') is not null) drop table #Astime
create table #Astime (ResourceId smallint, Site smallint, DateKey int, TimeKey int, EventType smallint)
insert into #Astime select 1, 1, 20100901, 80010,2 union select 1, 1, 20100901, 113001,3 union select 1, 1, 20100901, 122014,2 union select 1, 1, 20100901, 153223,3 union select 1, 1, 20100901, 153814,2 union select 1, 1, 20100901, 161503,3 union select 1, 1, 20100902, 80010,2 union select 1, 1, 20100902, 122014,2 union select 1, 1, 20100902, 153223,3 union select 1, 1, 20100902, 153814,2 union select 1, 1, 20100902, 161510,3 union select 2, 1, 20100901, 80015,2 union select 2, 1, 20100901, 113001,3 union select 2, 1, 20100901, 122014,2 union select 2, 1, 20100901, 153223,3 union select 2, 1, 20100901, 153814,2 union select 2, 1, 20100901, 161523,3 union select 2, 1, 20100902, 80010,2 union select 2, 1, 20100902, 122014,2 union select 2, 1, 20100902, 153223,3 union select 2, 1, 20100902, 153814,2 union select 2, 1, 20100902, 161520,3
select * from #Astime
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 8:32 PM
Points: 5,672,
Visits: 6,113
|
|
I broke this into CTE's because it's easier to see the methodology that way. You could do this all in one query if you wanted. The key is the two subqueries, which are listed as cte and cte2.
Note one of the larger issues is turning your separate date and time stamps into usable values.
/* if (object_id('tempdb..#Astime') is not null) drop table #Astime
create table #Astime (ResourceId smallint, Site smallint, DateKey int, TimeKey int, EventType smallint)
insert into #Astime select 1, 1, 20100901, 80010,2 union select 1, 1, 20100901, 113001,3 union select 1, 1, 20100901, 122014,2 union select 1, 1, 20100901, 153223,3 union select 1, 1, 20100901, 153814,2 union select 1, 1, 20100901, 161503,3 union select 1, 1, 20100902, 80010,2 union select 1, 1, 20100902, 122014,2 union select 1, 1, 20100902, 153223,3 union select 1, 1, 20100902, 153814,2 union select 1, 1, 20100902, 161510,3 union select 2, 1, 20100901, 80015,2 union select 2, 1, 20100901, 113001,3 union select 2, 1, 20100901, 122014,2 union select 2, 1, 20100901, 153223,3 union select 2, 1, 20100901, 153814,2 union select 2, 1, 20100901, 161523,3 union select 2, 1, 20100902, 80010,2 union select 2, 1, 20100902, 122014,2 union select 2, 1, 20100902, 153223,3 union select 2, 1, 20100902, 153814,2 union select 2, 1, 20100902, 161520,3
*/
;WITH cte AS (SELECT ResourceID, DateKey, MIN( TimeKey) AS StartTime FROM #Astime WHERE EventType = 2 GROUP BY ResourceID, DateKey ) ,cte2 AS (SELECT ResourceID, DateKey, MAX( TimeKey) AS EndTime FROM #Astime WHERE EventType = 3 GROUP BY ResourceID, DateKey ) ,cte3 AS (SELECT c.ResourceID, c.DateKey, c.StartTime, ISNULL( c2.EndTime, 115959) AS EndTime FROM cte AS c LEFT JOIN cte2 AS c2 On c.ResourceID = c2.ResourceID AND c.DateKey = c2.DateKey ) , cte4 AS (SELECT ResourceID, CONVERT( DateTime, CONVERT( VARCHAR(20), DateKey)) AS DateKey, DATEADD( mm, DATEDIFF( mm, 0, CONVERT( DATETIME, CONVERT( VARCHAR(20), DateKey))), 0) AS MonthStartDateKey, --REVERSE( STUFF( STUFF( REVERSE( CONVERT( VARCHAR(30), StartTime)), 3, 0, ':'), 6, 0, ':')) AS test Convert( Datetime, '1/1/1900 ' + REVERSE( STUFF( STUFF( REVERSE( CONVERT( VARCHAR(30), StartTime)), 3, 0, ':'), 6, 0, ':'))) AS StartTime, Convert( Datetime, '1/1/1900 ' + REVERSE( STUFF( STUFF( REVERSE( CONVERT( VARCHAR(30), endTime)), 3, 0, ':'), 6, 0, ':'))) AS EndTime FROM cte3 )
SELECT ResourceID, MonthStartDateKey, SUM( DATEDIFF(n, StartTime, EndTime)) AS TotalMinutesWorkedInMonth FROM cte4 GROUP BY ResourceID, MonthStartDateKey
- Craig Farrell
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions | Forum Netiquette For index/tuning help, follow these directions. |Tally Tables Twitter: @AnyWayDBA
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 6:09 AM
Points: 42,
Visits: 99
|
|
Thanks. I am learning CTE and so it was helpful when you broke it down, but how do you put all this in one query?
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 8:32 PM
Points: 5,672,
Visits: 6,113
|
|
KS-321165 (12/8/2010)
Thanks. I am learning CTE and so it was helpful when you broke it down, but how do you put all this in one query? 
SELECT drvStart.ResourceID, DATEADD( mm, DATEDIFF( mm, 0, drvStart.DateKey), 0) AS MonthStartDateKey, SUM( DATEDIFF(n, StartTime, ISNULL( EndTime, CONVERT( DATETIME, '1/1/1900 23:59:59')))) AS TotalMinutesWorkedInMonth FROM (SELECT ResourceID, CONVERT( DateTime, CONVERT( VARCHAR(20), DateKey)) AS DateKey, MIN( Convert( Datetime, '1/1/1900 ' + REVERSE( STUFF( STUFF( REVERSE( CONVERT( VARCHAR(30), TimeKey)), 3, 0, ':'), 6, 0, ':')))) AS StartTime FROM #Astime WHERE EventType = 2 GROUP BY ResourceID, DateKey ) AS drvStart LEFT JOIN (SELECT ResourceID, CONVERT( DateTime, CONVERT( VARCHAR(20), DateKey)) AS DateKey, MAX( Convert( Datetime, '1/1/1900 ' + REVERSE( STUFF( STUFF( REVERSE( CONVERT( VARCHAR(30), TimeKey)), 3, 0, ':'), 6, 0, ':')))) AS EndTime FROM #Astime WHERE EventType = 3 GROUP BY ResourceID, DateKey ) AS drvEnd ON drvStart.ResourceID = drvEnd.ResourceID AND drvStart.DateKey = drvEnd.DateKey GROUP BY drvStart.ResourceID, DATEADD( mm, DATEDIFF( mm, 0, drvStart.DateKey), 0)
- Craig Farrell
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions | Forum Netiquette For index/tuning help, follow these directions. |Tally Tables Twitter: @AnyWayDBA
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 6:09 AM
Points: 42,
Visits: 99
|
|
| Thank You! You saved me a ton of time.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 8:32 PM
Points: 5,672,
Visits: 6,113
|
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 5:23 PM
Points: 52,
Visits: 188
|
|
1. Craig, can I have an autographed photo of your brain for Chistmas?
2. This is totally cool. I'm trying to figure it all out. I think I'm doing pretty well. Couple questions/confirmations (re: Post #1032106):
CONVERT( DateTime, CONVERT( VARCHAR(20), DateKey)) AS DateKey I guess this means you can't directly convert an INT to DateTime?
DATEADD( mm, DATEDIFF( mm, 0, CONVERT( DATETIME, CONVERT( VARCHAR(20), DateKey))), 0) AS MonthStartDateKey Okay, this confuses me. I have taken it apart from the inside out, I understand what DATEADD and DATEDIFF do, but I don't understand the use of "0" here...?
And the REVERSE, STUFF, REPEAT... , as much as I'd like to pretend to understand it, I don't.
If you feel like elaborating on any of this, I'd love to learn what exactly you're doing.
"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 4:38 PM
Points: 6,368,
Visits: 8,230
|
|
autoexcrement (12/9/2010)
1. Craig, can I have an autographed photo of your brain for Chistmas? 2. This is totally cool. I'm trying to figure it all out. I think I'm doing pretty well. Couple questions/confirmations (re: Post #1032106): CONVERT( DateTime, CONVERT( VARCHAR(20), DateKey)) AS DateKey I guess this means you can't directly convert an INT to DateTime? The answer to this is "it depends" - specifically on what you're storing in that int. If you are storing a date in YYYYMMDD format, then no. If you are storing an offset from 01/01/1900, then yes.
DATEADD( mm, DATEDIFF( mm, 0, CONVERT( DATETIME, CONVERT( VARCHAR(20), DateKey))), 0) AS MonthStartDateKey Okay, this confuses me. I have taken it apart from the inside out, I understand what DATEADD and DATEDIFF do, but I don't understand the use of "0" here...? If you were to run "SELECT CONVERT(DATETIME, 0)", you will see that this is converted to 01/01/1900. What the code is doing is getting the number of month boundaries crossed between 01/01/1900 and the other date. It then adds this number of months to 01/01/1900. The end result of this is that it will always return the first of the month for the specified date.
And the REVERSE, STUFF, REPEAT... , as much as I'd like to pretend to understand it, I don't. If you feel like elaborating on any of this, I'd love to learn what exactly you're doing.
The REVERSE/STUFF/STUFF/REVERSE is taking the TimeKey field (time represented as an integer). Since leading zeros are not shown, anything before 10am will only be 5 digits instead of 6. So the intent is to add the colons between the hours/minutes and minutes/seconds. SELECT REVERSE('80101') will return 10108. We now can put those colons in the proper places. (STUFF adds the colon in the proper position.) Once finished, you have 10:10:8. You now need to reverse this again to have the valid time: 8:01:01.
Edit: you could also do STUFF(STUFF(RIGHT('0' + CONVERT(VARCHAR(6), TimeKey), 6),5,0,':'),3,0,':') . This converts the TimeKey to a string, prefixes it with a '0', takes the right 6 characters, and adds the colons.
Does this explain it all?
Wayne Microsoft Certified Master: SQL Server 2008 If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it! Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines, CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
|
|
|
|