-
Notifications
You must be signed in to change notification settings - Fork 0
/
All_SQL_Queries.sql
66 lines (59 loc) · 1.78 KB
/
All_SQL_Queries.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- Finding top 10 highest revenue generating products
SELECT
TOP 10 product_id, SUM(sale_price) AS sales
FROM
dbo.df_orders
GROUP BY product_id
ORDER BY sales DESC
;
--- Finding top 5 selling products in region
WITH cte AS(
SELECT
region, product_id, SUM(sale_price) AS sales
FROM
dbo.df_orders
GROUP BY product_id,region)
SELECT *
FROM (
SELECT *
, ROW_NUMBER() over (partition by region order by sales desc)as rn
FROM cte) a
WHERE rn<=5
;
--- Finding month over month growth comparison for 2022 and 2023 sales
WITH cte as
(
SELECT SUM(sale_price) AS Sales, YEAR(order_date) as order_year, MONTH(order_date) as Order_month
FROM dbo.df_orders
GROUP BY YEAR(order_date), MONTH(order_date)
)
SELECT Order_month,
sum(CASE WHEN order_year = 2022 then sales else 0 end) as sales_2022,
sum(CASE WHEN order_year = 2023 then sales else 0 end) as sales_2023
FROM cte
group by Order_month;
--- Finding for each category which month had highest sales
With CTE as (
SELECT SUM(sale_price) as sales, format(order_date, 'yyyy-MM') as order_month_year , category
FROM
dbo.df_orders
GROUP BY category, format(order_date, 'yyyy-MM')
)
Select * from (
SELECT *,
ROW_NUMBER() over (partition by category order by sales desc)as rn
from cte
) as a
where rn = 1;
--- which sub categories had highest growth by profit in 2023 compare to 2022
WITH cte as
(
SELECT SUM(sale_price) AS Sales, YEAR(order_date) as order_year, MONTH(order_date) as Order_month, sub_category
FROM dbo.df_orders
GROUP BY YEAR(order_date), MONTH(order_date),sub_category
)
SELECT sub_category,
sum(CASE WHEN order_year = 2022 then sales else 0 end) as sales_2022,
sum(CASE WHEN order_year = 2023 then sales else 0 end) as sales_2023
FROM cte
group by sub_category;