Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Using SUM with DATETIME's

Author  Topic 

Fishwagon
Starting Member

10 Posts

Posted - 2009-10-01 : 09:23:32
Hello,

I am using this query to get the amount of time someone spends within a certain action:

*******************************

SELECT SiteID, StationID, StatsModuleLaunch.ModuleName,

CASE SUM( StatsModuleLaunch.EndDateTime - StatsModuleLaunch.StartDateTime )

WHEN 0 THEN NULL

ELSE COUNT( ExternalBatchID ) / SUM( StatsModuleLaunch.EndDateTime - StatsModuleLaunch.StartDateTime ) / 24

END

AS BatchesPerHour

FROM StatsModuleLaunch

LEFT JOIN StatsBatchModule ON StatsModuleLaunch.ModuleLaunchID = StatsBatchModule.ModuleLaunchID

WHERE StatsModuleLaunch.EndDateTime IS NOT NULL

GROUP BY SiteID, StationID, StatsModuleLaunch.ModuleName

*******************************

The problem resides in using DATETIME with SUM. Can anyone give me advice as to how to rewrite this query?

Thanks!!

Thanks and have a great day...

Rich

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-10-01 : 09:30:11
[code]SELECT sbm.SiteID,
sbm.StationID,
sml.ModuleName,
COUNT(sbm.ExternalBatchID) / (1 + DATEDIFF(DAY, sml.StartDateTime, sml.EndDateTime)) AS BatchesPerHour
FROM StatsModuleLaunch AS sml
LEFT JOIN StatsBatchModule AS sbm ON sbm.ModuleLaunchID = sml.ModuleLaunchID
WHERE sml.EndDateTime IS NOT NULL
GROUP BY sbm.SiteID,
sbm.StationID,
sml.ModuleName[/code]

N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Fishwagon
Starting Member

10 Posts

Posted - 2009-10-01 : 09:53:07
Thank you Peso, I appreciate it very much. That worked...

Thanks and have a great day...

Rich
Go to Top of Page
   

- Advertisement -