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


How to count active jobs at the end of each month across a period of time


How to count active jobs at the end of each month across a period of time

Author
Message
AndyAus
AndyAus
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 126
My apologies if this question has been answered elsewhere but I can’t find a solution. I need to count the number of active jobs that exist at the end of each month. A job is active if it has been received but not yet finished. The data looks like this:

CREATE TABLE #Files
(Job NVARCHAR(10), Received DATETIME, Finished DATETIME)

INSERT INTO #Files (JOB, Received, Finished)
SELECT 'A1', CAST('20120110' AS DATETIME), CAST('20120506' AS DATETIME) UNION ALL
SELECT 'A23', CAST('20120113' AS DATETIME), NULL UNION ALL
SELECT 'B4', CAST('20120221' AS DATETIME), CAST('20120224' AS DATETIME) UNION ALL
SELECT 'L5', CAST('20120307' AS DATETIME), CAST('20120425' AS DATETIME) UNION ALL
SELECT 'D5', CAST('20120316' AS DATETIME), NULL UNION ALL
SELECT 'S3', CAST('20120323' AS DATETIME), CAST('20120510' AS DATETIME) UNION ALL
SELECT 'D2', CAST('20120502' AS DATETIME), CAST('20120607' AS DATETIME)

SELECT * FROM #Files ORDER BY 2, 3
DROP TABLE #Files

So, from the data above, I need to calculate the following:

Date Active jobs
20120131 2
20120229 2
20120331 5
20120430 4
20120531 3
20120630 2

A job can be counted multiple times depending on when it was received and finished. Any advice would be appreciated. Thanks.
AndyAus
AndyAus
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 126
I think I may have found a solution. It's all in the joins. I created a recursive CTE to generate a framework of end-of-month dates covering the period of time required for the report. I then linked the job data back to the list of dates like this:

SELECT CTEdates.EndofMonth, COUNT(Job)
FROM CTEDates
LEFT OUTER JOIN #Files ON #Files.Received <= CTEDates.EndofMonth
AND ( #Files.Finished IS NULL OR #Files.Finished > CTEDates.EndofMonth )
GROUP BY CTEdates.EndofMonth

Does that make sense or is there a more efficient way of writing the query?
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