我有这张桌子
SECTION
由名为
的字段组成Semester
2
1
1
1
2
1
1
2
2
2
1
2
我需要sql来统计有多少1和2个
并且像这样出来
semester 1 | semester 2
6 | 6
答案 0 :(得分:3)
Demo here:测试后
select
sum(Case when semester=1 then 1 else 0 end) as 'Semester1',
sum(Case when semester=2 then 1 else 0 end) as 'Semester2'
from section
答案 1 :(得分:2)
试试这个:
Select sum(case when semster =1 then 1 else 0 end) as semster1 ,
sum(case when semster =2 then 1 else 0 end) as semster2
from section;
答案 2 :(得分:-1)
假设有多于1和2的值,并且值是整数:
SELECT semester, count(*)
FROM section
WHERE semester = 1 OR semester = 2
GROUP BY semester