例如,我的表有数据 - 10名志愿者.有两种类型的志愿者 - 学生和工作人员.如何使用此视图在一个表中插入几列:
COUNT(Volunteer_id),COUNT(Volunteer_id)在哪里Volunteer_type ='学生',COUNT(Volunteer_id在哪里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'
Run Code Online (Sandbox Code Playgroud)
这个语句现在表示为行,但我想让它们成为列
使用条件聚合:
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
Run Code Online (Sandbox Code Playgroud)