SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to Calculate Total Monthly time worked


How to Calculate Total Monthly time worked

Author
Message
KS-321165
KS-321165
SSC-Enthusiastic
SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)

Group: General Forum Members
Points: 110 Visits: 110
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
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9039 Visits: 7660
Any chance you can post the sample data and DDL like in the first link of my sig? Will make life much easier.


- 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
KS-321165
KS-321165
SSC-Enthusiastic
SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)

Group: General Forum Members
Points: 110 Visits: 110
:-)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
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9039 Visits: 7660
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
KS-321165
KS-321165
SSC-Enthusiastic
SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)

Group: General Forum Members
Points: 110 Visits: 110
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?:-)
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9039 Visits: 7660
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
KS-321165
KS-321165
SSC-Enthusiastic
SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)SSC-Enthusiastic (110 reputation)

Group: General Forum Members
Points: 110 Visits: 110
Thank You! You saved me a ton of time.
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9039 Visits: 7660
KS-321165 (12/8/2010)
Thank You! You saved me a ton of time.


You're welcome. If there's any bits and pieces of that you don't understand come back and ask. You're going to have to maintain that, so you really need to understand all the things I did against your dates and times to make them work.


- 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
autoexcrement
autoexcrement
SSC-Addicted
SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)

Group: General Forum Members
Points: 406 Visits: 777
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
WayneS
WayneS
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10324 Visits: 10575
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
Author - SQL Server T-SQL Recipes
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

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search