《SQL 进阶教程》 case:在 CASE 表达式中使用聚合函数
1.只加入一个社团的学生的社团id
select std_id, max(club_id) from
student_club
group by std_id
having count(*) =1
------------------------------
2.加入多个社团的学生的主社团id
select std_id,club_id from student_club where main_club_flag ='Y'
-------------------------------------
select std_id,
case when count(*) =1
then max(club_id)
else max(case when main_club_flag ='Y'
then club_id
else null end)
end as main_club
from student_club
group by std_id