-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.sql
50 lines (39 loc) · 1.2 KB
/
Functions.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
DELIMITER $$
CREATE FUNCTION total_gasto(id_cliente INT, data_inicio DATETIME, data_fim DATETIME)
RETURNS FLOAT DETERMINISTIC
BEGIN
RETURN (SELECT sum(b.preco)
FROM bilhete as b
WHERE b.cliente = id_cliente AND b.data_aquisicao >= data_inicio AND b.data_aquisicao <= data_fim);
END $$
-- SELECT total_gasto(1, '2018-11-01', '2018-12-31');
DELIMITER $$
CREATE FUNCTION total_bilhetes_vendidos(d_inicio DATETIME, d_fim DATETIME)
RETURNS FLOAT DETERMINISTIC
BEGIN
RETURN (SELECT count(b.id_bilhete)
FROM bilhete as b
WHERE b.data_aquisicao >= d_inicio
AND b.data_aquisicao <= d_fim);
END $$
DELIMITER $$
CREATE FUNCTION total_faturado(d_inicio DATETIME, d_fim DATETIME)
RETURNS FLOAT DETERMINISTIC
BEGIN
RETURN (SELECT sum(b.preco)
FROM bilhete as b
WHERE b.data_aquisicao >= d_inicio
AND b.data_aquisicao <= d_fim);
END $$
DELIMITER $$
CREATE FUNCTION lugar_livre(classe CHAR(1), numero SMALLINT, id_viagem INT)
RETURNS BOOLEAN DETERMINISTIC
BEGIN
IF (SELECT b.id_bilhete
FROM bilhete AS b
WHERE b.viagem = id_viagem AND b.classe = classe AND b.numero = numero) > 0
THEN RETURN 0;
ELSE RETURN 1;
END IF;
END $$
-- DROP FUNCTION lugar_livre;