Bonjour
J'ai une base de données permettant la gestion entre les medecins et leurs patients et contenant les tables suivantes :
PATIENT (numAs, nomp, prenomp, sexe, dateNaiss, matriculem#)
MEDECIN (matricule, nomm, prenomm, specialite, ville, adr)
CONSULTE (matricule#, numAs#, date, diagnostic,numOrd#)
indication: matriculem est une clè étrangère qui a pour référence medecin(matricule)
je dois écrire la requête SQL qui permet de : "Afficher le nom et le prénom du ou des médecins qui sont médecins traitants du plus grand
nombre de patients "
quand j'écris :
--> cela ne fonctionne pasCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 SELECT nomm,prenomm,count(numas) FROM patient,medecin GROUP BY matriculem HAVING ( matriculem = matricule AND count(numas) = ( SELECT max(champ) FROM ( SELECT count(numas) AS champ FROM patient GROUP BY matriculem ) y ) );
mais quand j'écris (en suivant les messages de postgres)
;Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 SELECT nomm,prem,count(numas) FROM patient,medecin GROUP BY matriculem, medecin.nomm, medecin.prenomm, medecin.matricule HAVING ( matriculem = matricule AND count(numas) = ( SELECT max(champ) FROM ( SELECT count(numas) AS champ FROM patient GROUP BY matriculem ) y ) )
--> cela fonctionne très bien
cependant je ne vois pas pourquoi j'ai à ajouter au 'group by' le nomm le prenomm et la matricule de la table medecin.
quelqu'un pourrait m'expliquer cela ? et aussi,j'aimerais bien savoir s'il y a une façon plus simple(plus claire) pour écrire cette requête.