I need to get monthly sales numbers in each Country & region.
Also - a number of orders, customers and sales persons in each month with a total amount.
I got stuck as I cannot understand how to make all counts/sum and also group by month as I only have daily data.
I have tried something like this:
SELECT
orderdate,
TerritoryID,
(
SELECT
COUNT(SalesOrderID)
FROM
adwentureworks_db.salesorderheader),
COUNT(DISTINCT CustomerID) SalesPersonID,
SUM(totaldue)
FROM
adwentureworks_db.salesorderheader
GROUP BY
OrderDate,
TerritoryID,
TotalDue`
It should look like this:
Data:
The 1st day of a month can be obtained by date_trunc(orderdate,month)
For the inner SELECT
, I do not know what you want to archive. The total number can be obtained by a window function count(...) over()
.
Select
date_trunc(orderdate,month),
TerritoryID,
COUNT(DISTINCT CustomerID) as SalesPersonID,
SUM(totaldue) as totaldue,
COUNT(SalesOrderID) over () as totalsalesorder_of_whole_table
FROM adwentureworks_db.salesorderheader
GROUP BY 1,2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.