通过在sql oracle

时间:2018-04-16 17:43:32

标签: oracle group-by count

我有这个问题:

SELECT * FROM PATIENT_MED_SPEC

给出了这个结果:

NUM_MALADE| NUM_MED| SPECIALITE
----------+--------+------------
3          53       Traumatologue
3          85       Anesthésiste
3          126      Radiologue
6          34       Pneumologue
6          85       Anesthésiste
6          114      Traumatologue
6          135      Anesthésiste
13         4        Orthopédiste
13         8        Cardiologue
13         114      Traumatologue
21         19       Traumatologue
21         64       Radiologue
21         135      Anesthésiste
23         4        Orthopédiste
23         8        Cardiologue
23         88       Cardiologue

我希望使用countgroup by子句

获得以下结果
NUM_MALADE| count_MED| count_SPEc
----------+----------+------------
3          3          3
6          4          3
13         3          3
21         3          3
23         3          2

即每MALADE(患者)我们都会计算MEDECIN (医生)号码和SPECIALITES有关的人数

1 个答案:

答案 0 :(得分:1)

您可以使用COUNT DISTINCT

SELECT MALADE, COUNT(*) AS count_MED, COUNT(DISTINCT SPECIALITE) AS num_SPEC
FROM PATIENT_MED_SPEC
GROUP BY MALADE