Conditional order by clause

穿精又带淫゛_ 提交于 2021-01-29 10:32:25

问题


Below query returning error. Help me under the error and a way to achieve the conditional order by. I am trying to order by grade and name when grade >=8 and grade and marks when grade <8.

SELECT
    name, grade, marks
FROM
    students, grades
WHERE
    min_mark <= marks
    AND   marks <= max_mark
    AND   marks >= 70
UNION
SELECT
    TO_CHAR('NULL') AS name, grade, marks
FROM
    students, grades
WHERE
    min_mark <= marks
    AND   marks <= max_mark
    AND   marks <= 69
order by grade desc,(case when grade >= 8 
                     then  name 
                     when grade < 8 
                     then marks  
                     end );

ERROR:
order by grade desc,(case when grade >= 8
*
ERROR at line 18:
ORA-01785: ORDER BY item must be the number of a SELECT-list expression


回答1:


This appears to be bug 5695629, which seems to have been raised against 10g and doesn't seem to have been fixed yet (as of 12cR2; I don't have 18 to play with yet), which is unusual.

You can avoid it by wrapping the query in an outer select before ordering:

select name, grade, marks
from
(
    SELECT
        name, grade, marks
    FROM
        students, grades
    WHERE
        min_mark <= marks
        AND   marks <= max_mark
        AND   marks >= 70
    UNION
    SELECT
        TO_CHAR('NULL') AS name, grade, marks
    FROM
        students, grades
    WHERE
        min_mark <= marks
        AND   marks <= max_mark
        AND   marks <= 69
)
order by grade desc,case when grade >= 1 
                     then  name 
                     when grade < 1 
                     then  marks
                     end ;

But as name and marks are (presumably) different data types - string and number - that will instead get

ORA-00932: inconsistent datatypes: expected CHAR got NUMBER

You could convert marks to a string, but if you do then you need to pad it so sorting the resulting string alphabetically still matches the numeric order - messy but plausible since the marks can (again, presumably - if it's a percentage?) only be up to three digits:

select name, grade, marks
from
(
    ...
    <the main part of your query here as a subquery, as above>
    ...
)
order by grade desc,case when grade >= 8 
                     then  name 
                     when grade < 8 
                     then  to_char(marks, 'FM000')
                     end ;

db<>fiddle demo using some dummy data supplied via CTEs.

If the marks can be more than three digits then change the format mask to match the maximum possible length.


The TO_CHAR('NULL') part is also odd as that will give you the literal string "NULL" in the name column for those rows. Since you start with a string literal the TO_CHAR() part is pointless, just use 'NULL' AS name directly. If you actually want it to be blank then you can just use null AS name and it will match the data type of the matching column expression from the first branch of the union (and will pick up its alias too). You could explicitly cast to a string type, e.g. cast(null as varchar2(20)) AS name but there doesn't seem to be much point.




回答2:


There are 2 forms of CASE Expressions in SQL.

CASE <expression> WHEN <v1> THEN <ret1> WHEN <v2> THEN <ret2> ... END

The other is

CASE WHEN <comp1> THEN <ret1> WHEN <comp2> THEN <ret2> ... END

The difference is THAT IN CASE 1 you specify the value or expression to be compared once and compare it to other values or expressions, where as with variant 2, you specify a comparison expression for each WHEN.

Change the expression to

CASE WHEN grade >= 8 THEN name ELSE TO_CHAR(marks, '0999999999') END

I also formatted the marks number to a string with leading zeroes to make it sortable as text together with the name.



来源:https://stackoverflow.com/questions/50933385/conditional-order-by-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!