From: dvestal on
Suppose you were looking at widget manufacturing data, and your DB
looked like this:

tblWidgetsProduced
dtStartTime
dtEndTime
iWidgetsProduced

Suppose you wanted to look at widgets produced per hour....but there
was no guarantee that dtStartTime and dtEndTime fell on hour
boundaries, so you might have the split an entry across multiple hours
by allocating iWidgetsProduced proportionately across the spanned
hours. Would that be possible to do in SQL?
From: Gert-Jan Strik on
"dvestal(a)gmail.com" wrote:
>
> Suppose you were looking at widget manufacturing data, and your DB
> looked like this:
>
> tblWidgetsProduced
> dtStartTime
> dtEndTime
> iWidgetsProduced
>
> Suppose you wanted to look at widgets produced per hour....but there
> was no guarantee that dtStartTime and dtEndTime fell on hour
> boundaries, so you might have the split an entry across multiple hours
> by allocating iWidgetsProduced proportionately across the spanned
> hours. Would that be possible to do in SQL?

Please tell your trainer / professor to change the training material to
use proper table and column names. It is considered a bad practice to
prefix column names with a data type indicator. Hungarian notation for
table names is even worse.

To get you start: you can use DATEDIFF with the HOUR parameter to count
the number of hour boundaries between two datetimes. If you also have to
take the minutes (or seconds) count into consideration, then you might
need a different parameter.

Also, be aware of integer division. Make sure to cast the number of
Widgets Produced to a decimal if you expect the average per hour to have
a fraction like for example 0.5.

Good luck,
Gert-Jan