forked from mdedeu/SalesReportDataProgram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.sql
More file actions
461 lines (405 loc) · 17 KB
/
functions.sql
File metadata and controls
461 lines (405 loc) · 17 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
CREATE TABLE yearT (
anio INTEGER,
bisiesto BOOLEAN,
PRIMARY KEY(anio)
);
CREATE TABLE semester (
semestre INTEGER CHECK (semestre IN (1, 2)),
anio INTEGER,
semID INTEGER,
FOREIGN KEY (anio) REFERENCES yearT,
PRIMARY KEY(semID),
UNIQUE(anio, semestre)
);
CREATE TABLE quarter (
trimestre INTEGER CHECK (trimestre IN (1, 2, 3, 4)),
semID INTEGER,
trimID INTEGER,
FOREIGN KEY (semID) REFERENCES semester,
PRIMARY KEY(trimID),
UNIQUE(trimestre, semID)
);
CREATE TABLE monthT (
mes INTEGER CHECK (mes BETWEEN 1 AND 12),
trimID INTEGER,
nombre TEXT CHECK (nombre IN ('Jan', 'Feb', 'Mar', 'April', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec')),
mesID INTEGER,
FOREIGN KEY (trimID) REFERENCES quarter,
PRIMARY KEY(mesID),
UNIQUE(mes, trimID)
);
CREATE TABLE dayT (
dia INTEGER,
mesID INTEGER,
diaID INTEGER,
nombre TEXT CHECK (nombre IN ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')),
finde BOOLEAN,
FOREIGN KEY (mesID) REFERENCES monthT,
PRIMARY KEY(diaID),
UNIQUE(dia, mesID)
);
----------------------------------------------------------------------
CREATE TABLE definitiva (
id INTEGER,
year_birth INTEGER NOT NULL,
education TEXT NOT NULL CHECK (education IN ('2n Cycle', 'Basic', 'Graduation', 'Master', 'PhD')),
marital_status TEXT NOT NULL CHECK( marital_status IN ('Absurd', 'Alone', 'Divorced', 'Married', 'Single', 'Together', 'Widow')),
income INTEGER,
kidhome INTEGER NOT NULL,
teenhome INTEGER NOT NULL,
diaID INTEGER NOT NULL, --hace ref a diaT. Trigger en dt_customer
recency INTEGER NOT NULL,
mnt_wines INTEGER NOT NULL,
mnt_fruits INTEGER NOT NULL,
mnt_meat INTEGER NOT NULL,
mnt_fish INTEGER NOT NULL,
mnt_sweet INTEGER NOT NULL,
num_deals_purchases INTEGER NOT NULL,
num_web_purchases INTEGER NOT NULL,
num_catalog_purchases INTEGER NOT NULL,
num_stores_purchases INTEGER NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY (diaID) REFERENCES dayT
);
CREATE TABLE intermedia (
id INTEGER,
year_birth INTEGER,
education TEXT,
marital_status TEXT,
income INTEGER,
kidhome INTEGER,
teenhome INTEGER,
dt_customer DATE,
recency INTEGER,
mnt_wines INTEGER,
mnt_fruits INTEGER,
mnt_meat INTEGER,
mnt_fish INTEGER,
mnt_sweet INTEGER,
num_deals_purchases INTEGER,
num_web_purchases INTEGER,
num_catalog_purchases INTEGER,
num_stores_purchases INTEGER,
PRIMARY KEY(id)
);
---------------------------------------------------------------
CREATE OR REPLACE FUNCTION bisiesto( yearP yearT.anio%type)
RETURNS BOOLEAN AS $$
BEGIN
IF yearP % 4 = 0 THEN
IF yearP % 100 <> 0 THEN
RETURN TRUE;
END IF;
IF yearP % 400 = 0 THEN
RETURN TRUE;
END IF;
END IF;
RETURN FALSE;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION get_semID(semP semester.semestre%type, yearP yearT.anio%type )
RETURNS INTEGER AS $$
DECLARE
foundSem INTEGER;
nextSemID INTEGER;
BEGIN
SELECT semID INTO foundSem
FROM semester
WHERE semestre = semP and anio = yearP;
IF foundSem IS NULL THEN
SELECT COUNT(*) INTO nextSemID
FROM semester;
INSERT INTO semester VALUES (semP, yearP, nextSemID + 1);
RETURN nextSemID + 1;
END IF;
RETURN foundSem;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION get_trimID(trimP quarter.trimestre%type, semP quarter.semID%type)
RETURNS INTEGER AS $$
DECLARE
foundTri INTEGER;
nextTriID INTEGER;
BEGIN
SELECT trimID INTO foundTri
FROM quarter
WHERE trimestre = trimP and semID = semP;
IF foundTri IS NULL THEN
SELECT COUNT(*) INTO nextTriID
FROM quarter;
INSERT INTO quarter VALUES (trimP, semP, nextTriID + 1);
RETURN nextTriID + 1;
ELSE
RETURN foundTri;
END IF;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION get_mesID(monthP monthT.mes%type, trimP monthT.trimID%type)
RETURNS INTEGER as $$
DECLARE
mName monthT.nombre%type;
foundMes INTEGER;
nextMesID INTEGER;
BEGIN
SELECT mesID INTO foundMes
FROM monthT
WHERE mes = monthP and trimID = trimP;
IF foundMes IS NULL THEN
SELECT COUNT(*) INTO nextMesID
FROM monthT;
CASE
when monthP = 01 THEN INSERT INTO monthT VALUES (monthP, trimP, 'Jan', nextMesID + 1);
when monthP = 02 THEN INSERT INTO monthT VALUES (monthP, trimP, 'Feb', nextMesID + 1);
when monthP = 03 THEN INSERT INTO monthT VALUES (monthP, trimP, 'Mar', nextMesID + 1);
when monthP = 04 THEN INSERT INTO monthT VALUES (monthP, trimP, 'April', nextMesID + 1);
when monthP = 05 THEN INSERT INTO monthT VALUES (monthP, trimP, 'May', nextMesID + 1);
when monthP = 06 THEN INSERT INTO monthT VALUES (monthP, trimP, 'June', nextMesID + 1);
when monthP = 07 THEN INSERT INTO monthT VALUES (monthP, trimP, 'July', nextMesID + 1);
when monthP = 08 THEN INSERT INTO monthT VALUES (monthP, trimP, 'Aug', nextMesID + 1);
when monthP = 09 THEN INSERT INTO monthT VALUES (monthP, trimP, 'Sept', nextMesID + 1);
when monthP = 10 THEN INSERT INTO monthT VALUES (monthP, trimP, 'Oct', nextMesID + 1);
when monthP = 11 THEN INSERT INTO monthT VALUES (monthP, trimP, 'Nov', nextMesID + 1);
when monthP = 12 THEN INSERT INTO monthT VALUES (monthP, trimP, 'Dec', nextMesID + 1);
END CASE;
RETURN nextMesID + 1;
END IF;
RETURN foundMes;
END;
$$ language plpgsql;
CREATE OR REPLACE FUNCTION get_diaID(diaP dayT.dia%type, mesIDP dayT.mesID%type, weekDayP INT)
RETURNS INTEGER as $$
DECLARE
mName dayT.nombre%type;
foundDia INTEGER;
nextDiaID INTEGER;
BEGIN
SELECT diaID INTO foundDia
FROM dayT
WHERE dia = diaP and mesID = mesIDP;
IF foundDia IS NULL THEN
SELECT COUNT(*) INTO nextDiaID
FROM dayT;
CASE
when weekDayP = 0 THEN INSERT INTO dayT VALUES (diaP, mesIDP, nextDiaID + 1, 'Sun', true); --hay que ver cual viene primero
when weekDayP = 1 THEN INSERT INTO dayT VALUES (diaP, mesIDP, nextDiaID + 1, 'Mon', false);
when weekDayP = 2 THEN INSERT INTO dayT VALUES (diaP, mesIDP, nextDiaID + 1, 'Tue', false);
when weekDayP = 3 THEN INSERT INTO dayT VALUES (diaP, mesIDP, nextDiaID + 1, 'Wed', false);
when weekDayP = 4 THEN INSERT INTO dayT VALUES (diaP, mesIDP, nextDiaID + 1, 'Thu', false);
when weekDayP = 5 THEN INSERT INTO dayT VALUES (diaP, mesIDP, nextDiaID + 1, 'Fri', false);
when weekDayP = 6 THEN INSERT INTO dayT VALUES (diaP, mesIDP, nextDiaID + 1, 'Sat', true);
END CASE;
RETURN nextDiaID + 1;
ELSE
RETURN foundDia;
END IF;
END;
$$ language plpgsql;
set datestyle to MDY;
CREATE OR REPLACE FUNCTION load_dates(fechaP DATE)
RETURNS INTEGER AS $$
DECLARE
yearV yearT.anio%type;
trimV quarter.trimestre%type;
monthV monthT.mes%type;
dayV dayT.dia%type;
bisV yearT.bisiesto%type;
semIDV semester.semID%type;
trimIDV quarter.trimID%type;
mesIDV monthT.mesID%type;
weekdayV INTEGER;
foundYear INTEGER;
BEGIN
--01/01/2021
yearV := EXTRACT(year from fechaP);
trimV := EXTRACT(quarter from fechaP);
monthV := EXTRACT(month from fechaP);
dayV := EXTRACT(day from fechaP);
weekdayV := EXTRACT(dow from fechaP);
bisV := bisiesto(yearV);
SELECT COUNT(*) INTO foundYear
FROM yearT
WHERE anio = yearV;
IF foundYear = 0 THEN
INSERT INTO yearT VALUES (yearV, bisV);
END IF;
--sem
IF monthV >= 7 THEN
semIDV := get_semID(2, yearV);
ELSE
semIDV := get_semID(1, yearV);
END IF;
--trim
trimIDV := get_trimID(trimV, semIDV);
--month
mesIDV := get_mesID(monthV, trimIDV);
--day
RETURN get_diaID(dayV, mesIDV, weekdayV);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION load_table()
RETURNS trigger AS $load_table$
DECLARE
IDdia definitiva.diaID%type;
BEGIN
IDdia := load_dates(new.dt_customer);
INSERT INTO definitiva VALUES (new.id, new.year_birth, new.education, new.marital_status, new.income, new.kidhome, new.teenhome, IDdia,
new.recency,new.mnt_wines,new.mnt_fruits,new.mnt_meat,new.mnt_fish, new.mnt_sweet,new.num_deals_purchases,new.num_web_purchases, new.num_catalog_purchases,new.num_stores_purchases);
RETURN new;
END;
$load_table$ LANGUAGE plpgsql;
CREATE TRIGGER load_table
AFTER INSERT OR UPDATE ON intermedia
FOR EACH ROW
EXECUTE PROCEDURE load_table();
CREATE OR REPLACE FUNCTION ReporteConsolidado(years int ) RETURNS void AS $$
DECLARE
currentYear int;
auxiChar char(4);
auxi RECORD;
auxiYear RECORD;
category2 TEXT;
recency2 INTEGER;
frecuency2 INTEGER;
monetary2 INTEGER;
totalRecency INTEGER;
totalMonetary INTEGER;
totalFrecuency INTEGER;
yearV INT;
cursor_year CURSOR FOR (
SELECT
CAST(yearT.anio AS CHAR(4)) as year,
AVG(recency) as recency,
AVG(num_catalog_purchases+num_deals_purchases+num_stores_purchases+num_web_purchases) as frecuency,
AVG(mnt_wines+mnt_fruits+mnt_meat+mnt_fish+mnt_sweet) as monetary
FROM definitiva
JOIN dayT ON definitiva.diaID = dayT.diaID
JOIN monthT ON monthT.mesID = dayT.mesID
JOIN quarter ON quarter.trimID = monthT.trimID
JOIN semester ON semester.semID = quarter.semID
JOIN yearT ON semester.anio = yearT.anio
GROUP BY yearT.anio
ORDER BY yearT.anio
);
cursor_birth CURSOR FOR(
SELECT (
CASE
WHEN 2016-year_birth < 25 THEN 'Birth range: 1) - de 25'
WHEN 2016-year_birth < 40 THEN 'Birth range: 2) de 25 a 39'
WHEN 2016-year_birth < 50 THEN 'Birth range: 3) de 40 a 49'
WHEN 2016-year_birth < 70 THEN 'Birth range: 4) de 50 a 69'
ELSE 'Birth range: 5) + de 70'
END
) AS category,
AVG(recency) as recency,
AVG(num_catalog_purchases+ num_deals_purchases+num_stores_purchases+num_web_purchases) as frecuency,
AVG(mnt_wines+mnt_fruits+mnt_meat+mnt_fish+mnt_sweet) as monetary
FROM definitiva
JOIN dayT ON definitiva.diaID = dayT.diaID
JOIN monthT ON monthT.mesID = dayT.mesID
JOIN quarter ON quarter.trimID = monthT.trimID
JOIN semester ON semester.semID = quarter.semID
JOIN yearT ON semester.anio = yeart.anio
WHERE yearT.anio = currentYear
GROUP BY category
);
cursor_education CURSOR FOR(
SELECT 'Education: ' || education AS category,
AVG(recency) as recency,
AVG(num_catalog_purchases+num_deals_purchases+num_stores_purchases+num_web_purchases) as frecency,
AVG(mnt_wines+mnt_fruits+mnt_meat+mnt_fish+mnt_sweet) as monetary
FROM definitiva
JOIN dayT ON definitiva.diaID = dayT.diaID
JOIN monthT ON monthT.mesID = dayT.mesID
JOIN quarter ON quarter.trimID = monthT.trimID
JOIN semester ON semester.semID = quarter.semID
JOIN yearT ON semester.anio = yearT.anio
WHERE yearT.anio = currentYear
GROUP BY category
);
cursor_maritalStatus CURSOR FOR(SELECT 'Marital status: ' || Marital_Status AS category,
AVG(recency) as recency,
AVG(num_catalog_purchases+num_deals_purchases+num_stores_purchases+num_web_purchases) as frecuency,
AVG(mnt_wines+mnt_fruits+mnt_meat+mnt_fish+mnt_sweet) as monetary
FROM definitiva
JOIN dayT ON definitiva.diaID = dayT.diaID
JOIN monthT ON monthT.mesID = dayT.mesID
JOIN quarter ON quarter.trimID = monthT.trimID
JOIN semester ON semester.semID = quarter.semID
JOIN yearT ON semester.anio = yearT.anio
WHERE yearT.anio = currentYear
GROUP BY marital_Status
ORDER BY category);
cursor_income CURSOR FOR(
SELECT (
CASE
WHEN income > 100000 THEN 'Income Range: 1) + de 100k'
WHEN income > 70000 THEN 'Income Range: 2) Entre 70k y 100k'
WHEN income > 30000 THEN 'Income Range: 3) Entre 30k y 70k'
WHEN income >= 10000 THEN 'Income Range: 4) Entre 10k y 30k'
ELSE 'Income Range: 5) Menos de 10k'
END
) AS category,
AVG(recency)as recency,
AVG(num_catalog_purchases+num_deals_purchases+num_stores_purchases+num_web_purchases) as frecuency,
AVG(mnt_wines+mnt_fruits+mnt_meat+mnt_fish+mnt_sweet) as monetary
FROM definitiva
JOIN dayT ON definitiva.diaID = dayT.diaID
JOIN monthT ON monthT.mesID = dayT.mesID
JOIN quarter ON quarter.trimID = monthT.trimID
JOIN semester ON semester.semID = quarter.semID
JOIN yearT ON semester.anio = yearT.anio
WHERE yearT.anio = currentYear
GROUP BY category
);
BEGIN
OPEN cursor_year;
LOOP
FETCH cursor_year INTO yearV, totalRecency,totalFrecuency, totalMonetary ;
EXIT WHEN NOT FOUND OR (years <= 0);
IF currentYear IS NULL THEN
BEGIN
raise info '-------------------------------------Consolidated Customer Report-------------------------------';
raise info 'Year-----Category--------------------------Recency-Frecuency-Monetary-------------------------';
END;
END IF;
raise info '--------------------------------------------------------------------------------------------------';
currentYear := yearV;
auxiChar := CAST( currentYear AS CHAR(4));
OPEN cursor_birth;
LOOP
FETCH cursor_birth INTO category2, recency2, frecuency2, monetary2;
EXIT WHEN NOT FOUND;
raise info '% % % % %', auxiChar, category2, recency2, frecuency2, monetary2;
auxiChar := '---';
END LOOP;
CLOSE cursor_birth;
OPEN cursor_education;
LOOP
FETCH cursor_education INTO category2, recency2, frecuency2, monetary2;
EXIT WHEN NOT FOUND;
raise info '% % % % %', auxiChar, category2, recency2, frecuency2, monetary2;
auxiChar := '---';
END LOOP;
CLOSE cursor_education;
OPEN cursor_income;
LOOP
FETCH cursor_income INTO category2, recency2, frecuency2, monetary2;
EXIT WHEN NOT FOUND;
raise info '% % % % %', auxiChar, category2, recency2, frecuency2, monetary2;
auxiChar := '---';
END LOOP;
CLOSE cursor_income;
OPEN cursor_maritalStatus;
LOOP
FETCH cursor_maritalStatus INTO category2, recency2, frecuency2, monetary2;
EXIT WHEN NOT FOUND;
raise info '% % % % %', auxiChar, category2, recency2, frecuency2, monetary2;
auxiChar := '---';
END LOOP;
CLOSE cursor_maritalStatus;
years := years-1;
raise info '------------------------------------------------------------------------------------% % %', totalRecency, totalFrecuency, totalMonetary;
END LOOP;
CLOSE cursor_year;
END
$$ LANGUAGE plpgsql;