-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcedures.sql
More file actions
133 lines (114 loc) · 3.78 KB
/
Procedures.sql
File metadata and controls
133 lines (114 loc) · 3.78 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
-- PROCEDIMIENTO 1: Análisis completo de rendimiento de piloto por temporada
-- Propósito: Genera estadísticas detalladas de un piloto en una temporada específica
DELIMITER $$
CREATE OR REPLACE PROCEDURE sp_analisis_piloto_temp(
IN p_driver_id INT,
IN p_year INT
)
BEGIN
DECLARE v_total_races INT DEFAULT 0;
DECLARE v_championship_position INT DEFAULT 0;
DECLARE v_driver_fullname VARCHAR(100) DEFAULT '';
SELECT COUNT(ra.raceId) INTO v_total_races
FROM races ra
JOIN results r ON ra.raceId = r.raceId
AND r.driverId = p_driver_id
AND ra.`year` = p_year;
IF v_total_races = 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'El piloto no participó en esa temporada';
END IF;
DROP TEMPORARY TABLE IF EXISTS tbt_analisis_piloto_temp;
CREATE TEMPORARY TABLE tbt_analisis_piloto_temp (
race_name VARCHAR(50),
circuit_name VARCHAR(50),
laps_completed INT,
grid_position INT,
final_position INT,
points_earned DECIMAL(8,2),
end_status VARCHAR(50)
);
INSERT INTO tbt_analisis_piloto_temp
SELECT ra.`name`, c.`name`, r.laps, r.grid, r.`position`, r.points, s.`status`
FROM results r
JOIN races ra ON r.raceId = ra.raceId
JOIN circuits c ON ra.circuitId = c.circuitId
JOIN `status` s ON r.statusId = s.statusId
WHERE r.driverId = p_driver_id
AND ra.`year` = p_year
ORDER BY ra.`round`;
SELECT CONCAT(d.forename, ' ', d.surname), ds.`position`
INTO v_driver_fullname, v_championship_position
FROM driver_standings ds
JOIN races ra ON ds.raceId = ra.raceId
JOIN drivers d ON ds.driverId = d.driverId
WHERE ds.driverId = p_driver_id AND ra.`year` = p_year
ORDER BY ra.round DESC
LIMIT 1;
SELECT
v_driver_fullname AS piloto,
v_total_races as carreras_disputadas,
v_championship_position as posicion_campeonato,
SUM(points_earned) as puntos_totales,
COUNT(CASE WHEN final_position = 1 THEN 1 END) as victorias,
COUNT(CASE WHEN final_position BETWEEN 1 AND 3 THEN 1 END) as podios,
COUNT(CASE WHEN final_position <= 10 THEN 1 END) as puntos_finish,
AVG(grid_position) as promedio_salida,
AVG(final_position) as promedio_llegada,
COUNT(CASE WHEN end_status = 'Finished' THEN 1 END) as Crs_terminadas
FROM tbt_analisis_piloto_temp;
SELECT * FROM tbt_analisis_piloto_temp;
DROP TEMPORARY TABLE tbt_analisis_piloto_temp;
END$$
CALL sp_analisis_piloto_temp(1, 2009);
-- obtener el podio de una carrera
DELIMITER //
CREATE FUNCTION ObtenerPodio(
p_raceId INT
) RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE podio VARCHAR(255);
SELECT GROUP_CONCAT(
CONCAT(d.forename, ' ', d.surname)
ORDER BY r.positionOrder SEPARATOR ', '
) INTO podio
FROM results r
JOIN drivers d ON r.driverId = d.driverId
WHERE r.raceId = p_raceId
AND r.positionOrder BETWEEN 1 AND 3;
RETURN podio;
END//
DELIMITER ;
-- 3. FUNCIÓN: Calcular diferencia de tiempo con el ganador
-- PROPÓSITO: Automatizar el cálculo de la diferencia en milisegundos entre un piloto y el ganador de una carrera
DELIMITER //
CREATE FUNCTION calcular_diferencia_ganador(
p_race_id INT,
p_driver_id INT
) RETURNS INT
READS SQL DATA
DETERMINISTIC
BEGIN
DECLARE tiempo_ganador INT DEFAULT 0;
DECLARE tiempo_piloto INT DEFAULT 0;
DECLARE diferencia INT DEFAULT 0;
SELECT COALESCE(r.miliseconds, 0)
INTO tiempo_ganador
FROM results r
WHERE r.raceId = p_race_id
AND r.position = 1
LIMIT 1;
SELECT COALESCE(r.miliseconds, 0)
INTO tiempo_piloto
FROM results r
WHERE r.raceId = p_race_id
AND r.driverId = p_driver_id
LIMIT 1;
IF tiempo_ganador > 0 AND tiempo_piloto > 0 THEN
SET diferencia = tiempo_piloto - tiempo_ganador;
ELSE
SET diferencia = -1;
END IF;
RETURN diferencia;
END //
DELIMITER ;