mirror of
https://github.com/youronlydimwit/Data_ScienceUse_Cases.git
synced 2025-12-24 11:09:54 +01:00
147 lines
4.8 KiB
SQL
147 lines
4.8 KiB
SQL
-- Calculate the total revenue generated by each store for year 2018.
|
|
SELECT
|
|
[Store Name] = S.store_name,
|
|
[Gross Revenue] = FORMAT(SUM(OI.list_price*OI.quantity), 'N0'),
|
|
[Net Revenue] = FORMAT(SUM((OI.list_price*OI.quantity)-((OI.list_price*OI.quantity)*OI.discount)), 'N0')
|
|
FROM
|
|
order_items OI join orders O on O.order_id = OI.order_id join stores S on O.store_id = S.store_id
|
|
WHERE
|
|
YEAR(O.order_date) = 2018
|
|
GROUP BY
|
|
S.store_name
|
|
|
|
-- Find the top 5 selling products across all stores based on the quantity sold.
|
|
WITH RankedProducts AS (
|
|
SELECT
|
|
[Product Name] = P.product_name,
|
|
[qty_Sold] = SUM(OI.quantity),
|
|
[RANK] = DENSE_RANK() OVER (ORDER BY SUM(OI.quantity) DESC)
|
|
FROM
|
|
order_items OI
|
|
JOIN products P ON OI.product_id = P.product_id
|
|
GROUP BY
|
|
P.product_name
|
|
)
|
|
SELECT DISTINCT
|
|
[Product Name],
|
|
[qty_Sold],
|
|
[RANK]
|
|
FROM
|
|
RankedProducts
|
|
WHERE
|
|
[RANK] <= 5;
|
|
|
|
|
|
-- Retrieve Jamaal Albert and Melia Brady's order history, including the order date, product details, and total amount spent.
|
|
SELECT
|
|
[Customer Name] = CONCAT(CU.first_name , ' ' , CU.last_name),
|
|
[Order Date] = O.order_date,
|
|
[Product details] = P.product_name,
|
|
[Category] = C.category_name,
|
|
[Amount spent] = FORMAT(SUM((OI.list_price*OI.quantity)-((OI.list_price*OI.quantity)*OI.discount)),'N0')
|
|
FROM
|
|
orders O
|
|
join order_items OI on O.order_id = OI.order_id
|
|
join products P on OI.product_id = P.product_id
|
|
join categories C on P.category_id = C.category_id
|
|
join customers CU on CU.customer_id = O.customer_id
|
|
WHERE (CU.first_name = 'Jamaal' and CU.last_name = 'Albert') OR (CU.first_name = 'Melia' and CU.last_name = 'Brady')
|
|
GROUP BY
|
|
CONCAT(CU.first_name, ' ' , CU.last_name), O.order_date, P.product_name, C.category_name
|
|
ORDER BY
|
|
[Customer Name] asc
|
|
|
|
|
|
-- List the products that are running low on stock (quantity less than 10) in each store.
|
|
SELECT
|
|
[Store Name] = S.store_name,
|
|
[Product Name] = P.product_name,
|
|
[Stocks] = SUM(ST.quantity)
|
|
FROM
|
|
stores S join stocks ST on S.store_id = ST.store_id join products P on ST.product_id = P.product_id
|
|
GROUP BY store_name, product_name
|
|
HAVING SUM(ST.quantity) < 10
|
|
ORDER BY [Stocks] desc
|
|
|
|
-- List the products that are running low on stock (quantity less than 10) in each store.
|
|
SELECT
|
|
[Store Name],
|
|
[Product Name],
|
|
[Sum Qty]
|
|
FROM (
|
|
SELECT
|
|
[Store Name] = S.store_name,
|
|
[Product Name] = P.product_name,
|
|
[Sum Qty] = SUM(ST.quantity)
|
|
FROM
|
|
stores S join stocks ST on S.store_id = ST.store_id join products P on ST.product_id = P.product_id
|
|
GROUP BY
|
|
store_name, product_name
|
|
) as SUBQQ
|
|
WHERE [Sum Qty] < 10
|
|
|
|
-- Identify the top 5 staff members who have processed the highest total value of orders.
|
|
SELECT TOP 5
|
|
[Staff Name] = CONCAT(ST.first_name, ' ' , ST.last_name),
|
|
[Orders Processed] = COUNT(O.order_id)
|
|
FROM staffs ST join orders O on ST.staff_id = O.staff_id
|
|
GROUP BY ST.first_name, ST.last_name
|
|
ORDER BY [Orders Processed] desc
|
|
|
|
-- category wise sales percentage XX
|
|
SELECT
|
|
[Category Name] = C.category_name,
|
|
[Total Sales] = FORMAT(SUM((OI.list_price*OI.quantity)-((OI.list_price*OI.quantity)*OI.discount)),'N0'),
|
|
[Percentage] = FORMAT((SUM((OI.list_price*OI.quantity)-((OI.list_price*OI.quantity)*OI.discount)) /
|
|
SUM(SUM((OI.list_price * OI.quantity) - ((OI.list_price * OI.quantity) * OI.discount))) OVER ()),'#.##%')
|
|
FROM
|
|
categories C join products P on C.category_id = P.category_id join order_items OI on OI.product_id = P.product_id
|
|
GROUP BY C.category_name
|
|
ORDER BY [Percentage] desc
|
|
|
|
-- Identify staff members who are marked as inactive (1) and have not processed any orders.
|
|
SELECT
|
|
[Staff Name] = CONCAT(S.first_name, ' ' , S.last_name)
|
|
FROM staffs S join orders O on O.staff_id = S.staff_id
|
|
WHERE S.active = 1
|
|
GROUP BY S.first_name, S.last_name
|
|
HAVING count(O.order_id) = 0
|
|
|
|
-- Customer Loyalty
|
|
WITH Loyalty AS(
|
|
SELECT
|
|
[Customer Name] = CONCAT(C.first_name, ' ' , C.last_name),
|
|
[Stores] = S.store_name,
|
|
[Store_Orders] = COUNT(DISTINCT S.store_name),
|
|
[Count_Orders] = COUNT(O.order_id),
|
|
[Avg_Spending] = AVG((OI.list_price*OI.quantity)-((OI.list_price*OI.quantity)*OI.discount))
|
|
FROM
|
|
customers C
|
|
join orders O on C.customer_id = O.customer_id
|
|
join order_items OI on O.order_id = OI.order_id
|
|
join stores S on S.store_id = O.store_id
|
|
GROUP BY
|
|
C.first_name, C.last_name, S.store_name, O.order_id
|
|
)
|
|
SELECT
|
|
[Customer Name],
|
|
[Stores],
|
|
[Count_Orders],
|
|
[Avg_Spending]
|
|
FROM
|
|
Loyalty
|
|
WHERE [Store_Orders] > 1
|
|
|
|
-- Calculate the average discount amount applied to products in each product category.
|
|
SELECT
|
|
[Product Category] = C.category_name,
|
|
[Average Discount] = FORMAT(AVG(OI.list_price*OI.discount),'N0')
|
|
FROM
|
|
categories C
|
|
join products P on C.category_id = P.category_id
|
|
join order_items OI on OI.product_id = P.product_id
|
|
GROUP BY
|
|
C.category_name
|
|
|
|
|