oracle ORA-01785:ORDER BY项必须是SELECT列表表达式的编号

w80xi6nr  于 2023-11-17  发布在  Oracle
关注(0)|答案(3)|浏览(156)

我有一个简单的查询,它给了我一个错误
错误(2713,12):PL/SQL:ORA-00907:缺少右括号

SELECT XMLAgg(Case When (Select xx_info_n26 From xxsample_table Where xx_info_v1 = 'H' and xx_info_v27 = 'AR' ) > 0 Then 
      (SELECT XMLAGG(XMLCONCAT(XMLElement("tns:SprzedazWiersz",                                          
         xmlelement("tns:LpSprzedazy"      ,   rownum),
         xmlelement("tns:KodKrajuNadaniaTIN",  substr(ar_trx.tax_reg_num, 0, 2)),
         xmlelement("tns:NrKontrahenta"    ,   substr(ar_trx.tax_reg_num, 2)), 
         xmlelement("tns:NazwaKontrahenta" ,   ar_trx.cust_name),
         xmlelement("tns:DowodSprzedazy"   ,   ar_trx.trx_number),
         xmlelement("tns:DataWystawienia"  ,   TO_CHAR(ar_trx.invoice_date,'RRRR-MM-DD')),      
         Case When ar_trx.sales_date is not null Then xmlelement("tns:DataSprzedazy"    ,   TO_CHAR(ar_trx.sales_date,'RRRR-MM-DD')) END,
         -- DocumentType
         (Select  xmlelement("tns:TypDokumentu" , doctype.Document_type_code)
            from    (select distinct xx_info_n3 trx_id
                        ,   xx_info_v15         Document_type_code
                     From   xxsample_table
                     Where  xx_info_v1 = 'AR'
                     and    xx_info_v16 = 'ORA_JEPL_DOCUMENT_TYPE') doctype
            where    doctype.trx_id  = ar_trx.trx_id),  
        -- Transaction Attributes
        (Select xmlagg(xmlconcat(xmlelement(evalname(trdattr.TRANSACTION_ATTRIBUTE_CODE), 1)))
         From    ( Select distinct xx_info_v12 TRANSACTION_ATTRIBUTE_CODE
                   From   xxsample_table
                   Where  xx_info_V1 = 'AR'
                   and    xx_info_v12 <> 'tns:'
                   and    xx_info_n3 = ar_trx.trx_id 
                   UNION ALL
                   Select xx_info_v20 TRANSACTION_ATTRIBUTE_CODE
                   From   xxsample_table 
                   Where  xx_info_V1 = 'AR'
                   and    xx_info_n3 = ar_trx.trx_id 
                   and    xx_info_v20 <> 0) trdattr
        order by 
        case TRANSACTION_ATTRIBUTE_CODE 
        when 'tns:SW'       then 1
        when 'tns:EE'       THEN 2
        when 'tns:TP'       THEN 3
        when 'tns:TT_WNT'   then 4
        when 'tns:TT_D'     THEN 5
        when 'tns:MR_T'     THEN 6
        when 'tns:MR_UZ'    THEN 7
        when 'tns:I_42'     THEN 8
        when 'tns:I_63'     THEN 9
        when 'tns:B_SPV'    THEN 10
        when 'tns:B_SPV_DOSTAWA' THEN 11
        when 'tns:B_MPV_PROWIZJA' THEN 12
        when 'tns:MPP' THEN 13
        ELSE 14 END)   
        ,
        -- Kboxes
       (Select xmlagg(xmlconcat(xmlelement(evalname(ar_tax_sum.box),ar_tax_sum.amount)))
      From ( select box
                ,   sum(amount) amount
            from    (Select xx_info_v5      box
                        ,   Sum(xx_info_n1) amount
                    From    xxsample_table 
                    Where   xx_info_V1 = 'AR'
                    and     xx_info_n3 = ar_trx.trx_id
                    Group By xx_info_v5
                    UNION
                    Select  xx_info_v6      box
                         ,  Sum(xx_info_n2) amount        
                    From    xxsample_table 
                    Where   xx_info_V1 = 'AR'
                    and     xx_info_n3 = ar_trx.trx_id
                    Group By xx_info_v6)
            Where box <> 'tns:'
            group by box) ar_tax_sum
      )                                     
        ))
       )
         From ( Select distinct xx_info_n3 trx_id
                              , xx_info_v2 trx_number
                              , xx_info_d1 sales_date
                              , xx_info_d3 invoice_date
                              , xx_info_v3 cust_name
                              , xx_info_v17 cust_addr
                              , xx_info_v7  tax_reg_num
                 From xxsample_table
                 Where xx_info_v1 = 'AR'
                 Order by 3,4
              ) ar_trx
         ) End)
