Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / General DB Topics / General DB Topics / August 2004

Tip: Looking for answers? Try searching our database.

Query design puzzle

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lev - 27 Aug 2004 18:25 GMT
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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.