-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analysis of Customer Occupation.sql
292 lines (229 loc) · 12.8 KB
/
Analysis of Customer Occupation.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
USE Adworks;
--------------------------------------------------------------------------------------------------------------------------------------
/* Joining the sales tables to create 1 overall sales table*/
SELECT *
INTO Overall_sales
FROM Aw_Sales_2015
UNION ALL
SELECT*
FROM Aw_Sales_2016
UNION ALL
SELECT*
FROM Aw_Sales_2017;
--------------------------------------------------------------------------------------------------------------------------------------
/*CUSTOMER OCCUPATION DEMOGRAPHY*/
--Calculating the customer distribution accross each occupation
SELECT occupation, COUNT(occupation) AS [Count]
FROM Aw_Customers
GROUP BY Occupation
ORDER BY 2 DESC;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the Average Age of each occupation
SELECT occupation, AVG (DATEDIFF(YEAR,BirthDate, GETDATE())) AS [Average Age]
FROM Aw_Customers
GROUP BY Occupation
ORDER BY 2 DESC;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the Marital Status of each occupation
SELECT occupation, MaritalStatus, COUNT (MaritalStatus) AS [Count]
FROM Aw_Customers
GROUP BY Occupation,MaritalStatus
ORDER BY 1, 3 DESC;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the Household Size of each occupation
SELECT occupation,
CASE WHEN TotalChildren > 2 THEN 'LargeHousehold'
ELSE 'SmallHousehold'
END AS [Size],
COUNT(*) AS [Count]
FROM Aw_Customers
GROUP BY Occupation,
CASE WHEN TotalChildren > 2 THEN 'LargeHousehold'
ELSE 'SmallHousehold'
END
ORDER BY 1;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--Calculating the Educational level of each occupation
SELECT Occupation, EducationLevel, COUNT(EducationLevel) AS [Count]
FROM Aw_Customers
GROUP BY Occupation, EducationLevel
ORDER BY 1;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the Average Annual Salary of each occupation
SELECT occupation, ROUND(AVG(AnnualIncome),2) AS [Average Annual Income]
FROM Aw_Customers
GROUP BY Occupation
ORDER BY 2 DESC;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the Gender distribution per Customer Occupation
SELECT Occupation, Gender, COUNT(Gender) AS [Gender count]
FROM Aw_Customers
GROUP BY Occupation, Gender
ORDER BY 1;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the distribution of Occupation accross each Country
SELECT tr.Country,ct.occupation, COUNT(ct.occupation) AS [Occupation Count]
FROM Aw_Territories tr
JOIN Overall_sales os
ON os.TerritoryKey = tr.SalesTerritoryKey
JOIN Aw_Customers ct
ON os.CustomerKey = ct.CustomerKey
GROUP BY tr.Country,ct.occupation;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--Calculating the percentage of occupation that are home owners
SELECT occupation, COUNT(HomeOwner) AS [Count],
COUNT(HomeOwner)*100/
(SELECT COUNT(HomeOwner)
FROM Aw_Customers
WHERE HomeOwner = 'Y') AS [Percentage]
FROM Aw_Customers
WHERE HomeOwner = 'Y'
GROUP BY Occupation
ORDER BY 3 DESC;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--Calculating the percentage of occupation that are not home owners
SELECT occupation, COUNT(HomeOwner) AS [Count],
COUNT(HomeOwner)*100/
(SELECT COUNT(HomeOwner)
FROM Aw_Customers
WHERE HomeOwner = 'N') AS [Percentage]
FROM Aw_Customers
WHERE HomeOwner = 'N'
GROUP BY Occupation
ORDER BY 3 DESC;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*CUSTOMER OCCUPATION PURCHASE BEHAVIOR*/
-- Calculating the Overall Revenue for all Customer Occupation
SELECT ct.Occupation, ROUND(SUM(pt.ProductPrice * os.OrderQuantity),2) AS [Revenue]
FROM Overall_sales os
JOIN Aw_Customers ct
ON os.CustomerKey = ct.CustomerKey
JOIN Aw_Products pt
ON os.ProductKey = pt.ProductKey
GROUP BY ct.Occupation
ORDER BY 2 DESC;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the yearly revenue performance per Customer Occupation
SELECT YEAR(os.OrderDate) AS [Year],ct.Occupation,ROUND(SUM(pt.ProductPrice * os.OrderQuantity),2) AS [Revenue]
FROM Overall_sales os
JOIN Aw_Customers ct
ON os.CustomerKey = ct.CustomerKey
JOIN Aw_Products pt
ON os.ProductKey = pt.ProductKey
GROUP BY ct.Occupation,YEAR(os.OrderDate)
ORDER BY 1,2;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the quarterly revenue performance per Customer Occupation
SELECT CASE WHEN DATEPART(QUARTER,os.OrderDate) = 1 THEN 'Q1'
WHEN DATEPART(QUARTER,os.OrderDate) = 2 THEN 'Q2'
WHEN DATEPART(QUARTER,os.OrderDate) = 3 THEN 'Q3'
WHEN DATEPART(QUARTER,os.OrderDate) = 4 THEN 'Q4'
END AS [Quarter],
ct.Occupation,
ROUND(SUM(pt.ProductPrice * os.OrderQuantity),2) AS [Revenue]
FROM Overall_sales os
JOIN Aw_Customers ct
ON os.CustomerKey = ct.CustomerKey
JOIN Aw_Products pt
ON os.ProductKey = pt.ProductKey
GROUP BY CASE WHEN DATEPART(QUARTER,os.OrderDate) = 1 THEN 'Q1'
WHEN DATEPART(QUARTER,os.OrderDate) = 2 THEN 'Q2'
WHEN DATEPART(QUARTER,os.OrderDate) = 3 THEN 'Q3'
WHEN DATEPART(QUARTER,os.OrderDate) = 4 THEN 'Q4'
END,
ct.Occupation
ORDER BY 1,2;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the monthly revenue performance per Customer Occupation
SELECT DATEPART(MONTH,os.OrderDate) AS [Month_ID],DATENAME(MONTH,os.OrderDate) AS [Month],ct.Occupation,ROUND(SUM(pt.ProductPrice * os.OrderQuantity),2) AS [Revenue]
FROM Overall_sales os
JOIN Aw_Customers ct
ON os.CustomerKey = ct.CustomerKey
JOIN Aw_Products pt
ON os.ProductKey = pt.ProductKey
GROUP BY DATEPART(MONTH,os.OrderDate), ct.Occupation,DATENAME(MONTH,os.OrderDate)
ORDER BY 1;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the revenue performance per Customer Occupation for each day of the week
SELECT CASE WHEN DATEPART(WEEKDAY,os.OrderDate) = 1 THEN 8
ELSE DATEPART(WEEKDAY,os.OrderDate)
END AS [Day_ID],
DATENAME(WEEKDAY,os.OrderDate) AS [Day],
ct.Occupation,
ROUND(SUM(pt.ProductPrice * os.OrderQuantity),2) AS [Revenue]
FROM Overall_sales os
JOIN Aw_Customers ct
ON os.CustomerKey = ct.CustomerKey
JOIN Aw_Products pt
ON os.ProductKey = pt.ProductKey
GROUP BY CASE WHEN DATEPART(WEEKDAY,os.OrderDate) = 1 THEN 8
ELSE DATEPART(WEEKDAY,os.OrderDate)
END,
DATENAME(WEEKDAY,os.OrderDate),
ct.Occupation
ORDER BY 1;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the revenue performance per Customer Occupation during weekdays and weekends
SELECT
CASE WHEN DATEPART(WEEKDAY,os.OrderDate) IN (7,8) THEN 'Weekend'
ELSE 'Weekday'
END AS [Day_type],
ct.Occupation,
ROUND(SUM(pt.ProductPrice * os.OrderQuantity),2) AS [Revenue]
FROM Overall_sales os
JOIN Aw_Customers ct
ON os.CustomerKey = ct.CustomerKey
JOIN Aw_Products pt
ON os.ProductKey = pt.ProductKey
GROUP BY ct.Occupation, CASE WHEN DATEPART(WEEKDAY,os.OrderDate) IN (7,8) THEN 'Weekend'
ELSE 'Weekday'
END
ORDER BY 1;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the number of Orders and Quantity Ordered per Customer Occupation
SELECT ct.Occupation, COUNT(DISTINCT os.OrderNumber) AS [Orders], SUM(OrderQuantity) AS [Quantity]
FROM Aw_Customers ct
JOIN Overall_sales os
ON ct.CustomerKey = os.CustomerKey
GROUP BY ct.Occupation;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the average revenue generated per customer occupation
SELECT ct.Occupation, ROUND(SUM(pt.ProductPrice * os.OrderQuantity)/ COUNT(DISTINCT os.OrderNumber),2) AS [Average Order Value]
FROM Aw_Customers ct
JOIN Overall_sales os
ON ct.CustomerKey = os.CustomerKey
JOIN Aw_Products pt
ON os.ProductKey = pt.ProductKey
GROUP BY ct.Occupation
ORDER BY 2 DESC;
--------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the revenue performance for each product Category per Customer occupation
SELECT pc.CategoryName AS [Category Name],ct.Occupation, ROUND(SUM(pt.ProductPrice * os.OrderQuantity),2) AS [Revenue]
FROM Overall_sales os
JOIN Aw_Customers ct
ON os.CustomerKey = ct.CustomerKey
JOIN Aw_Products pt
ON os.ProductKey = pt.ProductKey
JOIN Aw_Product_Subcategory ps
ON pt.ProductSubcategoryKey = ps.ProductSubcategoryKey
JOIN Aw_Product_Category pc
ON ps.ProductCategoryKey = pc.ProductCategoryKey
GROUP BY ct.Occupation,pc.CategoryName
ORDER BY 1;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Calculating the Top 3 Products ordered per Customer Occupation
WITH RankedProducts AS (
SELECT
ct.Occupation AS [Customer Occupation],
pt.ProductName AS [Product],
COUNT(DISTINCT os.OrderNumber) AS [Order Count],
ROW_NUMBER() OVER(PARTITION BY ct.Occupation ORDER BY COUNT(DISTINCT os.OrderNumber) DESC) AS RowNum
FROM Aw_Customers ct
JOIN Overall_sales os ON ct.CustomerKey = os.CustomerKey
JOIN Aw_Products pt ON pt.ProductKey = os.ProductKey
GROUP BY ct.Occupation, pt.ProductName
)
SELECT [Customer Occupation], [Product], [Order Count]
FROM RankedProducts
WHERE RowNum <= 3
ORDER BY [Customer Occupation], [Order Count] DESC;