取消透视显示错误结果的多列

时间:2018-09-18 09:38:33

标签: sql oracle unpivot

我的初始查询和结果如下所示

SELECT * FROM (
      SELECT S.CURRENCYCD , S.COMBINEDTXNAMOUNT
      , S.INTERCHANGEAMOUNT
      , S.OTHERCHARGEAMOUNT
     ,S.APPLIEDCHARGES
      FROM 
      (SELECT X.CURRENCYCD,
             CASE WHEN X.CombinedTxnAmount IS NULL THEN 0 ELSE X.CombinedTxnAmount END AS CombinedTxnAmount,
             CASE WHEN X.InterchangeAmount IS NULL THEN 0 ELSE X.InterchangeAmount END AS InterchangeAmount,
             CASE WHEN X.OtherChargeAmount IS NULL THEN 0 ELSE X.OtherChargeAmount END AS OtherChargeAmount,
             CASE WHEN X.MinimumBillAdjustmentAmount IS NULL THEN 0 ELSE X.MinimumBillAdjustmentAmount END +
             CASE WHEN X.AppliedChargeAmount IS NULL THEN 0 ELSE X.AppliedChargeAmount END AS APPLIEDCHARGES FROM 
     XMLTABLE (XMLNAMESPACES('http://www.eds.com/AgileCard/xsd/ACFMerchantStatement/2013/05' AS "acf",'http://www.eds.com/AgileCard/xsd/ACFMerchant/2012/03' as "acfMerchant")
 ,'/acf:Statement/acf:ChargeSummaryTotals/acf:ChargeSummaryTotalDetail' passing xmltype ('Myxml')
  COLUMNS CURRENCYCD VARCHAR(4) PATH 'acfMerchant:CurrencyCd',
          CombinedTxnAmount NUMBER PATH 'acf:CombinedTxnAmount',
          InterchangeAmount NUMBER PATH 'acf:InterchangeAmount',
          OtherChargeAmount NUMBER PATH 'acf:OtherChargeAmount',
          MinimumBillAdjustmentAmount NUMBER PATH 'acf:MinimumBillAdjustmentAmount',
          AppliedChargeAmount NUMBER PATH 'acf:AppliedChargeAmount'
 )X ) S

结果是:

currencyCD | combinedTxnAmount | InterchangeAmount| OtherChangeAmount|AppliedCharges

GBP | 126.5 | 97.02 | 252.92 | 476.44

我正在尝试取消查询结果,但是由于某种原因,我没有得到预期的结果。以下是我的关键查询。

 SELECT * FROM (
      SELECT S.CURRENCYCD , S.COMBINEDTXNAMOUNT
      , S.INTERCHANGEAMOUNT
      , S.OTHERCHARGEAMOUNT
     ,S.APPLIEDCHARGES
      FROM 
      (SELECT X.CURRENCYCD,
             CASE WHEN X.CombinedTxnAmount IS NULL THEN 0 ELSE X.CombinedTxnAmount END AS CombinedTxnAmount,
             CASE WHEN X.InterchangeAmount IS NULL THEN 0 ELSE X.InterchangeAmount END AS InterchangeAmount,
             CASE WHEN X.OtherChargeAmount IS NULL THEN 0 ELSE X.OtherChargeAmount END AS OtherChargeAmount,
             CASE WHEN X.MinimumBillAdjustmentAmount IS NULL THEN 0 ELSE X.MinimumBillAdjustmentAmount END +
             CASE WHEN X.AppliedChargeAmount IS NULL THEN 0 ELSE X.AppliedChargeAmount END AS APPLIEDCHARGES FROM 
     XMLTABLE (XMLNAMESPACES('http://www.eds.com/AgileCard/xsd/ACFMerchantStatement/2013/05' AS "acf",'http://www.eds.com/AgileCard/xsd/ACFMerchant/2012/03' as "acfMerchant")
 ,'/acf:Statement/acf:ChargeSummaryTotals/acf:ChargeSummaryTotalDetail' passing xmltype ('myxml')
  COLUMNS CURRENCYCD VARCHAR(4) PATH 'acfMerchant:CurrencyCd',
          CombinedTxnAmount NUMBER PATH 'acf:CombinedTxnAmount',
          InterchangeAmount NUMBER PATH 'acf:InterchangeAmount',
          OtherChargeAmount NUMBER PATH 'acf:OtherChargeAmount',
          MinimumBillAdjustmentAmount NUMBER PATH 'acf:MinimumBillAdjustmentAmount',
          AppliedChargeAmount NUMBER PATH 'acf:AppliedChargeAmount'
 )X ) S) UNPIVOT ((AMT) FOR TASK IN ((COMBINEDTXNAMOUNT) AS 'Combined Transaction Charge',
                                     (INTERCHANGEAMOUNT) as 'Interchange'     
                                   ,(OTHERCHARGEAMOUNT) as 'Other Charges'
                                   ,(APPLIEDCHARGES) as 'Charges Applied To Account'
                                   ))

以下是此查询返回的内容:

currencycd |      Task                   | AMT  
GBP        | Combined Transaction Charge | 126.5  
GBP        | Interchange                 | 0  
GBP        | Other Charges               | 0  
GBP        | Charges Applied To Account  | 0  

只有第一个未透视值会正确显示该金额。剩余值显示为零。

有人可以指出这个查询出了什么问题吗

0 个答案:

没有答案