我想计算学费不等于99999.99的实例数,但是我很难让SQL计数功能起作用。我有以下查询:
select
year, college,
case
when tuition < 99999.99 then 'Other'
else to_char(tuition)
end tuition,
count(*) as tuition_count
from
enrolment
group by
year, college, tuition
order by
year, college, tuition
我期待以下结果,“其他”的计数卷起。
YEAR COLLEGE TUITION TUITION_COUNT
---------------------------------------------
2012 CollegeA Other 123
2012 CollegeA 99999.99 456
相反,我得到了“其他”的多个实例,每个实例都有一个不同的学费值。
YEAR COLLEGE TUITION TUITION_COUNT
---------------------------------------------
2012 CollegeA Other 100
2012 CollegeA Other 20
2012 CollegeA Other 3
2012 CollegeA 99999.99 456
答案 0 :(得分:1)
您需要按(按组声明)分组您想要的内容。像这样:
select year, college, case
when tuition < 99999.99 then 'Other'
else to_char(tuition)
end as tuition, count(*) as tuition_count
from enrolment
group by year, college, case
when tuition < 99999.99 then 'Other'
else to_char(tuition)
end
order by year, college, case
when tuition < 99999.99 then 'Other'
else to_char(tuition)
end
这看起来更好:
select year, college, tuition, count(*) as tuition_count
from
(
select year, college, case
when tuition < 99999.99 then 'Other'
else to_char(tuition)
end as tuition
from enrolment
) subselect
group by year, college, tuition
order by year, college, tuition