如何在Oracle SQL中使用row_number()

时间:2019-02-27 11:38:28

标签: sql oracle

运行下面的代码时收到错误-以下是供您参考的示例。

BRDB.EXPORT SHIPMENT(数据库表)

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

BRDB.EVENT_CODE(数据库表)

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

我要在报告中显示的内容就像

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

下面是我的编码

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

以下是我收到的错误。

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 "".

2 个答案:

答案 0 :(得分:3)

我知道您想要的是

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;

我看不到row_number()与您指定的结果有什么关系。

答案 1 :(得分:0)

var i = 0;

// Load them with 
$(result).each(function () {
  // Appending the Tab Here                     
  $('#nav-tab').append('<a class="nav-item nav-link" id="nav-contact-tab' + i.toString() + '" data-toggle="tab" role="tab" aria-controls="nav-contact" aria-selected="false">' + this.TAB_CONTENT + '</a>');

  // Some code to execute if we click on the tab
  $('#nav-contact-tab' + i.toString()).click(function() {
    // This is where my problem is: 
    // I need to make the assignment below as a value.
    daTabID = this.TABLE_NAME;
    alert(this.TABLE_NAME);
    LoadReport();
    alert('Report Loaded done');
  });

  i++;

  alert(i);
});

此上部查询返回多列,但您使用了别名(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 ,该别名不正确,因此返回了错误

您可以使用join替换子查询

Sir