dbTalk Databases Forums  

Split a period of time

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Split a period of time in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
dvestal@gmail.com
 
Posts: n/a

Default Split a period of time - 02-26-2010 , 10:08 AM






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?

Reply With Quote
  #2  
Old   
Gert-Jan Strik
 
Posts: n/a

Default Re: Split a period of time - 02-26-2010 , 04:44 PM






"dvestal (AT) gmail (DOT) com" wrote:
Quote:
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

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.