英文:
Get the sum of the column
问题
获取Patient
列的总和,并按DESC
顺序排序,不考虑SUM行。
查询如下:
SELECT
C.Especialidad, COUNT(P.ClaveConsultorio) AS Paciente
FROM
CONSULTORIOS C
INNER JOIN
pacientes P ON C.ClaveConsultorio = P.ClaveConsultorio
GROUP BY
C.Especialidad
UNION
SELECT
'Total', COUNT(P.ClaveConsultorio) AS Paciente
FROM
CONSULTORIOS C
INNER JOIN
pacientes P ON C.ClaveConsultorio = P.ClaveConsultorio
ORDER BY
2 DESC
英文:
Get the sum of the Patient
column and sort in DESC
order without taking into account the SUM row
The query is the following:
SELECT
C.Especialidad, COUNT(P.ClaveConsultorio) AS Paciente
FROM
CONSULTORIOS C
INNER JOIN
pacientes P ON C.ClaveConsultorio = P.ClaveConsultorio
GROUP BY
C.Especialidad
UNION
SELECT
'Total', COUNT(P.ClaveConsultorio) AS Paciente
FROM
CONSULTORIOS C
INNER JOIN
pacientes P ON C.ClaveConsultorio = P.ClaveConsultorio
ORDER BY
2 DESC
答案1
得分: 3
我们可以在这里尝试使用ROLLUP进行分组:
SELECT COALESCE(c.Especialidad, 'Total') AS Especialidad,
COUNT(p.ClaveConsultorio) AS Paciente
FROM CONSULTORIOS c
INNER JOIN pacientes p ON c.ClaveConsultorio = p.ClaveConsultorio
GROUP BY ROLLUP(c.Especialidad);
英文:
We can try a group by with rollup here:
<!-- language: sql -->
SELECT COALESCE(c.Especialidad, 'Total') AS Especialidad,
COUNT(p.ClaveConsultorio) AS Paciente
FROM CONSULTORIOS c
INNER JOIN pacientes p ON c.ClaveConsultorio = p.ClaveConsultorio
GROUP BY ROLLUP(c.Especialidad);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论