计算表中的1&2和2

时间:2016-08-15 11:48:29

标签: sql

我有这张桌子

SECTION

由名为

的字段组成
Semester 
2
1
1
1
2
1
1
2
2
2
1
2

我需要sql来统计有多少1和2个

并且像这样出来

semester 1  |  semester 2
6           |   6

3 个答案:

答案 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