-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLQuery1_2.sql
More file actions
157 lines (131 loc) · 4.03 KB
/
SQLQuery1_2.sql
File metadata and controls
157 lines (131 loc) · 4.03 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
select * from df_orders
--FIND TOP 10 HIGHEST REVENUE GENERATING PRODUCTS
select top 10 product_id, sum(sell_price) as sales
from df_orders
group by product_id
order by sales desc
--FIND TOP 5 HIGHEST SELLING PRODUCTS REGIONwise
select top 5 product_id, region, sum(sell_price) as sales
from df_orders
group by product_id, region
order by sales desc
select product_id, region, sum(sell_price) as sales
from df_orders
group by product_id, region
order by region,sales desc
--FIND TOP 5 HIGHEST SELLING PRODUCTS IN EACH REGION
--Method - 1
select distinct region from df_orders
select top 5 product_id, region, sum(sell_price) as sales
from df_orders
where region = 'EAST'
group by product_id, region
order by sales desc
select top 5 product_id, region, sum(sell_price) as sales
from df_orders
where region = 'South'
group by product_id, region
order by sales desc
select top 5 product_id, region, sum(sell_price) as sales
from df_orders
where region = 'West'
group by product_id, region
order by sales desc
select top 5 product_id, region, sum(sell_price) as sales
from df_orders
where region = 'Central'
group by product_id, region
order by sales desc
--Method - 2(cte and ranking)
with cte as(
select product_id, region,sum(sell_price) as sales
from df_orders
group by region,product_id)
select * from (
select *
,rank() over(partition by region order by sales desc) as rn
from cte) A
where rn<=5
--FIND MONTH OVER MONTH GROWTH COMPARISON FOR 2022 AND 2023 SALES EG JAN 2022 VS JAN 2023
--Method-1
select distinct year(order_date) from df_orders
select year(order_date) as order_year ,month(order_date),
sum(sell_price) as sales
from df_orders
where year(order_date) = '2023'
group by year(order_date),month(order_date)
order by month(order_date)
select year(order_date) as order_year ,month(order_date),
sum(sell_price) as sales
from df_orders
where year(order_date) = '2022'
group by year(order_date),month(order_date)
order by month(order_date)
--Method-2
with cte as(
select year(order_date) as order_year, month(order_date) as order_month , sum(sell_price) as sales
from 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
order by order_month
--FOR EACH CATEGORY WHICH MONTH HAD HIGHEST SALES
select distinct category from df_orders
select category , max(sell_price) as sales
from df_orders
where category = 'Furniture'
group by category
select category , max(sell_price) as sales
from df_orders
where category = 'Office Supplies'
group by category
select category , max(sell_price) as sales
from df_orders
where category = 'Technology'
--group by category
--Method -2
with cte as (
select category ,year(order_date) as order_year, month(order_date) as order_month, sum(sell_price) as sales
from df_orders
group by category,year(order_date), month(order_date)
)
select * from (
select *,
row_number() over(partition by category order by sales desc) as rn
from cte
) A
where rn = 1
--OR
with cte as (
select category ,format(order_date,'yyyyMM') as order_date_format, max(sell_price) as sales
from df_orders
group by category,format(order_date,'yyyyMM')
)
select * from (
select *,
row_number() over(partition by category order by sales desc) as rn
from cte
) A
where rn = 1
--WHICH SUB-CATEGORY HAD THE HIGHEST GROWTH BY PROFIT IN 2023 COMPARE TO 2022
with cte as(
select sub_category,year(order_date) as order_year,
sum(sell_price) as sales
from df_orders
group by sub_category,year(order_date)
)
, cte2 as (
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
)
select top 1 *,
(sales_2023 - sales_2023)*100/sales_2022
from cte2
order by (sales_2023 - sales_2023)*100/sales_2022