Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

How to Calculate Total Monthly time worked Expand / Collapse
Author
Message
Posted Wednesday, December 8, 2010 10:12 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 1:27 PM
Points: 42, Visits: 107
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
Post #1032011
Posted Wednesday, December 8, 2010 11:12 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Saturday, October 18, 2014 4:43 AM
Points: 5,401, Visits: 7,512
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
Post #1032056
Posted Wednesday, December 8, 2010 11:40 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 1:27 PM
Points: 42, Visits: 107
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
Post #1032075
Posted Wednesday, December 8, 2010 12:18 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Saturday, October 18, 2014 4:43 AM
Points: 5,401, Visits: 7,512
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
Post #1032106
Posted Wednesday, December 8, 2010 1:01 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 1:27 PM
Points: 42, Visits: 107
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?

Post #1032129
Posted Wednesday, December 8, 2010 2:24 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Saturday, October 18, 2014 4:43 AM
Points: 5,401, Visits: 7,512
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
Post #1032160
Posted Wednesday, December 8, 2010 2:29 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 1:27 PM
Points: 42, Visits: 107
Thank You! You saved me a ton of time.
Post #1032164
Posted Wednesday, December 8, 2010 2:34 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Saturday, October 18, 2014 4:43 AM
Points: 5,401, Visits: 7,512
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
Post #1032169
Posted Thursday, December 9, 2010 8:24 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, October 17, 2014 10:23 AM
Points: 156, Visits: 599
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
Post #1032816
Posted Thursday, December 9, 2010 9:17 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, October 17, 2014 7:46 AM
Points: 5,364, Visits: 8,948
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
Post #1032824
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse