获取该列的总和。

huangapple go评论57阅读模式
英文:

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, &#39;Total&#39;) AS Especialidad,
       COUNT(p.ClaveConsultorio) AS Paciente
FROM CONSULTORIOS c
INNER JOIN pacientes p ON c.ClaveConsultorio = p.ClaveConsultorio
GROUP BY ROLLUP(c.Especialidad);

huangapple
  • 本文由 发表于 2023年2月14日 09:45:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定