Solved

how to compute for the downtime hours based on business days/hours

Posted on 2006-07-12
10
513 Views
Last Modified: 2012-06-27
hi experts,

can anyone suggest a logic that i can use for the following specs:

downtime: 06-15-2006 08:00:00
fixed time: 07-25-2006 15:23:00
business days: monday - friday
business hours: 08:00:00 - 17:00:00

how do i get the downtime hours for the month of july taking into consideration the business days and business hours. so weekends and after office hours will not be included in the computation.

i would really really appreciate any ideas/thoughts on this one.  i am giving all my available points. =)

regards and thanks in advance.

ann

0
Comment
Question by:wala_lang
  • 5
  • 4
10 Comments
 
LVL 25

Expert Comment

by:jrb1
ID: 17088479
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 17091635
First, create a table of sequential numbers from 0 to the max number of days difference between downtime and fixedtime (1100 days = ~ 3 yrs).  [Btw, that table is *remarkably* useful for many things in SQL.]  For example:

SET NOCOUNT ON
CREATE TABLE seqNums (
    seqNum SMALLINT,
    CONSTRAINT seqNums_CI  --this is important for best performace!
        UNIQUE CLUSTERED (seqNum)
    )
DECLARE @seqNum SMALLINT
SET @seqNum = 0
WHILE @seqNum <= 1100
BEGIN
    INSERT INTO seqNums VALUES(@seqNum)
    SET @seqNum = @seqNum + 1
END --WHILE
SET NOCOUNT OFF
SELECT COUNT(*) FROM seqNums


Then, this code should produce a total for any dates specified [NOTE: sample inline data is enclosed; naturally change that to be whatever table or other source of data you need :-)] :


SELECT CAST(SUM(
    CASE seqNum
    WHEN 0 THEN
        --first day
        CASE WHEN DATENAME(WEEKDAY, downtime) IN ('Saturday', 'Sunday') OR DATEPART(HOUR, downtime) >= 17
        THEN 0 ELSE DATEDIFF(MINUTE, CONVERT(CHAR(5), downtime, 8), '17:00') END
    WHEN DATEDIFF(DAY, downtime, fixedtime) THEN
        --last day
        CASE WHEN DATEDIFF(DAY, downtime, fixedtime) = 0 OR DATEPART(HOUR, fixedtime) < 8 OR
            DATENAME(WEEKDAY, fixedtime) IN ('Saturday', 'Sunday') THEN 0 ELSE
            DATEDIFF(MINUTE, '08:00', CASE WHEN DATEPART(HOUR, fixedtime) > 17 THEN '17:00'
                ELSE CONVERT(CHAR(5), fixedtime, 8) END) END
    ELSE
        --middle day(s)
        CASE WHEN DATENAME(WEEKDAY, DATEADD(DAY, seqNum, downtime)) IN ('Saturday', 'Sunday')
            THEN 0 ELSE 540 END
    END) / 60.0 AS DECIMAL(6, 1))
FROM (
    SELECT CAST('20060615 08:00:00' AS DATETIME) AS downtime,
        CAST('20060725 15:23:00' AS DATETIME) AS fixedtime
) AS dates
INNER JOIN seqNums ON seqNum <= DATEDIFF(DAY, downtime, fixedtime)
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 17091655
D'OH, not working correctly, will continue to work on it :-) .
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 235 total points
ID: 17091761
CORRECTION: Needed to adjust the first day if the downtime started before 08:00.

SELECT CAST(SUM(   --comment out to see individual day totals
    CASE seqNum
    WHEN 0 THEN
        --first day
        CASE WHEN DATENAME(WEEKDAY, downtime) IN ('Saturday', 'Sunday') OR DATEPART(HOUR, downtime) >= 17
        THEN 0 ELSE DATEDIFF(MINUTE, CASE WHEN DATEPART(HOUR, downtime) < 8 THEN '08:00' ELSE CONVERT(CHAR(5), downtime, 8) END, '17:00') END
    WHEN DATEDIFF(DAY, downtime, fixedtime) THEN
        --last day
        CASE WHEN DATEDIFF(DAY, downtime, fixedtime) = 0 OR DATEPART(HOUR, fixedtime) < 8 OR
            DATENAME(WEEKDAY, fixedtime) IN ('Saturday', 'Sunday') THEN 0 ELSE
            DATEDIFF(MINUTE, '08:00', CASE WHEN DATEPART(HOUR, fixedtime) > 17 THEN '17:00'
                ELSE CONVERT(CHAR(5), fixedtime, 8) END) END
    ELSE
        --middle day(s)
        CASE WHEN DATENAME(WEEKDAY, DATEADD(DAY, seqNum, downtime)) IN ('Saturday', 'Sunday')
            THEN 0 ELSE 540 END
    END
    ) / 60.0 AS DECIMAL(6, 1))   --comment out to see individual day totals
FROM (
    --SELECT CAST('20060707' AS DATETIME) AS downtime,
        --GETDATE() AS fixedtime
    SELECT CAST('20060615 08:00:00' AS DATETIME) AS downtime,
        CAST('20060725 15:23:00' AS DATETIME) AS fixedtime
) AS dates
INNER JOIN seqNums ON seqNum <= DATEDIFF(DAY, downtime, fixedtime)
--ORDER BY seqNum  --uncomment to see individual day totals


The (seemingly) odd formatting is to allow easy commenting/uncommenting so that you can easily see, and verify, the minutes computed for each day.
Ditto with the input date values -- just adjust the comments to use different test data.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 17091906
Finally (I promise, no more unprompted posts!), if you want to process multiple date ranges at the same time, while computing each separately, you can do this:


SELECT CONVERT(VARCHAR(16), downtime, 120) AS [Downtime],
    CONVERT(VARCHAR(16), fixedtime, 120) AS [Fixed Time],
    CAST(SUM(
    CASE seqNum
    WHEN 0 THEN
        --first day
        CASE WHEN DATENAME(WEEKDAY, downtime) IN ('Saturday', 'Sunday') OR DATEPART(HOUR, downtime) >= 17
        THEN 0 ELSE DATEDIFF(MINUTE, CASE WHEN DATEPART(HOUR, downtime) < 8 THEN '08:00' ELSE CONVERT(CHAR(5), downtime, 8) END, '17:00') END
    WHEN DATEDIFF(DAY, downtime, fixedtime) THEN
        --last day
        CASE WHEN DATEDIFF(DAY, downtime, fixedtime) = 0 OR DATEPART(HOUR, fixedtime) < 8 OR
            DATENAME(WEEKDAY, fixedtime) IN ('Saturday', 'Sunday') THEN 0 ELSE
            DATEDIFF(MINUTE, '08:00', CASE WHEN DATEPART(HOUR, fixedtime) > 17 THEN '17:00'
                ELSE CONVERT(CHAR(5), fixedtime, 8) END) END
    ELSE
        --middle day(s)
        CASE WHEN DATENAME(WEEKDAY, DATEADD(DAY, seqNum, downtime)) IN ('Saturday', 'Sunday')
            THEN 0 ELSE 540 END
    END
    ) / 60.0 AS DECIMAL(6, 1))
    AS [Total Down Hours]
FROM (
    SELECT CAST('20060707' AS DATETIME) AS downtime,
        GETDATE() AS fixedtime
    UNION ALL
    SELECT CAST('20060615 08:00:00' AS DATETIME) AS downtime,
        CAST('20060725 15:23:00' AS DATETIME) AS fixedtime
) AS dates
INNER JOIN seqNums ON seqNum <= DATEDIFF(DAY, downtime, fixedtime)
GROUP BY downtime, fixedtime
ORDER BY 1
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:wala_lang
ID: 17095344
thanks for all the posts guys.  i will give them all a try today.
0
 

Author Comment

by:wala_lang
ID: 17095750
hi experts,

i will award the points to scott.  it make much more sense to me.  thanks a lot for your time guys.  i really really appreciate it.

thanks scott. ur the man!

ann
0
 

Author Comment

by:wala_lang
ID: 17095793
hi scott,

oopppss....i forgot.  noticed that my sample data are:

downtime: 06-15-2006 08:00:00
fixed time: 07-25-2006 15:23:00

and i only want to get the downtime for the month of july.  so using the data above, it should only give me the downtime from jul 1 up to jul 25 15:23:00.

would you mind including this piece of logic on your code.  in the meantime, i'll try to see if i can revise your code here at my end.  =)

thanks heaps.

ann
0
 

Author Comment

by:wala_lang
ID: 17097186
hi scott,

i fixed it.  =) thanks heaps again.

ann
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 17108077
Glad you got what you needed Hi wala_lang!

Sorry, I was not feeling well yesterday and so did not read and/or post at all.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL JOIN 6 39
BULK INSERT most recent CSV 19 21
Test a query 23 19
Determine which columns have the exact value 1 24
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now