简体   繁体   English

Oracle SQL按表达式排序

[英]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 ORA-01791:不是SELECTED表达式

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 . 问题在于, order by是在select distinct之后发生的。 The only values available are those in the select . 唯一可用的值是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))));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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