-- Question 1
SELECT d.*
FROM Discipline d, Epreuve e
WHERE d.code=e.discipl AND e.dateE < '2021-08-01' AND e.dateE > '2021-06-30'
GROUP BY d.code
HAVING count(*) >=ALL (SELECT count(*)
FROM Epreuve
WHERE dateE >= '2021-08-01' AND dateE <= '2021-08-31' AND d.code=discipl);

-- code |          nom          
-- ------+-----------------------
--  FEN  | Fencing
--  JUD  | Judo
--  TKW  | Taekwondo
--  GTR  | Trampoline Gymnastics
--  TRI  | Triathlon
--  TTE  | Table Tennis
--  CRD  | Cycling Road
--  SKB  | Skateboarding
--  SWM  | Swimming
--  BK3  | 3x3 Basketball
--  SRF  | Surfing
--  ROW  | Rowing
--  RUG  | Rugby Sevens
--  BSB  | Baseball/Softball
--  DIV  | Diving
--  SHO  | Shooting
--  BMX  | Cycling BMX Racing
--  CSL  | Canoe Slalom
--  ARC  | Archery
--  TEN  | Tennis
--  WLF  | Weightlifting

-- Question 2
SELECT p.nom 
FROM pays p, Athlete a, Resultat r
WHERE 

-- Question 3
SELECT 

--Q1
SELECT d.*
FROM Discipline d, Epreuve e
WHERE d.code=e.discipl AND e.dateE < '2021-08-01' AND e.dateE > '2021-06-30'
GROUP BY d.code
HAVING count(*) >=ALL (SELECT count(*)
                        FROM Epreuve
                        WHERE dateE >= '2021-08-01' AND dateE <= '2021-08-31' AND d.code=discipl);
/* code |          nom          
------+-----------------------
 FEN  | Fencing
 JUD  | Judo
 TKW  | Taekwondo
 GTR  | Trampoline Gymnastics
 TRI  | Triathlon
 TTE  | Table Tennis
 CRD  | Cycling Road
 SKB  | Skateboarding
 SWM  | Swimming
 BK3  | 3x3 Basketball
 SRF  | Surfing
 ROW  | Rowing
 RUG  | Rugby Sevens
 BSB  | Baseball/Softball
 DIV  | Diving
 SHO  | Shooting
 BMX  | Cycling BMX Racing
 CSL  | Canoe Slalom
 ARC  | Archery
 TEN  | Tennis
 WLF  | Weightlifting
 MTB  | Cycling Mountain Bike
(22 lignes)
*/


SELECT p.nom FROM Pays p WHERE (SELECT COUNT(*) FROM Athlete a WHERE a.pays = p.code) * 0.5 <= (SELECT COUNT(DISTINCT r.athlete) FROM Resultat r 
WHERE r.medaille IS NOT NULL AND r.athlete IN (SELECT a.code FROM Athlete a WHERE a.pays = p.code));
/*        nom         
--------------------
 Bermuda
 San Marino
 Fiji
 Russian Federation
 URSS
(5 lignes)
*/


/*a faire sans left join*/
SELECT Pays.nom FROM Pays JOIN Athlete ON Athlete.pays = Pays.code JOIN Resultat ON Resultat.athlete = Athlete.code 
JOIN Medaille ON Medaille.place = Resultat.medaille AND Medaille.couleur = 'Or' 
LEFT JOIN Medaille MedailleArgent ON MedailleArgent.place = Resultat.medaille AND MedailleArgent.couleur = 'Argent'
LEFT JOIN Medaille MedailleBronze ON MedailleBronze.place = Resultat.medaille AND MedailleBronze.couleur = 'Bronze' GROUP BY Pays.nom
HAVING COUNT(Medaille.place) > COUNT(MedailleArgent.place) + COUNT(MedailleBronze.place);
/*           nom             
----------------------------
 Australia
 Austria
 Bahamas
 Belarus
 Belgium
 Bermuda
 Brazil
 Bulgaria
 Canada
 Chinese Taipei
 Croatia
 Cuba
 Czech Republic
 Denmark
 Ecuador
*/


SELECT a.nom, a.prenom FROM Athlete a WHERE a.sexe = 'F' AND a.taille > ALL ( SELECT a2.taille FROM Athlete a2 WHERE a2.sexe = 'M' AND a2.pays = a.pays);
/*Marche pas ou retourne rien*/


SELECT a.nom, a.prenom, a.taille, t.max_taille FROM Athlete a JOIN (SELECT pays, MAX(taille) AS max_taille FROM Athlete WHERE sexe = 'M' AND pays = 'FRA' 
GROUP BY pays) t ON a.pays = t.pays WHERE a.sexe = 'M' AND a.pays = 'FRA';
/*       nom        |     prenom      | taille | max_taille 
------------------+-----------------+--------+------------
 ABALO            | Luc             |   1.83 |       2.18
 AIT SAID         | Samir           |   1.68 |       2.18
 ALBICY           | Andrew          |   1.78 |       2.18
 ALIEV            | Mourad          |        |       2.18
 AMDOUNI          | Morhad          |        |       2.18
 AMOROS           | Emile           |   1.78 |       2.18
 ANDRE            | Sylvain         |        |       2.18
 ANDRODIAS        | Matthieu        |   1.94 |       2.18
 ANNE             | Mame-Ibra       |        |       2.18
 APITHY           | Bolade          |        |       2.18
 ATSU             | Jonathan        |        |       2.18
*/


SELECT e.code, e.discipl, e.nom, e.dateE, MIN(e2.dateE) AS datemin, MAX(e2.dateE) AS datemax FROM Epreuve e JOIN Epreuve e2 ON e.discipl = e2.discipl 
GROUP BY e.code, e.discipl, e.nom, e.dateE ORDER BY e.dateE;
/* code | discipl |                     nom                      |   datee    |  datemin   |  datemax   
------+---------+----------------------------------------------+------------+------------+------------
 E002 | CRD     | Men's Road Race                              | 2021-07-24 | 2021-07-24 | 2021-07-28
 E004 | FEN     | Women's EpÃ©e Individual                      | 2021-07-24 | 2021-07-24 | 2021-08-01
 E007 | SHO     | 10m Air Pistol Men                           | 2021-07-24 | 2021-07-24 | 2021-08-02
 E009 | TKW     | Men -58kg                                    | 2021-07-24 | 2021-07-24 | 2021-07-27
 E008 | SHO     | 10m Air Rifle Women                          | 2021-07-24 | 2021-07-24 | 2021-08-02
 E003 | FEN     | Men's Sabre Individual                       | 2021-07-24 | 2021-07-24 | 2021-08-01
 E011 | WLF     | Women's 49kg                                 | 2021-07-24 | 2021-07-24 | 2021-08-04
 E010 | TKW     | Women -49kg                                  | 2021-07-24 | 2021-07-24 | 2021-07-27
 E006 | JUD     | Women -48 kg                                 | 2021-07-24 | 2021-07-24 | 2021-07-31
 E001 | ARC     | Mixed Team                                   | 2021-07-24 | 2021-07-24 | 2021-07-31
*/


UPDATE Epreuve SET dateE = dateE + INTERVAL '7' DAY WHERE dateE = (SELECT MAX(dateE) FROM Epreuve);
/*UPDATE 13*/


/*JSP MON REUF*/


INSERT INTO Epreuve (code, discipl, nom, dateE)
VALUES ('E356', 'GLF', 'Women''s team', '2021-08-09');
/*INSERT 0 1*/
