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
Related
I have a dataset which has customer information regarding the number of orders they have placed, their order value and their customer id.
I am trying to find out what is the average order value of the customers, how many orders they have placed and what is the total revenue they generated in the month of November. However, I also want to group this into quintiles to see what were these values for the top 5%, next 5% and so on. I want to group them into quintiles based on the customers total order values.
So far I have tried:
select customer_number , orders, amount,
percentile_cont(0.95) within group(order by amount) over(partition by customer_account_id) as ninetyfive,
percentile_cont(0.90) within group(order by amount) over(partition by customer_account_id) as ninety,
from(
select customer_number , count(ordernum) as orders, sum(amount_paid) as amount
from data_table
and date_part(month ,order_date) = 11
and date_part(year, order_date) =2021
group by customer_number
order by amount desc
)
group by customer_number, orders, amount
order by amount desc;
I want to count the number of books delivered per country, per month and also generate the average time it took to deliver the book.
I have the following columns book_title, Country_Delivered, OrderDate, DeliveryDate
The results should look like this;
|Country|Month|Number_Books|Average_Delivery|
I am using Postgres
select to_char(OrderDate, 'YYYY-MM') year_month
, Country
, count(*) Number_Books
, avg(DeliveryDate - OrderDate) Average_Delivery_days
from tablename
group by to_char(OrderDate, 'YYYY-MM') ,Country
I have two tables: orders(id, date, qty) A and sales(id, date, qty) B.
I want to know the total orders and sales quantity per day (I may have none or many orders and sales on the same day).
Results should look like this: A.day1 A.qty B.day1 B.qty.
I tried a full outer join but totals per day double.
Assuming I understood what you want, you are on the right lines with Full join:
SELECT ISNULL(o.OrderDate,s.SaleDate),
o.Total AS TotalOrders,
s.Total AS TotalSales
FROM (
SELECT OrderDate,
SUM(Qty) AS Total
FROM Orders
GROUP BY OrderDate
) o
FULL JOIN (
SELECT SaleDate,
SUM(Qty) AS Total
FROM Sales
GROUP BY SaleDate
) s
ON o.OrderDate = s.SaleDate
I have renamed some of your column names (date is not a good name as it is a reserved)
Also, your DBMS isn't specified. The above is SQL Server. If you are using MySQL you will need to substitute ISNULL for IFNULL
I performed the following query with cte's, but I was wondering if there was a simpler way of writing the code, maybe with subqueries? I'm retrieving everything from one table SALES, but I'm using 3 columns: AgentID, SaleDate, and OrderID.
WITH RECENT_SALE AS(
SELECT AGENTID,(
SALEDATE,
ROW_NUMBER() OVER (PARTITION BY AGENTID ORDER BY SALEDATE DESC) AS RN
FROM SALES
)
,
COUNT_SALE AS (
SELECT AGENTID,
COUNT(ORDERID) AS COUNTORDERS
FROM SALES
)
SELECT RECENT_SALE.MRN,
SALEDATE,
COUNTORDERS
FROM RECENT_SALE
INNER JOIN COUNT_SALE ON RECENT_SALE.AGENTID = COUNT_SALE.AGENTID;
It looks to me like you're just trying to get the total number of sales per agent as well as the date of his or her most recent sale? If I understand your structure correctly (and I may not), then it seems pretty straightforward. I'm guessing orderid is the primary key of SALES?
SELECT agentid, MAX(saledate) AS saledate -- Most recent sale date
, COUNT(orderid) AS countsales -- total sales
FROM sales
GROUP BY agentid;
There does not seem to be any need for CTEs or subqueries here.
Try this:
SELECT
saledate,
AGENTID,
count(orderid) over(partition by AGENTID order by saledate)
FROM SALES
group by
saledate,
AGENTID
Using Oracle/SQL, I am looking for a way to count the number distinct IDs above or below a specified value following a sum. For example, I need to find how many customers have ordered $1,000 or more in goods last year, regardless of whether it was in one large order or several smaller orders. I don't need to identify each customer specifically, just find the total number of customers over this amount.
So far I am able to find the total of orders with:
select sum (Order_Amount), Customer_ID
from table.orders_placed
where year = 2013
group by Customer_ID
order by Customer_ID
I can also expand it doing this:
select count (dinstinct Customer_ID)
from(
select sum (Order_Amount), Customer_ID
from table.orders_placed
where year = 2013
group by Customer_ID
order by Customer_ID
)
but this just gives me the total number of distinct Customer_ID. Any other argument that I add to try to narrow what the "count" gives me results in an error. How can I specify that I want the total Order_Amount of $1,000 or more?
Try (no need to order by in inner query)
select count (dinstinct Customer_ID)
from(
select sum (Order_Amount) total_order_amount, Customer_ID
from table.orders_placed
where year = 2013
group by Customer_ID
) where total_order_amount > 1000
OR with Having
select sum (Order_Amount) total_order_amount, Customer_ID
from table.orders_placed
where year = 2013
group by Customer_ID
having sum(order_amount) > 1000
Use a HAVING clause to restrict the results of the grouping:
select sum (Order_Amount) as Total, Customer_ID
from table.orders_placed
where year = 2013
group by Customer_ID
having sum (Order_Amount) >= 1000
Then you can use this as a subquery to perform your aggregation and filtering.