-
Notifications
You must be signed in to change notification settings - Fork 72
/
PostgreSQL-BI-CHALLENGE
339 lines (289 loc) · 6.18 KB
/
PostgreSQL-BI-CHALLENGE
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
-- BUSINESS INTELLIGENCE CHALLENGE
-- TASK 1 - EXPLORATION
-- QUESTION 1 - How many projects are in the dataset?
select
count(*)
from
public.projects
where
id is not null;
-- QUESTION 2 - Explore the data and dive into any irregularities.
-- DUPLICATE IDS
-- TABLE 1 - PROJECTS
select
id,
COUNT(*)
from
public.projects
group by
id
having
count(*)>1;
-- TABLE 2 - BIDPACKAGES
select
id,
COUNT(*)
from
public.bidpackages
group by
id
having
count(*)>1;
-- TABLE 3 - BIDREQUESTS
select
id,
COUNT(*)
from
public.bidrequests
group by
id
having
count(*)>1;
-- TABLE 4 - SUBCONTRACTORS
select
id,
COUNT(*)
from
public.subcontractors
group by
id
having
count(*)>1;
-- NULL VALUES
-- TABLE 1 - PROJECTS
select
count(sbquery)
from
(
select
*
from
public.projects
where
id is null
or status is null
or createdat is null
or updatedat is null
or deletedat is null
or startson is null
or endson is null
or budget is null) as sbquery;
-- TABLE 2 - BIDPACKAGES
select
count(sbquery)
from
(
select
*
from
public.bidpackages
where
id is null
or status is null
or createdat is null
or updatedat is null
or deletedat is null
or bidsdueat is null
or projectid is null
or currency is null
or budget is null) as sbquery;
-- TABLE 3 - BIDPACKAGES
select
count(sbquery)
from
(
select
*
from
public.bidrequests
where
id is null
or status is null
or createdat is null
or updatedat is null
or deletedat is null
or subcontractorid is null
or bidpackageid is null
or declinedreason is null) as sbquery;
-- TABLE 4 - SUBCONTRACTORS
select
count(sbquery)
from
(
select
*
from
public.subcontractors
where
id is null
or "type" is null
or createdat is null
or updatedat is null
or deletedat is null
or companysize is null
or countrycode is null) as sbquery;
-- QUESTION 3 - What geographical area are we currently covering?
select
distinct countrycode
from
public.subcontractors
where
countrycode is not null;
-- QUESTION 4 - BIDPROJECTS REGISTERED BY MONTH
select
to_char(to_timestamp (date_part('month', createdat)::text, 'MM'), 'Month')
as Month_Name,
count(id) as Number_of_Projects
from
public.projects
group by
Month_Name
order by
Number_of_Projects desc;
-- QUESTION 5 - AVERAGE BUDGET OF ALL BID PROJECTS
select
round(avg(budget)::decimal, 0) as Budget
from
public.bidpackages;
-- QUESTION 6 - TOTAL SUM OF ALL BID PROJECTS
select
round(sum(budget)::decimal, 0) as Budget
from
public.bidpackages;
-- QUESTION 7 - NUMBER OF BID REQUESTS BY STATUS
select
status,
count(*) as Number_of_Bidrequests
from
bidrequests
group by
status
order by
Number_of_Bidrequests desc;
--------------------------------------------------------------------------------------------------------------------------
-- TASK 2 - ANALYSE
-- QUESTION 1 - Which subcontractors are sent bid requests most often?
select
subcontractorid,
count(*) as sent_bid_requests
from
public.bidrequests
where
status = 'invited'
or status = 'pendingInvite'
group by
subcontractorid
order by
sent_bid_requests desc
limit 5;
-- QUESTION 2 - What is the average time between the creation of a project and the creation of the first bid request
-- within that project?
select
avg(sbquery.time_diff) as average_time_diff
from
(
select
min(br.createdat) - min(p.createdat) as time_diff
from
public.projects p
join public.bidpackages bp
on
p.id = bp.projectid
join public.bidrequests br
on
br.bidpackageid = bp.id
group by
br.id
) as sbquery;
-- QUESTION 3 - WHICH COMPANY SIZE OF SUBCONTRACTORS HAVE THE HIGHEST NUMBERS OF BIDAWARDS?
select
s.companysize as Company_Size,
count(*) as Number_of_bidAwards
from
public.bidrequests br
join public.subcontractors s
on
s.id = br.subcontractorid
where
br.status = 'bidAwarded'
and s.companysize is not null
group by
Company_Size
order by
Number_of_bidAwards desc;
-- QUESTION 4 - NUMBER ONE REASON OF DECILINING THE BID REQUEST OF SUBCONTRACTORS
select
declinedreason,
count(*) as Number_of_Declines
from
public.bidrequests
where
declinedreason is not null
group by
declinedreason
order by
Number_of_Declines desc;
-- QUESTION 5 - The product team would like to know whether the conversion rate of bid requests, from the initial
-- status to status “bidSubmitted” is better or worse depending on the type of subcontractor that is being
-- invited.
-- CONVERSION RATE OF NETWORK SUBCONTRACTORS
select
(
select
cast(count(distinct s.id) as numeric)
from
public.subcontractors s
join public.bidrequests b
on
b.subcontractorid = s.id
where
s."type" = 'network'
and b.status = 'bidSubmitted') / (
select
cast(count(distinct s.id) as numeric)
from
public.subcontractors s
join public.bidrequests b
on
b.subcontractorid = s.id
where
s."type" = 'network'
and status = 'invited') * 100 as Conversion_Rate_of_Network_Subcontractors;
-- CONVERSION RATE OF COMPANY SUBCONTRACTORS
select
(
select
cast(count(distinct s.id) as numeric)
from
public.subcontractors s
join public.bidrequests b
on
b.subcontractorid = s.id
where
s."type" = 'company'
and b.status = 'bidSubmitted') / (
select
cast(count(distinct s.id) as numeric)
from
public.subcontractors s
join public.bidrequests b
on
b.subcontractorid = s.id
where
s."type" = 'company'
and status = 'invited') * 100 as Conversion_Rate_of_Company_Subcontractors;
--------------------------------------------------------------------------------------------------------------------------
-- TASK 3 - RECOMMENDATION
-- QESTION 1 - SEGMENTATION OF SUBCONTRACTORS BY COUNTRY
select
countrycode,
count(id) as Number_of_Subcontractors
from
public.subcontractors
where
countrycode is not null
group by
countrycode
order by
Number_of_Subcontractors desc;
--------------------------------------------------------------------------------------------------------------------------