You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WITH base_cte AS (
SELECTc.CustomerName,
c.CustomerID,
OrderNumber,
o.OrderID,
o.OrderDate,
Quantity,
UnitPrice
FROMorders.orders o
LEFT JOINorders.OrderItems od
ONod.OrderID=o.OrderIDLEFT JOINCustomer.Customers c
ONc.CustomerID=o.CustomerIDLEFT JOINsales.Payments p
ONp.OrderID=o.OrderID
),
cte_aggregation AS (
SELECT
CustomerName,
CustomerID,
MAX(OrderDate) AS last_order,
MIN(OrderDate) AS first_order,
COUNT(OrderNumber) AS total_orders,
SUM(Quantity) AS total_quantity,
SUM(Quantity * UnitPrice) AS total_sales
FROM base_cte
GROUP BY
CustomerName,
CustomerID
)
SELECT TOP 10
CustomerName,
total_quantity,
total_orders,
total_sales,
ROUND((total_sales / total_orders), 2) AS avg_order_value,
total_sales / DATEDIFF(month, first_order, last_order) AS avg_monthly_spend
FROM cte_aggregation
ORDER BY total_sales DESC;
2. Find customers whose total revenue is greater than the average total revenue of all customers.
SELECT
CustomerName,
total_sales
FROM(
SELECTc.CustomerName,
SUM(quantity * unitprice) AS total_sales,
AVG(SUM(quantity * unitprice)) OVER() AS avg_sales
FROMorders.orders o
LEFT JOINorders.OrderItems od
ONod.OrderID=o.OrderIDLEFT JOINCustomer.Customers c
ONc.CustomerID=o.CustomerIDGROUP BY CustomerName
) AS t
WHERE total_sales > avg_sales
3. Product sales performance by category
SELECTpc.CategoryNameAS category_name,
COALESCE(SUM(o.Quantity), 0) AS total_quantity,
COALESCE(SUM(o.Quantity*p.UnitCost), 0) AS cost_of_sales,
CAST(ROUND(COALESCE(SUM((o.Quantity*o.UnitPrice) * (1-o.DiscountPct)), 0), 1) ASDECIMAL(18,1)) AS total_sales,
CAST(
COALESCE(SUM((o.Quantity*o.UnitPrice) * (1-o.DiscountPct)), 0)
/ NULLIF(SUM(o.Quantity), 0)
ASDECIMAL(18,1)
) AS avg_selling_price,
COUNT(DISTINCT p.ProductID) AS number_of_products
FROMProduct.Products p
LEFT JOINProduct.Categories pc
ONp.CategoryID=pc.CategoryIDLEFT JOINOrders.OrderItems o
ONo.ProductID=p.ProductIDGROUP BYpc.CategoryNameORDER BY total_sales DESC;
4. Top 3 products per category
WITH cte_base AS (
SELECTpc.CategoryName,
p.ProductName,
o.Quantity,
o.UnitPrice,
o.DiscountPctFROMProduct.Products p
LEFT JOINProduct.Categories pc
ONp.CategoryID=pc.CategoryIDLEFT JOINOrders.OrderItems o
ONo.ProductID=p.ProductID
),
cte_aggregation AS (
SELECT
CategoryName,
ProductName,
SUM((Quantity * UnitPrice) * (1- DiscountPct)) AS total_revenue
FROM cte_base
GROUP BY
CategoryName,
ProductName
),
cte_rank AS (
SELECT
CategoryName,
ProductName,
CAST(
SUM(total_revenue) OVER (
PARTITION BY CategoryName
ORDER BY total_revenue DESC
) ASDECIMAL(10, 2)
) AS product_revenue,
ROW_NUMBER() OVER (
PARTITION BY CategoryName
ORDER BY total_revenue ASC
) AS Rank_number
FROM cte_aggregation
)
SELECT
CategoryName,
ProductName,
product_revenue,
Rank_number
FROM cte_rank
WHERE Rank_number <=3;
5.Monthly revenue with previous month comparison,running totals, 3 Months rolling average
WITH base_query AS(
SELECT
YEAR(s.orderdate) AS order_year,
datetrunc(Month,s.OrderDate) AS order_month,
CAST(SUM((UnitPrice * Quantity) * (1-DiscountPct)) ASDECIMAL (10,2)) AS total_revenue
FROMOrders.OrderItems o
INNER JOINorders.Orders s
ONo.OrderID=s.OrderIDGROUP BY datetrunc(Month,s.OrderDate),YEAR(s.orderdate)
)
SELECT
order_year,
order_month,
total_revenue,
CASE WHEN LAG(total_revenue) OVER (ORDER BY order_month) is NULL THEN '-'
ELSE
CONCAT(CAST(((total_revenue / LAG(total_revenue) OVER (ORDER BY order_month)) -1)*100ASDECIMAL (10,2)),'%')
END AS pct_change,
SUM(total_revenue) Over(order By order_month) as running_total,
CAST(AVG(total_revenue) Over(order by order_month rows between 2 preceding and current row) asdecimal(10,2)) as three_month_rolling_avg
FROM base_query
ORDER BY order_month
6. Running total revenue for customers over time
WITH cte_cust AS(
SELECTo.CustomerID,
c.CustomerName,
o.OrderID,
OrderDate,
SUM(Quantity * UnitPrice) as revenue
FROMCustomer.Customers c
LEFT JOINorders.Orders o
ONc.CustomerID=o.CustomerIDINNER JOINOrders.OrderItems OT
ONot.OrderID=o.OrderIDGROUP BYo.CustomerID,
c.CustomerName,
o.OrderID,
OrderDate
)
SELECT*,
SUM(revenue) Over (partition BY customerID order by revenue,orderdate
rows between unbounded preceding and current row) as running_revenue
FROM cte_cust;
7. Find products that were returned at least once
SELECT
ProductID,
productName,
total_quantity_sold,
total_return,
CONCAT(
CAST(100* CAST(
total_return as FLOAT) / NULLIF(total_quantity_sold, 0) asdecimal(10,2))
,'%') as pct_return
FROM(
SELECTp.ProductID,
p.ProductName,
SUM(o.quantity) as total_quantity_sold,
COALESCE(SUM(r.ReturnQty), 0) as total_return
FROMOrders.OrderItems o
LEFT JOINsales.Returns r
ONr.OrderItemID=o.OrderItemIDLEFT JOINProduct.Products p
ONo.ProductID=p.ProductIDGROUP BYp.ProductID,
p.ProductName)t
ORDER BY pct_return ASC;
8. Creating View: order_revenue_details
CREATE or ALTER VIEW order_summary ASSELECTo.OrderID,
DATETRUNC(MONTH,o.OrderDate) as order_month,
c.CustomerID,
c.CustomerName,
e.EmployeeID,
e.FullName,
r.RegionID,
r.CityName,
o.OrderStatus,
SUM(od.Quantity*od.UnitPrice* (1-od.DiscountPct))as total_revenue
FROMorders.Orders o
LEFT JOINCustomer.Customers c
ONc.CustomerID=o.CustomerIDLEFT JOINEmployees.Employees e
ONe.EmployeeID=o.EmployeeIDLEFT JOINRegion.Regions r
ONr.RegionID=o.RegionIDLEFT JOINorders.OrderItems od
ONod.OrderID=o.OrderIDGROUP BYo.OrderID,
o.OrderDate,
c.CustomerID,
c.CustomerName,
e.EmployeeID,
e.FullName,
r.RegionID,
o.OrderStatus,
r.CityName;
GO
8. Creating View: customer_performance_details
CREATE OR ALTER VIEW dbo.customer_performance_detailsASSELECT
customerid,
customername,
last_order_date,
total_orders,
total_revenue,
CAST(total_revenue / total_orders asdecimal (10,2)) as avg_order_revenue,
DATEDIFF(Month,last_order_date,getdate()) as month_since_last_order,
CASE when total_revenue <=15000 THEN 'Low Value'
WHEN total_revenue between 15000and35000 THEN 'Mid Value'
ELSE 'High Value'
END as customer_category
FROM(
SELECTc.CustomerID,
c.CustomerName,
MAX(o.orderdate) as last_order_date,
SUM(Quantity) as total_orders,
CAST(SUM((Quantity * UnitPrice)*(1-DiscountPct)) asdecimal(10,2)) as total_revenue
FROMorders.Orders o
LEFT JOINorders.OrderItems od
ONo.OrderID=od.OrderIDLEFT JOINcustomer.Customers c
ONc.CustomerID=o.CustomerIDGROUP BYc.CustomerID,
c.CustomerName)t
;
9. For each sales territory, find the employee with the highest total revenue.
WITH cte_base AS(
SELECT
SalesTerritory,
e.FullName,
CONCAT('£',CAST(SUM((quantity * unitprice)*(1-od.discountpct)) asdecimal(10,2))) AS total_rev
FROMEmployees.Employees e
LEFT JOINRegion.Regions r
ONr.RegionID=e.RegionIDLEFT JOINorders.orders ord
ONord.EmployeeID=e.EmployeeIDLEFT JOINorders.OrderItems od
ONod.OrderID=ord.OrderIDGROUP BY SalesTerritory,
e.FullName
),
rank_cte AS(
SELECT*,
ROW_NUMBER() OVER(PARTITION BY salesterritory ORDER BY total_rev DESC) AS Ranks
FROM cte_base)
SELECT
SalesTerritory,
FullName,
total_rev
FROM rank_cte
WHERE Ranks =1ORDER BY total_rev;