-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesafio_triggers.sql
More file actions
127 lines (96 loc) · 4.66 KB
/
Copy pathdesafio_triggers.sql
File metadata and controls
127 lines (96 loc) · 4.66 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
/*
O desafio é composto de duas partes:
Caso o instrutor inserido receba acima da média, cancele a instrução, ou seja, não permita que a inserção do instrutor ocorra.
Depois... Desfaça o desafio anterior.
Caso o instrutor inserido receba mais do que 100% dos instrutores existentes, modifique a inserção para que ele passe a receber o mesmo que o instrutor mais bem pago.
*/
CREATE OR REPLACE FUNCTION cria_instrutor() RETURNS trigger AS $$
DECLARE
media_salarial DECIMAL;
instrutores_recebem_menos INTEGER DEFAULT 0;
total_instrutores INTEGER DEFAULT 0;
salario DECIMAL;
maior_salario DECIMAL;
percentual DECIMAL (5, 2);
BEGIN
SELECT AVG(instrutor.salario) INTO media_salarial FROM instrutor WHERE id <> NEW.id;
IF NEW.salario > media_salarial THEN
INSERT INTO log_instrutores (informacao) VALUES (NEW.nome || ' recebe acima da média!');
END IF;
FOR salario IN SELECT instrutor.salario FROM instrutor WHERE id <> NEW.salario LOOP
total_instrutores := total_instrutores + 1;
IF NEW.salario > salario THEN
instrutores_recebem_menos := instrutores_recebem_menos + 1;
END IF;
END LOOP;
percentual = instrutores_recebem_menos::DECIMAL / total_instrutores::DECIMAL * 100;
-- Condição do desafio
IF percentual >= 100 THEN
SELECT MAX(instrutor.salario) INTO maior_salario FROM instrutor WHERE id <> NEW.id;
NEW.salario := maior_salario;
END IF;
INSERT INTO log_instrutores(informacao)
VALUES (NEW.nome || ' recebe mais do que ' || percentual || '% da grade de instrutores');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
----------------------------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION cria_instrutor() RETURNS trigger AS $$
DECLARE
media_salarial DECIMAL;
instrutores_recebem_menos INTEGER DEFAULT 0;
total_instrutores INTEGER DEFAULT 0;
salario DECIMAL;
maior_salario DECIMAL;
percentual DECIMAL (5, 2);
BEGIN
SELECT AVG(instrutor.salario) INTO media_salarial FROM instrutor WHERE id <> NEW.id;
-- Condição do desafio
SELECT MAX(instrutor.salario) INTO maior_salario FROM instrutor WHERE id <> NEW.id;
IF NEW.salario > maior_salario THEN
NEW.salario := maior_salario;
ELSIF NEW.salario > media_salarial THEN
INSERT INTO log_instrutores (informacao) VALUES (NEW.nome || ' recebe acima da média');
ELSE
INSERT INTO log_instrutores (informacao) VALUES (NEW.nome || ' recebe abaixo da média');
END IF;
FOR salario IN SELECT instrutor.salario FROM instrutor WHERE id <> NEW.id LOOP
total_instrutores := total_instrutores + 1;
IF NEW.salario > salario THEN
instrutores_recebem_menos := instrutores_recebem_menos + 1;
END IF;
END LOOP;
percentual = instrutores_recebem_menos::DECIMAL / total_instrutores::DECIMAL * 100;
INSERT INTO log_instrutores (informacao)
VALUES (NEW.nome || ' recebe mais do que' || percentual || '% da grade de instrutores');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-----------------------------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION cria_instrutor() RETURNS trigger AS $$
DECLARE
media_salarial DECIMAL;
instrutores_recebem_menos INTEGER DEFAULT 0;
total_instrutores INTEGER DEFAULT 0;
salario DECIMAL;
maior_salario DECIMAL;
percentual DECIMAL (5, 2);
BEGIN
SELECT AVG(instrutor.salario) INTO media_salarial FROM instrutor WHERE id <> NEW.id;
IF NEW.salario > media_salarial THEN
INSERT INTO log_instrutores (informacao) VALUES (NEW.nome || ' recebe acima da média!');
END IF;
FOR salario IN SELECT instrutor.salario FROM instrutor WHERE id <> NEW.salario LOOP
total_instrutores := total_instrutores + 1;
IF NEW.salario > salario THEN
instrutores_recebem_menos := instrutores_recebem_menos + 1;
END IF;
END LOOP;
percentual = instrutores_recebem_menos::DECIMAL / total_instrutores::DECIMAL * 100;
-- Condição do desafio
ASSERT percentual < 100::DECIMAL;
INSERT INTO log_instrutores(informacao)
VALUES (NEW.nome || ' recebe mais do que ' || percentual || '% da grade de instrutores');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;