SQL - Aggregates by Weekday

Status
Not open for further replies.

Twinspar

Seniorius Lurkius
6
Hi everybody! I need some guidance putting together an SQL query that will return averages by day of week. The table I’m querying holds transactional data consisting of over a thousand records daily. Fields include a Creation_Date (datetime), a unique document number (primary key, varchar) and a value (numeric) per record. There are of course many additional fields but these are the only ones I’m interested in working with. I can use some simple code to return day of week using datename, record count and sum of values per day.
I need to then analyze this data to return average count and amount by day of week.
Initial query and sample results:

SELECT
DATENAME(DW,Creation_Date) AS Day,
CONVERT(date,Creation_Date) AS Date,
COUNT(DocNo) AS DocCount,
SUM(Total) AS Amount
FROM
dbo.MyData
WHERE
Creation_Date > '2015-01-01'
GROUP BY
Creation_Date
ORDER BY
Creation_Date
--RETURNS—-

DayDate DocCount Amount
Friday 1/2/2015 1441 68192
Saturday 1/3/2015 799 34583
Sunday 1/4/2015 451 18617
Monday 1/5/2015 1350 59975
Tuesday 1/6/2015 677 25424
Wednesday 1/7/2015 1128 48521
Thursday 1/8/2015 998 43151
Friday 1/9/2015 1114 46813
Saturday 1/10/2015 774 31038
Sunday 1/11/2015 255 11400
Monday 1/12/2015 1209 50153
Tuesday 1/13/2015 1257 57449
Wednesday 1/14/2015 1201 49877
Thursday 1/15/2015 1268 56306
Friday 1/16/2015 1285 60320
Saturday 1/17/2015 865 34715
Sunday 1/18/2015 487 21199
Tuesday 1/20/2015 1294 58168
Wednesday 1/21/2015 1127 47409
Thursday 1/22/2015 1436 64852
Friday 1/23/2015 1344 56398
Saturday 1/24/2015 884 36033
Sunday 1/25/2015 509 21588
Monday 1/26/2015 1126 46652
Tuesday 1/27/2015 1238 55571
Wednesday 1/28/2015 1286 59487
Thursday 1/29/2015 1415 66770
Friday 1/30/2015 1320 58685
Saturday 1/31/2015 883 36821
Sunday 2/1/2015 432 20674
Monday 2/2/2015 1526 68797
Tuesday 2/3/2015 1413 61141
Wednesday 2/4/2015 1389 60538
Thursday 2/5/2015 1525 70245
Friday 2/6/2015 1366 61992
Saturday 2/7/2015 588 22846
Sunday 2/8/2015 480 21060
Monday 2/9/2015 1483 65946
Tuesday 2/10/2015 1280 53970
Wednesday 2/11/2015 1330 61890

The ultimate result I’m looking for would look like (no decimal results):
Day AvgCount AvgAmount
Sunday 436 19090
Monday 1339 58305
Tuesday 1193 51954
Wednesday 1244 54620
Thursday 1578 73136
Friday 1312 58733
Saturday 799 32673

There are some days that have no records so the averages must be determined by totals/count of daySunday, totals/count of dayTuesday, etc. What is the simplest way to arrive at this result?
 

mcmnky

Ars Tribunus Militum
2,625
Subscriptor
Something like this?

SELECT
daily_data.Day,
SUM(daily_data.DocCount)/COUNT(daily_data.Date) AS AvgCount,
SUM(daily_data.Amount)/COUNT(daily_data.Date) AS AvgAmount
FROM
(
SELECT
DATENAME(DW,Creation_Date) AS Day,
CONVERT(date,Creation_Date) AS Date,
COUNT(DocNo) AS DocCount,
SUM(Total) AS Amount
FROM
dbo.MyData
WHERE
Creation_Date > '2015-01-01'
GROUP BY
Creation_Date
) daily_data
GROUP BY
daily_data.Day

Though check if your system has an AVERAGE function for groups, rather than messing with COUNT and calculations.

edit: While the I think the query above works, I don't like. Why take a count/sum for each date and then count/sum for a group of dates? A sum of sums is just a sum. This should be done with a single query. Get a count of docs for all Tuesdays, then divide by the number of Tuesdays (or use a built in average function).
 

Twinspar

Seniorius Lurkius
6
This last query....

SELECT
DATENAME(DW,Creation_Date) AS Day,
COUNT(DocNo)/COUNT(CONVERT(date,Creation_Date)) AS AvgDocCount,
SUM(Total)/COUNT(CONVERT(date,Creation_Date)) AS AvgAmount
FROM
dbo.MyData
WHERE
Creation_Date > '2015-01-01'
GROUP BY
DATENAME(DW,Creation_Date)

....is returning goofy info....
Day AvgCount AvgAmount
Friday 1 44.918407
Monday 1 43.609246
Saturday 1 40.886706
etc, etc.

The previous solution (with the subquery) gives me the results I'm looking for.
 
Status
Not open for further replies.