We are attempting to get the average number of sales per month using SQL. We've been able to return the average number of sales for all of our data but not for specific months. Our current thinking is that by using the following code and dividing by the number of work days in a month, we should be able to get the results we are looking for. Any help or suggestions will be greatly appreciated.
By 'working days' we mean days where a sale came in. Example of what we're referring to is shown in the final code block.
Our current code looks as follows
SELECT
AVG(Orders.num)
/*Need Help Here*/
FROM
(
SELECT
DAY(DateTimeCreated) as day,
MONTH(DateTimeCreated) as month,
YEAR(DateTimeCreated) as year,
COUNT(DISTINCT OrderID) AS num
FROM OrderHeader
WHERE
DateTimeCreated >= DATEADD(
month,
datediff(month, 0, DATEADD(yy,DATEDIFF(yy,0,GETDATE())-1,0)),
0
)
AND OrderType <> 2
AND Deleted <> 1
AND BranchID = 9
GROUP BY
YEAR(DateTimeCreated),
MONTH(DateTimeCreated),
DAY(DateTimeCreated)
)
AS Orders
Returns the Following
AVG Orders
48
Only Running the following
SELECT
DAY(DateTimeCreated) as day,
MONTH(DateTimeCreated) as month,
YEAR(DateTimeCreated) as year,
COUNT(DISTINCT OrderID) AS num
FROM OrderHeader
WHERE
DateTimeCreated >= DATEADD(
month,
datediff(month, 0, DATEADD(yy,DATEDIFF(yy,0,GETDATE())-1,0)),
0
)
AND OrderType <> 2
AND Deleted <> 1
AND BranchID = 9
GROUP BY
YEAR(DateTimeCreated),
MONTH(DateTimeCreated),
DAY(DateTimeCreated)
Order By
YEAR(DateTimeCreated),
MONTH(DateTimeCreated),
DAY(DateTimeCreated)
Returns (Only showing 2 months as not to make this post longer than necessary)
day month year num
18 7 2023 22
19 7 2023 12
20 7 2023 37
21 7 2023 50
22 7 2023 18
23 7 2023 1
24 7 2023 56
25 7 2023 56
26 7 2023 74
27 7 2023 68
28 7 2023 41
30 7 2023 1
31 7 2023 55
1 8 2023 88
2 8 2023 62
3 8 2023 123
4 8 2023 91
5 8 2023 10
6 8 2023 4
7 8 2023 84
8 8 2023 77
9 8 2023 65
10 8 2023 56
11 8 2023 57
12 8 2023 5
13 8 2023 5
14 8 2023 78
15 8 2023 75
16 8 2023 53
17 8 2023 59
18 8 2023 51
19 8 2023 11
20 8 2023 24
21 8 2023 62
22 8 2023 59
23 8 2023 60
24 8 2023 92
25 8 2023 71
26 8 2023 1
27 8 2023 9
28 8 2023 63
29 8 2023 63
30 8 2023 72
31 8 2023 67
We have attempted too many different lines to state in one post. The closest we've came to accomplishing our desired result came from suggestions in the following post. How to determine the number of days in a month in SQL Server?. We believe the subquery path is the right way but are struggling on this final part.
Using a calendar table, this becomes somewhat trivial.
Using a randomized set of sales data and a calendar table function
This gives us the total invoice count for the month, and the number of days in each month and an average per day.
This of course does not take into account weekdays, or public holidays and assumes all days count.
If we just want to include invoices made on week days, the calendar table also has a column we can filter on. Note that this will then also exclude any invoices which were created on week ends:
This now gives us a count of just the week days, and an average of the invoices from those days. You can see that the total for January 2022 is now significantly lower, indicating that in my data set there were a lot of invoices on the weekends. Interestingly the averages are not dissimilar.