Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need help with query


Need help with query

Author
Message
Brian Pastre
Brian Pastre
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9005 Visits: 19028
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
Brian Pastre
Brian Pastre
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9005 Visits: 19028
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search