简体   繁体   English

如何在Oracle SQL中使用row_number()

[英]How to use row_number() in Oracle SQL

I have received error when running the code below - below is an example for your reference. 运行下面的代码时收到错误-以下示例供您参考。

BRDB.EXPORT SHIPMENT (database table) BRDB.EXPORT SHIPMENT(数据库表)

 SHPMNT_REF  | SHIPMENT_TYPE
    123      | EHO
    456      | EHO
    789      | EHO

BRDB.EVENT_CODE (database table) BRDB.EVENT_CODE(数据库表)

 FILE_NO  | REMARKS   EVENT_CODE
 123      | TEST0      SIR
 123      | TEST1      SIR
 123      | TEST2      SIR
 456      | TEST3      SIR
 789      | TEST4      EEO

What I want to show in report is like 我想在报告中显示的是

  FILE NO    |  REMARKS
     123     |  TEST0,TEST1,TEST2
     456     |  TEST3

Below is my coding 下面是我的编码

select min(X.SHPMNT_REF) as "House B/L #",
       listagg(case when SIR = 1 then X.REMARKS end, ',') within group (order by X.SHPMNT_REF) as "REMARKS(from SIR Event)"
FROM   (select ES.SHPMNT_REF,
               (select EE.REMARKS,
                       row_number() over (order by EE.FILE_NO)
                FROM   BRDB.EXPORT_EVENT EE
                where  EE.FILE_NO = ES.SHPMNT_REF
                and    EE.EVENT_CODE = 'SIR') as SIR
        from   BRDB.EXPORT_SHIPMENT ES)X
GROUP BY X.SHPMNT_REF

The following is the error i received. 以下是我收到的错误。

Multiple columns are returned from a subquery that is allowed only one column.. SQLCODE=-412, SQLSTATE=42823, DRIVER=4.19.49. 2) [Code: -727, SQL State: 56098]  An error occurred during implicit system action type "2". Information returned for the error includes SQLCODE "-412", SQLSTATE "42823" and message tokens "".

I get the idea that you want: 我知道您想要的是:

select ec.file_no,
       listagg(ec.remarks, ',') within group (order by ec.remarks) remarks
from event_code ec
where (ec.file_no, ec.event_code) not in
           (select SHPMNT_REF, SHIPMENT_TYPE
            from BRDB.EXPORT SHIPMENT
           )
      )
group by ec.file_no;

I don't see what row_number() has to do with your specified results. 我看不到row_number()与您指定的结果有什么关系。

(select EE.REMARKS,
row_number() over (order by EE.FILE_NO) 
FROM BRDB.EXPORT_EVENT EE where EE.FILE_NO = ES.SHPMNT_REF 
                            and EE.EVENT_CODE = 'SIR'
) as Sir

this upper query is returned multiple column but you used alias Sir which is incorrect so it returned error 此上层查询返回多列,但您使用的是Sir ,这是不正确的,因此返回了错误

you can use join to replce subquery 您可以使用联接替换子查询

select 
min(X.SHPMNT_REF) as "House B/L #",

listagg(case when SIR = 1 then X.REMARKS
end, ',') within group (order by X.SHPMNT_REF) as "REMARKS(from SIR Event)"

FROM
(select
ES.SHPMNT_REF,

row_number() over (order by EE.FILE_NO)  
  as SIR

from BRDB.EXPORT_SHIPMENT ES join BRDB.EXPORT_EVENT EE
on EE.FILE_NO = ES.SHPMNT_REF where EE.EVENT_CODE = 'SIR'
)X
GROUP BY X.SHPMNT_REF

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

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