Oracle
 sql >> Database >  >> RDS >> Oracle

La subquery correlata funziona in modo diverso per Oracle 12c e 11g

Sono stato in grado di risolvere il problema con una soluzione fornita da un esperto SQL Chris Saxon usando con WITH CTE. In precedenza l'ho usato come tabella di guida principale, ma risulta che dovrebbe essere nelle query di correlazione come di seguito:

with ar_kbox_details as (
  select ab_info_n3  trx_id,
         ab_info_v5  box,
         ab_info_n1  amount
  from xxab_zz_vat_trx_gt
  where ab_info_v1 = 'AR'
  and ab_info_v5 <> 'tns:'
  union all
  select ab_info_n3  trx_id,
         ab_info_v6  box,
         ab_info_n2  amount
  from xxab_zz_vat_trx_gt
  where ab_info_v1 = 'AR'
  and ab_info_v6 <> 'tns:'
)
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
    -- Document Type
    ,(select distinct xmlagg(xmlconcat(xmlelement("tns:TypDokumentu", ab_info_v15)))
    From   xxab_zz_vat_trx_gt
    Where  ab_info_v1 = 'AR'
    and ab_info_n3 = ar_trx.trx_id
    and ab_info_v16 = 'ORA_JEPL_DOCUMENT_TYPE')
    -- product category code
    ,(select distinct xmlagg(xmlconcat(xmlelement(evalname(ab_info_v13), 1)))
    from    xxab_zz_vat_trx_gt
    Where ab_info_V1 = 'AR'
    and ab_info_v13 <> 'tns:'
    and ab_info_n3  = ar_trx.trx_id)
    -- Trade Attributes
    ,(select distinct xmlagg(xmlconcat(xmlelement(evalname(DECODE(TRADE_ATTRIBUTE_VALUES, '1', 'tns:MPP', TRADE_ATTRIBUTE_VALUES)), 1))
    order by 
    case TRADE_ATTRIBUTE_VALUES
    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     xxab_zz_vat_trx_gt unpivot (TRADE_ATTRIBUTE_VALUES for TRADE_ATTRIBUTES in (ab_info_v12, ab_info_v20))
    Where  ab_info_V1 = 'AR'
    and  TRADE_ATTRIBUTE_VALUES not in ('tns:', 'tns:IMP', '0')
    and  ab_info_n3 = ar_trx.trx_id
    )
    --KBoxes
    ,(select xmlagg(xmlconcat(xmlelement(evalname(fnd_box.box),ROUND(sum(nvl(akd.amount, 0)),2))))
    from    ar_kbox_details akd
        ,   (SELECT 'tns:K_'||(LEVEL + 9) box
            FROM dual
            CONNECT BY LEVEL <= 27) fnd_box     
    where fnd_box.box = akd.box (+)
    and   ar_trx.trx_id = akd.trx_id (+)
    group by fnd_box.box)
    )))
From ( Select distinct 
        ab_info_n3  trx_id
    ,   ab_info_v2  trx_number
    ,   ab_info_d1  sales_date
    ,   ab_info_d3  invoice_date
    ,   ab_info_v3  cust_name
    ,   ab_info_v17 cust_addr
    ,   ab_info_v7  tax_reg_num
From xxab_zz_vat_trx_gt
Where ab_info_v1 = 'AR'
Order by 3,4) ar_trx