SQL计数函数不工作
我想计算学费不等于99999.99的实例数,但我很难让SQL count函数正常工作。我有以下疑问:SQL计数函数不工作,sql,count,Sql,Count,我想计算学费不等于99999.99的实例数,但我很难让SQL count函数正常工作。我有以下疑问: select year, college, case when tuition < 99999.99 then 'Other' else to_char(tuition) end tuition, count(*) as tuition_count from enrolment group by
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 100
2012 CollegeA Other 20
2012 CollegeA Other 3
2012 CollegeA 99999.99 456
您需要在group by语句中按所需内容分组。像这样:
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
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