简体   繁体   English

SELECT子查询中的ORACLE ORDER BY

[英]ORACLE ORDER BY in SELECT subquery

I want to select a varchar field SUBJECT in a subquery, ordered by a date field DATETO. 我想在子查询中选择一个varchar字段SUBJECT,按日期字段DATETO排序。 Specific: the ONE subject field of the latest entry, therefore ordered by the date field. 具体:最新条目的一个主题字段,因此按日期字段排序。

SELECT MDKAMVP.MDKAMDA_SID, 
(select SUBJECT from mdkndlst where rownum=1 and SUBJECT is not null 
order by DATETO) NAKTION, MDKAMVP.ROWID ROW_ID 
FROM SPRINGV2.MDKAMVP order by MDKAMVP.INDSTATUS

Error: 错误:

ORA-00907: Rechte Klammer fehlt
00907. 00000 -  "missing right parenthesis"
*Cause:    
*Action:
Fehler in Zeile: 8 Spalte: 99

It works without the order by DATETO 它无需order by DATETOorder by DATETO

In Oracle 12c , you can use the new FETCH FIRST clause: Oracle 12c中 ,可以使用新的FETCH FIRST子句:

SELECT
    mdkamvp.mdkamda_sid,
    (SELECT subject
        FROM mdkndlst
        WHERE subject IS NOT NULL
        ORDER BY dateto
        FETCH FIRST 1 ROW ONLY) naktion,
    mdkamvp.rowid row_id
FROM springv2.mdkamvp
ORDER BY mdkamvp.indstatus

Assigning rownum to a row is done before applying ORDER BY clause, so this query 在应用ORDER BY子句之前rownum分配给一行,因此此查询

SELECT subject
FROM   mdkndlst
WHERE  rownum = 1
       AND subject IS NOT NULL
ORDER  BY dateto

in fact returns a random row. 实际上会返回一个随机行。 I believe this is not what you wanted. 我相信这不是您想要的。 You probably wanted first to apply ORDER BY and then take the first row. 您可能想先应用ORDER BY ,然后再进行第一行。 That's where subqueries may come in handy: 子查询可能会派上用场:

SELECT subject
FROM   (SELECT subject
        FROM   mdkndlst
        WHERE  subject IS NOT NULL
        ORDER  BY dateto)
WHERE  rownum = 1

and such query you may now use as a scalar subquery 这样的查询现在可以用作标量子查询

SELECT mdkamvp.mdkamda_sid
      ,(SELECT subject
        FROM   (SELECT subject
                FROM   mdkndlst
                WHERE  subject IS NOT NULL
                ORDER  BY dateto)
        WHERE  rownum = 1) naktion
      ,mdkamvp.rowid row_id
FROM   springv2.mdkamvp
ORDER  BY mdkamvp.indstatus

you should use first_value 您应该使用first_value

SELECT mdkamvp.mdkamda_sid,

  (SELECT DISTINCT first_value(subject) over (partition BY subject
                                              ORDER BY dateto DESC)
   FROM mdkndlst
   WHERE subject IS NOT NULL ) naktion,
       mdkamvp.rowid row_id
FROM springv2.mdkamvp
ORDER BY mdkamvp.indstatus

Instead of using a scalar sub-query in the select list, use a derived table where you don't have the limitation with the order by: 不要在选择列表中使用标量子查询,而应使用派生表,在该表中您不受顺序的限制:

SELECT MDKAMVP.MDKAMDA_SID, 
       x.subject as NAKTION, 
       MDKAMVP.ROWID ROW_ID 
FROM SPRINGV2.MDKAMVP 
  cross join (
    select SUBJECT 
    from mdkndlst 
    where rownum=1 
      and SUBJECT is not null 
    order by DATETO DESC
  ) x
order by MDKAMVP.INDSTATUS;

As the derived table is guaranteed to return exactly one row, the cross join will not change the overall result. 由于可以确保派生表仅返回一行,因此cross join不会更改整体结果。

If you want the latest subject you should also order the DATETO descending. 如果您想要最新的主题,还应该将DATETO降序排列。 Without that you will get the oldest subject. 没有这个,您将获得最古老的学科。

If you are using 12c then you might want to use the fetch first option as Jiri has shown. 如果您使用的是12c,则可能要使用Jiri所示的fetch first选项。

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

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