Oracle中的以下case语句给出了一个错误。请帮忙。错误是:ORA:00907。
select * from schools_master_table where TEST_SUBJECT_KEY in (case
when SCHOOL_YEAR<=2014 and TEST_GRADE_KEY in (3,4,5,6,7,8,10) THEN (1,2,3,4,5)
when SCHOOL_YEAR >2014 and TEST_GRADE_KEY in (10) THEN (1,3)
when SCHOOL_YEAR>2014 and TEST_GRADE_KEY in (3,4,5,6,7,8) THEN (1,2,3,4,5)
when SCHOOL_YEAR>2014 and TEST_TYPE_KEY in (11) THEN (17,18,19,20,21,22,23,24,25)
ELSE NULL
END)
答案 0 :(得分:0)
case expression syntax允许返回表达式和else表达式。这里重要的是需要an expression,“......评估值”。您尝试返回an expression list,而这在案例构造中无效。您可以通过一个简单的示例看到:
select case when 1=1 then (1,2) end from dual;
也得到ORA-00907:missing right parenthesis
并且错误被标记为逗号所在的位置 - 因为它期望看到围绕单个表达式而不是列表的集合中的右括号。
您可以使用OR
和AND
的组合,在其他条件匹配时,根据相应的列表检查TEST_KEY_SUBJECT
值:
select * from schools_master_table
where (SCHOOL_YEAR<=2014 and TEST_GRADE_KEY in (3,4,5,6,7,8,10)
and TEST_SUBJECT_KEY in (1,2,3,4,5))
or (SCHOOL_YEAR>2014 and TEST_GRADE_KEY in (10)
and TEST_SUBJECT_KEY in (1,3))
or (SCHOOL_YEAR>2014 and TEST_GRADE_KEY in (3,4,5,6,7,8)
and TEST_SUBJECT_KEY in (1,2,3,4,5))
or (SCHOOL_YEAR>2014 and TEST_TYPE_KEY in (11)
and TEST_SUBJECT_KEY in (17,18,19,20,21,22,23,24,25))