我在Oracle中进行查询,使用ORDER BY进行大小写切换。代码如下:
SELECT Students.ID, (CASE WHEN Grade <8 THEN 'NULL' ELSE
Students.Name END CASE), Students.Marks, Grades.Grade
FROM Students LEFT JOIN Grades ON Students.Marks >= Grades.Min_Mark
AND Students.Marks <= Grades.Max_Mark
ORDER BY(
(
CASE WHEN
Grade <8 THEN Grade
END
)ASC + (
CASE WHEN
Grade >=8 THEN Grade, Name
END
)DESC
);
但是,我收到一条错误消息“ORA-00907:缺少右括号”。关于我可能做错什么的任何想法?我真的没有看到任何不匹配的括号。
答案 0 :(得分:0)
在“by”
之前删除第一个paréntesis答案 1 :(得分:0)
我想你想要:
ORDER BY (CASE WHEN Grade < 8 THEN Grade END) ASC,
(CASE WHEN Grade >= 8 THEN Grade END) DESC,
Name
您至少有三个错误:
ORDER BY
密钥只能包含一个ASC
或DESC
,并且位于密钥表达式的末尾。CASE
。CASE
表达式只能返回一个值。答案 2 :(得分:0)
您可以通过CASE中定义的内容进行排序,但您无法选择升序或降序。您似乎需要提取您的数据等级&lt;分别为8和等级&gt; = 8,按照您的意愿排序,然后将它们合并在一起:
SELECT Students.ID,
'NULL' AS STUDENT_NAME,
Students.Marks,
Grades.Grade
FROM Students
LEFT OUTER JOIN Grades
ON Students.Marks >= Grades.Min_Mark AND
Students.Marks <= Grades.Max_Mark
WHERE GRADE < 8
ORDER BY GRADE ASC
UNION ALL
SELECT Students.ID,
Students.Name AS STUDENT_NAME,
Students.Marks,
Grades.Grade
FROM Students
LEFT OUTER JOIN Grades
ON Students.Marks >= Grades.Min_Mark AND
Students.Marks <= Grades.Max_Mark
WHERE GRADE < 8
ORDER BY GRADE || NAME DESC;
祝你好运。