FROM DUAL)

字符串
我认为这个错误是由条件ORDER BY引起的,因为当我完全删除ORDER BY(第31-46行)时,查询工作正常。然而,我很困惑为什么当我可以自己运行子查询时,它会导致错误:

Select xmlagg(xmlconcat(xmlelement(evalname(trdattr.TRANSACTION_ATTRIBUTE_CODE), 1)))
From    ( Select distinct xx_info_v12 TRANSACTION_ATTRIBUTE_CODE
           From   xxsample_table
           Where  xx_info_V1 = 'AR'
           and    xx_info_v12 <> 'tns:'
           --and      xx_info_n3 = ar_trx.trx_id 
           UNION ALL
           Select xx_info_v20 TRANSACTION_ATTRIBUTE_CODE
           From   xxsample_table 
           Where  xx_info_V1 = 'AR'
           --and      xx_info_n3 = ar_trx.trx_id 
           and    xx_info_v20 <> 0) trdattr
order by 
case TRANSACTION_ATTRIBUTE_CODE 
when 'tns:SW'       then 1
when 'tns:EE'       THEN 2
when 'tns:TP'       THEN 3
when 'tns:TT_WNT'   then 4
when 'tns:TT_D'     THEN 5
when 'tns:MR_T'     THEN 6
when 'tns:MR_UZ'    THEN 7
when 'tns:I_42'     THEN 8
when 'tns:I_63'     THEN 9
when 'tns:B_SPV'    THEN 10
when 'tns:B_SPV_DOSTAWA' THEN 11
when 'tns:B_MPV_PROWIZJA' THEN 12
when 'tns:MPP' THEN 13
ELSE 14 END


我也尝试了常规的ORDER BY,它仍然给我一个错误。
我尝试将ORDER BY移动到最里面的查询trdattr中,它不知何故工作。然而,当我重新添加CASE语句时,它给了我下面的错误:
ORA-01785:ORDER BY项必须是SELECT列表表达式的编号
如何将条件ORDER BY正确地合并到主查询中?

h7wcgrx3

h7wcgrx31#

根据你之前的问题,我想你可能是想对XMLAGG()中的元素进行排序,所以你会想把你的order by语句in the xmlagg call

-- excerpt from your main query...
-- Transaction Attributes
(Select xmlagg(xmlconcat(xmlelement(evalname(trdattr.TRANSACTION_ATTRIBUTE_CODE), 1))
               order by 
               case TRANSACTION_ATTRIBUTE_CODE 
               when 'tns:SW'       then 1
               when 'tns:EE'       THEN 2
               when 'tns:TP'       THEN 3
               when 'tns:TT_WNT'   then 4
               when 'tns:TT_D'     THEN 5
               when 'tns:MR_T'     THEN 6
               when 'tns:MR_UZ'    THEN 7
               when 'tns:I_42'     THEN 8
               when 'tns:I_63'     THEN 9
               when 'tns:B_SPV'    THEN 10
               when 'tns:B_SPV_DOSTAWA' THEN 11
               when 'tns:B_MPV_PROWIZJA' THEN 12
               when 'tns:MPP' THEN 13
               ELSE 14 END)
 From    ( Select distinct xx_info_v12 TRANSACTION_ATTRIBUTE_CODE
           From   xxsample_table
           Where  xx_info_V1 = 'AR'
           and    xx_info_v12 <> 'tns:'
           and    xx_info_n3 = ar_trx.trx_id 
           UNION ALL
           Select xx_info_v20 TRANSACTION_ATTRIBUTE_CODE
           From   xxsample_table 
           Where  xx_info_V1 = 'AR'
           and    xx_info_n3 = ar_trx.trx_id 
           and    xx_info_v20 <> 0) trdattr),
