我想使用SQL select语句有条件地返回列数据。我正在使用Oracle SQL Developer。
输出应如下所示:
REP_START_YEAR UNIVERSITY_CODE STUDENT_OEN OEN_COUNT 2012 UNIVERSITYA 999999999 3 2012 UNIVERSITYA 000000000 0 2012 UNIVERSITYA Other 2105
这是我到目前为止所提出的:
select enrol.Rep_Start_Year, uni.university_code, enrol.student_OEN, count(*) as Invalid_OEN_Count from (select enrol.Rep_Start_Year, uni.university_code, enrol.student_OEN, CASE WHEN enrol.student_OEN not in ('999999999','000000000') THEN 'Other' ELSE enrol.student_OEN END as student_OEN from edcs_unistat.ustat_enrolment_data enrol) inner join edcs_unistat.uni_institution_type uniinst on enrol.uni_institution_type_ID = uniinst.uni_institution_type_id inner join (select u.UNIVERSITY_ID, (case when U.UNIVERSITY_CODE = 'DOMC' then 'CARL' when U.UNIVERSITY_CODE = 'NOSM-LAKE' then 'NOSM' when U.UNIVERSITY_CODE = 'NOSM-LAUR' then 'NOSM' when U.UNIVERSITY_CODE = 'LAUR-ALGM' then 'ALGM' when U.UNIVERSITY_CODE = 'LAUR-HRST' then 'HRST' when U.UNIVERSITY_CODE = 'DOMC' then 'CARL' else U.UNIVERSITY_CODE end) as university_code from edcs_core.university u) uni on uniinst.university_id = uni.university_id inner join edcs_unistat.reg_status_type regstatus on enrol.reg_status_type_id = regstatus.reg_status_type_id inner join edcs_unistat.fee_cat_type feetyp on enrol.fee_cat_type_id = feetyp.fee_cat_type_id group by enrol.Rep_Start_Year, uni.university_code, enrol.student_OEN order by enrol.Rep_Start_Year, uni.university_code, enrol.student_OEN
答案 0 :(得分:0)
select Institution, Student_Number, count(*)
from (select Institution, CASE WHEN Student_Number not in ('999','000')
THEN "Other"
ELSE Student_Number
END as Student_Number
from Enrolment)
group by Institution, Student_Number;