Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL计数函数不工作_Sql_Count - Fatal编程技术网

SQL计数函数不工作

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

我想计算学费不等于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    
    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