我很难在最后一行显示列的总计。
SELECT AgeGroup, COUNT(CASE WHEN sex='Male' THEN 1 END) AS Male_Patients, COUNT(CASE WHEN sex='Female' THEN 1 END) AS Female_Patients, COUNT(*) AS Total
FROM(SELECT
CASE
WHEN age <1 THEN 'Under age 1'
WHEN age =1 THEN 'Age 1'
.........
WHEN age >= 85 THEN 'Age 85 AND over'
ELSE 'Unknown'
END AS AgeGroup,
CASE
WHEN age <1 THEN 0
..........
WHEN age >= 85 THEN 37
WHEN age IS NULL THEN 38
END as grp_order, sex
FROM (SELECT FLOOR(DATEDIFF('2019-06-30', p.DOB) / 365.25) as age, p.sex AS sex, e.id, e.date
FROM patient_data AS p
JOIN form_encounter AS e
ON p.id = e.id
WHERE e.date BETWEEN 'date' AND 'date'
GROUP BY e.id
) as Age_subquery
) AS AGE_Group_subquery
GROUP BY AgeGroup
ORDER BY grp_order
有没有办法像这样在底部包含列总计:
AgeGroup Male_Patients Female_Patients Total
Under age 1 100 150 250
..... ... ... ...
Age 85 AND over 50 50 100
Grand Total 150 200 350