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.
| Author |
Topic |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2008-07-09 : 02:59:50
|
| I need to calculate time durations limited to search boundaries. Here is an example of what I mean.Using SQL 2005 Business Intelligence Development Studio - Visual Studio 2005 for the first time. Implementing in Reporting Services.User enters a date range as @sourcestarttime and @sourceendtime using report parameters. For this example the user enters 1/2/2008 to 29/2/2008. In the final report I need it to work for specific year, month, day, hour or specific date range as used in this example.Data in TimeDetails table:TimeID SourceStartTime SourceEndTime1 2/2/2008 10:00:00AM 3/2/2008 11:00:00AM2 31/1/2008 11:00:00PM 1/2/2008 01:00:00AM3 29/2/2008 11:00:00PM 1/2/2008 01:00:00AM4 29/2/2008 11:00:00PM NULL5 31/1/2008 11:00:00PM 1/3/2008 01:00:00AM6 31/1/2008 11:00:00PM NULLThe total duration for this report should be 59 days and 4 hours.1 1day 1hour (report duration 1 day 1 hour - start and end within range)2 2hours (report duration 1 hour ie from midnight to 1AM - start not within range3 2hours (report duration 1 hour ie from 11PM to midnight - end not within range)4 NA (report duration 1 hour ie from 11PM to midnight - no end date)5 29days and 2hours (report duration 29 days - start and end date not within range)6 NA (report duration 29days - no end date and start date not within range)Here is what I have so far which is fine if sourcestarttime and sourceendtime is within @sourcestarttime and @sourceendtime. Also calculating totalseconds as a field and then using that to calculate seperate fields for days, hours, minutes, seconds. Also I am filtering by the SiteName field so the TimeID's (key) in the TimeDetails table are linked to certain SiteID's (key) in the SiteDetails table and hence the SiteName field in the table. This means the total duration is for each SiteName in the SiteDetails table.SELECT SUM(Duration.TotalSeconds) / 86400 AS Days, SUM(Duration.TotalSeconds) % 86400 / 3600 AS Hours, SUM(Duration.TotalSeconds) % 3600 / 60 AS Minutes, SUM(Duration.TotalSeconds) % 60 AS Seconds, TimeDetails_1.TimeID, Duration.TotalSecondsFROM(SELECT TimeID, DATEDIFF(second, SourceStartTime, SourceEndTime) AS TotalSecondsFROM TimeDetails)AS Duration INNER JOINTimeDetails AS TimeDetails_1 ON Duration.TimeID = TimeDetails_1.TimeID INNER JOINEventDetails ON TimeDetails_1.TimeID = EventDetails.TimeID INNER JOINSystemState ON EventDetails.EventID = SystemState.EventID INNER JOINSiteDetails ON SystemState.SiteID = SiteDetails.SiteIDWHERE(TimeDetails_1.SourceStartTime >= @SourceStartTime) AND (SiteDetails.SiteName IN (@SiteName)) AND (TimeDetails_1.SourceStartTime <= @SourceEndTime + DAY(0))GROUP BY TimeDetails_1.TimeID, Duration.TotalSecondsI hope I have provided the correct detail.Thankyou in advance |
|
|
|
|
|
|
|