Skip to content

You're working for a company that sells motorcycle parts, and they've asked for some help in analyzing their sales data!

They operate three warehouses in the area, selling both retail and wholesale. They offer a variety of parts and accept credit cards, cash, and bank transfer as payment methods. However, each payment type incurs a different fee.

The board of directors wants to gain a better understanding of wholesale revenue by product line, and how this varies month-to-month and across warehouses. You have been tasked with calculating net revenue for each product line and grouping results by month and warehouse. The results should be filtered so that only "Wholesale" orders are included.

They have provided you with access to their database, which contains the following table called sales:

Sales

ColumnData typeDescription
order_numberVARCHARUnique order number.
dateDATEDate of the order, from June to August 2021.
warehouseVARCHARThe warehouse that the order was made from— North, Central, or West.
client_typeVARCHARWhether the order was Retail or Wholesale.
product_lineVARCHARType of product ordered.
quantityINTNumber of products ordered.
unit_priceFLOATPrice per product (dollars).
totalFLOATTotal price of the order (dollars).
paymentVARCHARPayment method—Credit card, Transfer, or Cash.
payment_feeFLOATPercentage of total charged as a result of the payment method.

Your query output should be presented in the following format:

product_linemonthwarehousenet_revenue
product_one---------
product_one---------
product_one---------
product_one---------
product_one---------
product_one---------
product_two---------
............
Spinner
DataFrameas
revenue_by_product_line
variable
/*
Find out how much Wholesale net revenue each product_line generated per month per warehouse in the dataset.

The query should be saved as revenue_by_product_line using the SQL cell provided, and contain the following:

- product_line,
- month: displayed as 'June', 'July', and 'August',
- warehouse, and
- net_revenue: the sum of total minus the sum of payment_fee.

The results should be sorted by product_line and month, followed by net_revenue in descending order.

Note: Please also ensure that you do not change the name of the DataFrame that the query result will be saved as - creating new cells in the workbook will produce a DataFrame with a different name. Make sure that your final solution uses the names provided: revenue_by_product_line (see image below).
*/

-- Start coding here
WITH grouped_sales AS (
	
	SELECT
		product_line,
	    TO_CHAR (date, 'Month') AS month,
		warehouse,
		SUM((total - payment_fee)) AS net_revenue
	FROM sales
	WHERE client_type = 'Wholesale'
	GROUP BY product_line, TO_CHAR (date, 'Month'), warehouse
	
)

SELECT
	product_line,
	month,
	warehouse,
    net_revenue
FROM grouped_sales
ORDER BY product_line, month, net_revenue DESC