Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need help with query Expand / Collapse
Author
Message
Posted Wednesday, January 30, 2008 2:46 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, December 19, 2013 12:41 PM
Points: 11, Visits: 43
Hello,
I have a column in a table that stores time in milliseconds and I want to be able to sum the rows for each day and have a total for the week. I have just started on this and have the following query:

SELECT queueTime, StartDateTime
FROM ContactRoutingDetail WHERE
StartDateTime >= DATEADD(wk,DATEDIFF(wk,0,GETDATE()),0)-1
- CASE WHEN DATENAME(dw,GETDATE()) LIKE 'Sun%' THEN 7 ELSE 0 END
AND StartDateTime < DATEADD(wk,DATEDIFF(wk,0,GETDATE())+1,0)
- CASE WHEN DATENAME(dw,GETDATE()) LIKE 'Sun%' THEN 7 ELSE 0 END
GROUP BY queueTime, StartDateTime
Order By StartDateTime

I'm getting the results for each day of the current week, but I'm stuck on how to SUM them.
TIA,
BP
Post #449711
Posted Thursday, January 31, 2008 6:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:33 AM
Points: 7,178, Visits: 13,630
This calculates daily totals for the last whole week, monday through saturday:

DROP TABLE #ContactRoutingDetail
GO
CREATE TABLE #ContactRoutingDetail (queueTime INT, StartDateTime DATETIME)
GO
INSERT INTO #ContactRoutingDetail (queueTime, StartDateTime)
SELECT 10, '2008-01-31 11:23:32.937' UNION ALL -- THU
SELECT 10, '2008-01-31 11:22:32.937' UNION ALL
SELECT 10, '2008-01-30 11:21:32.937' UNION ALL -- WED
SELECT 10, '2008-01-30 11:19:32.937' UNION ALL
SELECT 10, '2008-01-29 11:18:32.937' UNION ALL -- TUE
SELECT 10, '2008-01-29 11:17:32.937' UNION ALL
SELECT 10, '2008-01-28 11:16:32.937' UNION ALL -- MON
SELECT 10, '2008-01-28 11:15:32.937' UNION ALL
SELECT 10, '2008-01-27 11:14:32.937' UNION ALL -- SUN
SELECT 10, '2008-01-27 11:13:32.937' UNION ALL
SELECT 10, '2008-01-27 00:00:00.000' UNION ALL
SELECT 10, '2008-01-26 23:59:59.997' UNION ALL -- SAT
SELECT 10, '2008-01-26 11:11:32.937' UNION ALL
SELECT 10, '2008-01-26 00:00:00.000' UNION ALL
SELECT 10, '2008-01-25 11:10:32.937' UNION ALL -- FRI
SELECT 10, '2008-01-25 11:09:32.937' UNION ALL
SELECT 10, '2008-01-24 11:08:32.937' UNION ALL -- THU
SELECT 10, '2008-01-24 11:07:32.937' UNION ALL
SELECT 10, '2008-01-23 11:06:32.937' UNION ALL -- WED
SELECT 10, '2008-01-23 11:05:32.937' UNION ALL
SELECT 10, '2008-01-22 11:04:32.937' UNION ALL -- TUE
SELECT 10, '2008-01-22 11:03:32.937' UNION ALL
SELECT 10, '2008-01-21 11:02:32.937' UNION ALL -- MON
SELECT 10, '2008-01-21 11:01:32.937' UNION ALL
SELECT 10, '2008-01-21 00:00:00.000' UNION ALL
SELECT 10, '2008-01-20 23:59:59.997' UNION ALL -- SUN
SELECT 10, '2008-01-20 11:59:32.937' UNION ALL
SELECT 10, '2008-01-20 00:00:00.000'
GO

DECLARE @Today DATETIME
SET @Today = GETDATE() + 2 -- choose your run day / date for testing

SET DATEFIRST 1 -- monday is now considered the first day of the week (check the current setting)

SELECT SumDayTime, CONVERT(DATETIME, TextDate) AS StartDateTime
FROM (
SELECT SUM(queueTime) AS SumDayTime, CONVERT(CHAR(8), StartDateTime, 112) AS TextDate, DATENAME(dw,StartDateTime) AS DayName, DATEPART(dw, StartDateTime) AS DayNo
FROM #ContactRoutingDetail
WHERE StartDateTime >= CONVERT(CHAR(8), @Today - (DATEPART(dw, @Today)+6), 112)
AND StartDateTime < CONVERT(CHAR(8), @Today - (DATEPART(dw, @Today)+0), 112)
GROUP BY CONVERT(CHAR(8), StartDateTime, 112), DATENAME(dw,StartDateTime), DATEPART(dw, StartDateTime)
) t
ORDER BY t.TextDate

Cheers

ChrisM


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #449911
Posted Thursday, January 31, 2008 2:48 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, December 19, 2013 12:41 PM
Points: 11, Visits: 43
Thanks Chris,
That did the trick. I had to tweak it just a little to suite my exact situation, but I couldn't have done it without your help.
Thanks
BP
Post #450195
Posted Thursday, January 31, 2008 11:35 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:33 AM
Points: 7,178, Visits: 13,630
You're very welcome Brian, and many thanks for the feedback.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #450327
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse