例如,我的表有数据 - 10 名志愿者。 志愿者有两种类型——学生和工作人员。 如何使用此 View 在一个表中插入几列:
COUNT(Volunteer_id), COUNT(Volunteer_id) WHERE Volunteer_type = '学生', COUNT(Volunteer_id WHERE Volunteer_type = '员工'
SELECT COUNT(Volunteer_id) AS "TOTAL VOLUNTEERS"
from volunteer
UNION
SELECT COUNT(Volunteer_id) AS "TOTAL VOLUNTEERS"
from volunteer
WHERE Volunteer_type = 'Staff'
UNION
SELECT COUNT(Volunteer_id) AS "TOTAL VOLUNTEERS"
from volunteer
WHERE Volunteer_type = 'Student'
这些语句目前表示为行,但我想让它们成为列
最佳答案
使用条件聚合:
SELECT
COUNT(*) AS "Total Volunteers",
COUNT(CASE WHEN Volunteer_tpye = 'Staff' THEN 1 END) AS "Staff Volunteers",
COUNT(CASE WHEN Volunteer_tpye = 'Student' THEN 1 END) AS Student
FROM volunteers
关于sql - COUNT 一个表的不同列中的值 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40834692/