What seems like a simple problem has turned out to be quite a
challenge.
Let's say I have a table of data that represents various jobs
performed. The columns in this table are job ID, Start Date/Time, and
End Date/Time.
Now I would like to calculate the amount of work hours spent on each
job, given that work schedule is, let's say, Mon-Fri, 9-5. Job
durations can be as short as 5 minutes, or as long as many months.
This needs to perform well on large tables, so if possible I'd like to
avoid cursors and stored procedures. It also needs to be portable, so
no esoteric SQL extentions like Java.
Any ideas are highly appreciated.
Leo
Robert Stearns - 27 Aug 2004 21:01 GMT
As a first step, consider a calendar table with the date as datex and
startwork/stopwork times each day. Then something along the lines of
select jobid, case when startdate=enddate then endtime-starttime
else (select stopwork - starttime from calendar
where datex=startdate) +
(select sum(endwork-startwork) from calendar
where datex>startdate and datex<enddate) +
(select (endtime-startwork) from calendar
where datex=enddate) as total_time from jobs;
Since I'm new at this you had better check it all very carefully; even
better go to an expert like Joe Celko, who sometimes answers questions
like these, but is unforgiving of mere mortals who fail to precisely
specify both the problem and ddl for its components.
> What seems like a simple problem has turned out to be quite a
> challenge.
[quoted text clipped - 14 lines]
>
> Leo
AK - 28 Aug 2004 00:16 GMT
> What seems like a simple problem has turned out to be quite a
> challenge.
[quoted text clipped - 14 lines]
>
> Leo
could you use a calendar table? If yes, it would be very easy
lev - 30 Aug 2004 17:20 GMT
Hello,
Calendar table will work. Thanks so much for your help.
Leo.