-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSales&Revenue_analysis.sql
More file actions
159 lines (121 loc) · 4.75 KB
/
Sales&Revenue_analysis.sql
File metadata and controls
159 lines (121 loc) · 4.75 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
158
159
/* In this sales and revenue analsysis I will try to find out the answer to the following quetions
1. Find the top 10 categories whose avg products price is expensive? Also do find cheapest 10 ?
2. Find the top 10 most ordered product categories?
3. Find out which payment method is most used for orders?
4. Find out the distribution of payment installments ?
5. Find the total orders yearly and monthly?
6. Find out the total sales revenue yearly and monthly?
7. Find out the average frieght paid by customers?
*/
--1. Find the top 10 categories whose avg products price is expensive? Also do find cheapest 10 ?
-- we have to join order_items with products and then with translation to get results in english
with exppd AS
(
select pt.product_category_name, avg(oi.price) avg_price
from olist_order_items oi join olist_products pt
on oi.product_id = pt.product_id
group by pt.product_category_name
order by avg_price DESC
limit 10
)
select exppd.product_category_name, nt.product_category_name_english, exppd.avg_price avg_price
from exppd join olist_product_name_translation nt
on exppd.product_category_name = nt.product_category_name
order by avg_price desc;
-- observation computer category have most expensive products in average
with chpd AS
(
select pt.product_category_name, avg(oi.price) as avg_price
from olist_order_items oi join olist_products pt
on oi.product_id = pt.product_id
group by pt.product_category_name
order by avg_price asc
limit 10
)
select chpd.product_category_name, nt.product_category_name_english, chpd.avg_price avg_price
from chpd join olist_product_name_translation nt
on chpd.product_category_name = nt.product_category_name
order by avg_price asc;
-- observation house_comfort_2 or flowers category have cheapest products in average
--2. Find the top 10 most ordered product categories?
with products_ordered as
(
select p.product_category_name, count(oi.product_id) productord
from olist_order_items oi join olist_products p
on oi.product_id = p.product_id
group by p.product_category_name
order by productord desc
limit 10
)
select po.product_category_name, nt.product_category_name_english, po.productord
from products_ordered po join olist_product_name_translation nt
on po.product_category_name = nt.product_category_name
order by po.productord desc
--observation : most order are from bed_bath_table category and least from security and services
--3. Find out which payment method is most used for orders?
with num_count as
(
select payment_type, count(order_id) num
from olist_order_payments
group by payment_type
order by num desc
)
,
total as
(
select count(order_id) tot
from olist_order_payments
)
select payment_type, num, (num :: float / tot) * 100 as percentage
from num_count, total
--observation : 73.9 percent of transsactions aare done through credit cards
--4. Find out the distribution of payment installments ?
with num_count as
(
select payment_installments, count(order_id) num
from olist_order_payments
group by payment_installments
order by payment_installments asc
)
,
total as
(
select count(order_id) tot
from olist_order_payments
)
select payment_installments, num, (num :: float / tot) * 100 as percentage
from num_count, total
-- Observation for 50% of the orders customers pay at once.
--5. Find the total orders yearly and monthly?
select extract(year from order_purchase_timestamp) as year, count(order_id) as total_orders
from olist_orders
group by year
select extract(month from order_purchase_timestamp) as month, count(order_id) as total_orders
from olist_orders
group by month
order by total_orders desc
-- oberservation : order sales are highest in the month August and least in the month of november
select extract(month from order_purchase_timestamp) as month, extract(year from order_purchase_timestamp) as year, count(order_id) as total_orders
from olist_orders
group by year, month
-- 6. Find out the total sales revenue yearly and monthly?
select sum(op.payment_value) as sales_revenue, extract(year from o.order_purchase_timestamp) as year
from olist_order_payments op join olist_orders o
on op.order_id = o.order_id
group by year
order by sales_revenue desc
--observation sales have increased in 2018
select sum(op.payment_value) as sales_revenue, extract(month from o.order_purchase_timestamp) as month
from olist_order_payments op join olist_orders o
on op.order_id = o.order_id
group by month
order by sales_revenue desc
select sum(op.payment_value) as sales_revenue, extract(year from o.order_purchase_timestamp) as year, extract(month from o.order_purchase_timestamp) as month
from olist_order_payments op join olist_orders o
on op.order_id = o.order_id
group by year, month
--7. Find out the average frieght paid by customers?
select avg(freight_value)
from olist_order_items
select count(product_id)
from olist_products