-- Kboxes
-- continue main query

字符串
我没法测试,但也许可以试试。

yvt65v4c

yvt65v4c2#

你的独立查询可以工作,但是在你的主查询中,你把它用作scalar subquery expression。因此,主查询的简化版本是:

select (
-- start of your standlone query
Select xmlagg(xmlconcat(xmlelement(evalname(trdattr.TRANSACTION_ATTRIBUTE_CODE), 1)))
From    ( Select distinct xx_info_v12 TRANSACTION_ATTRIBUTE_CODE
           From   xxsample_table
           Where  xx_info_V1 = 'AR'
           and    xx_info_v12 <> 'tns:'
           --and      xx_info_n3 = ar_trx.trx_id 
           UNION ALL
           Select xx_info_v20 TRANSACTION_ATTRIBUTE_CODE
           From   xxsample_table 
           Where  xx_info_V1 = 'AR'
           --and      xx_info_n3 = ar_trx.trx_id 
           and    xx_info_v20 <> 0) trdattr
order by 
case TRANSACTION_ATTRIBUTE_CODE 
when 'tns:SW'       then 1
when 'tns:EE'       THEN 2
when 'tns:TP'       THEN 3
when 'tns:TT_WNT'   then 4
when 'tns:TT_D'     THEN 5
when 'tns:MR_T'     THEN 6
when 'tns:MR_UZ'    THEN 7
when 'tns:I_42'     THEN 8
when 'tns:I_63'     THEN 9
when 'tns:B_SPV'    THEN 10
when 'tns:B_SPV_DOSTAWA' THEN 11
when 'tns:B_MPV_PROWIZJA' THEN 12
when 'tns:MPP' THEN 13
ELSE 14 END
-- end of your standalone query
)
from dual

字符串
.也抛出“ORA-00907:missing right parenthesis”。
正如docs所说:
标量子查询表达式是只从一行返回一个列值的子查询。
这就是你在这里所看到的--一行一列,这就是你的XMLAgg结果。尝试对这一行进行排序是有点无意义的,也是没有意义的。Oracle并不期望在这一点上看到一个order-by子句,它期望看到子查询表达式的右括号--因此它抛出了错误。
当您将order-by移动到内联视图时,它可能更有意义:

Select xmlagg(xmlconcat(xmlelement(evalname(trdattr.TRANSACTION_ATTRIBUTE_CODE), 1)))
From    ( Select distinct xx_info_v12 TRANSACTION_ATTRIBUTE_CODE
           From   xxsample_table
           Where  xx_info_V1 = 'AR'
           and    xx_info_v12 <> 'tns:'
           --and      xx_info_n3 = ar_trx.trx_id 
           UNION ALL
           Select xx_info_v20 TRANSACTION_ATTRIBUTE_CODE
           From   xxsample_table 
           Where  xx_info_V1 = 'AR'
           --and      xx_info_n3 = ar_trx.trx_id 
           and    xx_info_v20 <> 0
           order by 
           case TRANSACTION_ATTRIBUTE_CODE 
           when 'tns:SW'       then 1
           when 'tns:EE'       THEN 2
           when 'tns:TP'       THEN 3
           when 'tns:TT_WNT'   then 4
           when 'tns:TT_D'     THEN 5
           when 'tns:MR_T'     THEN 6
           when 'tns:MR_UZ'    THEN 7
           when 'tns:I_42'     THEN 8
           when 'tns:I_63'     THEN 9
           when 'tns:B_SPV'    THEN 10
           when 'tns:B_SPV_DOSTAWA' THEN 11
           when 'tns:B_MPV_PROWIZJA' THEN 12
           when 'tns:MPP' THEN 13
           ELSE 14 END
        ) trdattr


