You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
IUT/2A/BDD/tp/s1/requetesTP4-5-6.sql

172 lines
5.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

-- ? 1 - Écrire une fonction qui pour un match donné calcule le nombre de rebonds
-- ? pris par les joueurs qui nont pas débuté la rencontre. Proposez deux versions,
-- ? une utilisant les fonctions dagrégation et une autre utilisant un curseur mais aucune fonction dagrégation.
CREATE OR REPLACE FUNCTION calcul_rebonds(match Game.id%TYPE) RETURNS integer AS $$
DECLARE
sumRebounds integer;
BEGIN
SELECT sum(gd.rebounds) INTO sumRebounds
FROM GameDetail gd, Game g
WHERE g.id = match AND gd.idGame = g.id AND gd.startPosition is NULL;
RETURN sumRebounds;
END;
$$ LANGUAGE plpgsql;
SELECT calcul_rebonds('22100979');
-- CREATE FUNCTION
-- calcul_rebonds
-- ----------------
-- 32
-- (1 row)
CREATE OR REPLACE FUNCTION calcul_rebonds_curs(match Game.id%TYPE) RETURNS GameDetail.rebounds%TYPE AS $$
DECLARE
sumRebounds GameDetail.rebounds%TYPE := 0;
rbs GameDetail.rebounds%TYPE := 0;
curs cursor FOR SELECT rebounds
FROM GameDetail
WHERE idGame = match AND startPosition is NULL AND rebounds is NOT NULL;
BEGIN
OPEN curs;
FETCH curs INTO rbs;
WHILE FOUND LOOP
sumRebounds = sumRebounds + rbs;
FETCH curs INTO rbs;
END LOOP;
CLOSE curs;
RETURN sumRebounds;
END;
$$ LANGUAGE plpgsql;
SELECT calcul_rebonds_curs('22100979');
-- CREATE FUNCTION
-- calcul_rebonds_curs
-- ---------------------
-- 32
-- (1 row)
-- ? 2 - Vérifier que vos deux versions retournent les mêmes résultats en affichant le nombre total de rebonds
-- ? pris par les joueurs nayant pas débuté la rencontre pour tous les matchs ayant eu lieu le 12 mars 2022.
SELECT calcul_rebonds(id)
FROM Game
WHERE dateGame = '2022-03-12';
-- calcul_rebonds
-- ----------------
-- 39
-- 20
-- 43
-- 40
-- 41
-- 25
-- 24
-- (7 rows)
SELECT calcul_rebonds_curs(id)
FROM Game
WHERE dateGame = '2022-03-12';
-- calcul_rebonds_curs
-- ---------------------
-- 39
-- 20
-- 43
-- 40
-- 41
-- 25
-- 24
-- (7 rows)
-- ? 3 - Écrire une fonction qui calcule la même chose mais pour les joueurs ayant débuté la rencontre,
-- ? autrement dit qui calcule, pour un match donné, le nombre de rebonds pris par les joueurs ayant débuté la rencontre.
CREATE OR REPLACE FUNCTION calcul_rebonds_t(match Game.id%TYPE) RETURNS integer AS $$
DECLARE
sumRebounds integer;
BEGIN
SELECT sum(gd.rebounds) INTO sumRebounds
FROM GameDetail gd, Game g
WHERE g.id = match AND gd.idGame = g.id AND gd.startPosition is NOT NULL;
IF sumRebounds is NULL THEN
RETURN 0;
END IF;
RETURN sumRebounds;
END;
$$ LANGUAGE plpgsql;
SELECT calcul_rebonds_t('22100979');
-- CREATE FUNCTION
-- calcul_rebonds_t
-- ------------------
-- 57
-- (1 row)
CREATE OR REPLACE FUNCTION calcul_rebonds_curs_t(match Game.id%TYPE) RETURNS GameDetail.rebounds%TYPE AS $$
DECLARE
sumRebounds GameDetail.rebounds%TYPE := 0;
rbs GameDetail.rebounds%TYPE := 0;
curs cursor FOR SELECT rebounds
FROM GameDetail
WHERE idGame = match AND startPosition is NOT NULL AND rebounds is NOT NULL;
BEGIN
OPEN curs;
FETCH curs INTO rbs;
WHILE FOUND LOOP
sumRebounds = sumRebounds + rbs;
FETCH curs INTO rbs;
END LOOP;
CLOSE curs;
RETURN sumRebounds;
END;
$$ LANGUAGE plpgsql;
SELECT calcul_rebonds_curs_t('22100979');
-- CREATE FUNCTION
-- calcul_rebonds_curs_t
-- -----------------------
-- 57
-- (1 row)
SELECT calcul_rebonds_t('10300004');
SELECT calcul_rebonds_curs_t('10300004');
-- ? 4 - Trouver le match (abréviation des équipes et date) pendant lequel les joueurs
-- ? ayant débuté la rencontre ont réalisé le plus de rebonds.
SELECT t1.abbreviation, t2.abbreviation, g.dateGame, calcul_rebonds_t(g.id)
FROM Game g, Team t1, Team t2
WHERE g.idHomeTeam = t1.id AND g.idVisitorTeam = t2.id AND calcul_rebonds_t(g.id) = (SELECT max(calcul_rebonds_t(id))
FROM Game);
-- abbreviation | abbreviation | dategame | calcul_rebonds_t
-- --------------+--------------+------------+------------------
-- POR | DEN | 2019-05-03 | 101
-- (1 row)
-- ? 5 - Y a til des matchs pour lesquels les données sur les rebonds sont incohérentes?
-- ? Vérifier en comparant les valeurs obtenus grâce aux fonctions précédentes avec les valeurs contenues dans la table Game.
-- ! SELECT t1.abbreviation, t2.abbreviation, g.dateGame, (calcul_rebonds_t(g.id) + calcul_rebonds(g.id)) as fct, g.reboundsHome, g.reboundsAway
-- ! FROM Game g, Team t1, Team t2
-- ! WHERE g.idHomeTeam = t1.id AND g.idVisitorTeam = t2.id AND calcul_rebonds_t(g.id) = (SELECT max(calcul_rebonds_t(id))
-- ! FROM Game);