我有一个简单的查询,它给了我一个错误
错误(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
正确地合并到主查询中?
3条答案
按热度按时间h7wcgrx31#
根据你之前的问题,我想你可能是想对
XMLAGG()
中的元素进行排序,所以你会想把你的order by
语句in the xmlagg call:字符串
我没法测试,但也许可以试试。
yvt65v4c2#
你的独立查询可以工作,但是在你的主查询中,你把它用作scalar subquery expression。因此,主查询的简化版本是:
字符串
.也抛出“ORA-00907:missing right parenthesis”。
正如docs所说:
标量子查询表达式是只从一行返回一个列值的子查询。
这就是你在这里所看到的--一行一列,这就是你的XMLAgg结果。尝试对这一行进行排序是有点无意义的,也是没有意义的。Oracle并不期望在这一点上看到一个order-by子句,它期望看到子查询表达式的右括号--因此它抛出了错误。
当您将order-by移动到内联视图时,它可能更有意义:
型
你会得到“ORA-01785:ORDER BY item must be the number of a SELECT-list expression”,这可能是this bug。这里的变通方法也可以工作,添加另一个级别的子查询:
型
然后,您可以将 that 作为标量子查询表达式。
to94eoyn3#
下面是另一种可能有用的方法:
字符串