你会得到“ORA-01785:ORDER BY item must be the number of a SELECT-list expression”,这可能是this bug。这里的变通方法也可以工作,添加另一个级别的子查询:

Select xmlagg(xmlconcat(xmlelement(evalname(trdattr.TRANSACTION_ATTRIBUTE_CODE), 1)))
From (
  Select *
  From    ( Select distinct xx_info_v12 TRANSACTION_ATTRIBUTE_CODE
             From   xxsample_table
             Where  xx_info_V1 = 'AR'
             and    xx_info_v12 <> 'tns:'
             --and      xx_info_n3 = ar_trx.trx_id 
             UNION ALL
             Select xx_info_v20 TRANSACTION_ATTRIBUTE_CODE
             From   xxsample_table 
             Where  xx_info_V1 = 'AR'
             --and      xx_info_n3 = ar_trx.trx_id 
             and    xx_info_v20 <> 0
          )
         order by 
         case TRANSACTION_ATTRIBUTE_CODE 
         when 'tns:SW'       then 1
         when 'tns:EE'       THEN 2
         when 'tns:TP'       THEN 3
         when 'tns:TT_WNT'   then 4
         when 'tns:TT_D'     THEN 5
         when 'tns:MR_T'     THEN 6
         when 'tns:MR_UZ'    THEN 7
         when 'tns:I_42'     THEN 8
         when 'tns:I_63'     THEN 9
         when 'tns:B_SPV'    THEN 10
         when 'tns:B_SPV_DOSTAWA' THEN 11
         when 'tns:B_MPV_PROWIZJA' THEN 12
         when 'tns:MPP' THEN 13
         ELSE 14 END
  ) trdattr


然后,您可以将 that 作为标量子查询表达式。

to94eoyn

to94eoyn3#

下面是另一种可能有用的方法:

-- SQL*Plus formatting commands
SET PAGESIZE 60
SET LINESIZE 120
COLUMN sort_key         NOPRINT
COLUMN tablespace_name  FORMAT A18
COLUMN file_name        FORMAT A40

-- two queries, each contributing to unified view of tablespaces and files
-- each using DECODE() just because it's less wordy (and still supported)
-- column aliases not required on expressions in second query in UNION, but I like using them
SELECT
DECODE(ts.tablespace_name,
'SYSTEM',   'A1',
'SYSAUX',   'A2',
'UNDOTBS1', 'B',
'USERS',    'D',
'Z') AS sort_key,
ts.tablespace_name,
f.file_name,
(f.bytes / 1024 / 1024) AS mbytes,
ts.status
FROM
dba_tablespaces ts JOIN dba_data_files f ON ts.tablespace_name = f.tablespace_name
UNION
SELECT
DECODE(ts.tablespace_name,
'TEMP',  'C',
'Z') AS sort_key,
ts.tablespace_name,
f.file_name,
(f.bytes / 1024 / 1024) AS mbytes,
ts.status
FROM
dba_tablespaces ts JOIN dba_temp_files f ON ts.tablespace_name = f.tablespace_name
ORDER BY
sort_key,
tablespace_name
;

-- results - in the order I prefer to see
TABLESPACE_NAME    FILE_NAME                                    MBYTES STATUS
------------------ ---------------------------------------- ---------- ---------
SYSTEM             C:\DEV\ORADATA\DB01\SYSTEM01.DBF               890 ONLINE
SYSAUX             C:\DEV\ORADATA\DB01\SYSAUX01.DBF               510 ONLINE
UNDOTBS1           C:\DEV\ORADATA\DB01\UNDOTBS01.DBF               60 ONLINE
TEMP               C:\DEV\ORADATA\DB01\TEMP01.DBF                  32 ONLINE
USERS              C:\DEV\ORADATA\DB01\USERS01.DBF                  5 ONLINE
MYDAT              C:\DEV\ORACLE19C\DATABASE\MYDAT01.DBF           800 ONLINE
MYIDX              C:\DEV\ORACLE19C\DATABASE\MYIDX01.DBF           800 ONLINE

字符串

相关问题