简体   繁体   中英

Oracle SQL order by expresion

I'm trying to sort something by an expression and for some reason it won't work unless I have that expression as a selection :

    select distinct p.stuff
    from p.places 
    join otherPLACE
    order by cos(sin(to_number(p.nr_matricol)));

But I keep getting this error

ORA-01791: not a SELECTed expression

If I write it like this

select distinct 
    p.stuff,
    cos(sin(to_number(p.nr_matricol)))
from p.places 
join otherPLACE 
order by cos(sin(to_number(p.nr_matricol)));

it works but I don't want to have that column printed.

Is there a way I can make this work ?

You can wrap into inline view

SELECT a FROM
    (select distinct p.stuff a, cos(sin(to_number(p.nr_matricol))) b
     from p.places 
          join otherPLACE) T
ORDER BY b;

NOTE: I hope your code was pseudo-code. Because you-re having Cartesian join, unless you specifying columns

The problem is that the order by takes place after the select distinct . The only values available are those in the select . A typical approach would be aggregation.

Something like this:

select p.stuff
from places p join
     otherPLACE op
     on . . .
group by p.stuff
order by cos(sin(to_number(max(p.nr_matricol))));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM