是否可以创建总和作为大型选择查询的一部分

问题描述 投票:0回答:1

我有下面的查询,效果很好,但我只是想包含一个语句,该语句将在输出底部的新行上提供“P”列的总计 - SETTLMENT_AMOUNT。

 select oc.card_number,
       case opt.oper_type
       when 'OPTP0000' then '05-PURCHASE' 
       when 'OPTP0020' then '06-CREDIT VOUCHER' 
       else 'other' end as Transaction_Type,
       opt.oper_date as TRANSACTION_DATE, 
       op.account_number, 
       case vf.sttl_currency
       when '840' then 'USD'
       end as SOURCE_AMOUNT_CURRENCY, 
   TO_CHAR (
      CASE opt.oper_type
         WHEN 'OPTP0000' THEN COALESCE (-vf.sttl_amount /100, 0)
         ELSE vf.sttl_amount /100
      END,
      '99G999D00') AS SOURCE_AMOUNT,
       op.auth_code, 
       opt.mcc, 
       opt.terminal_number as TERMINAL_ID, 
       vf.pos_terminal_cap, 
       vf.pos_entry_mode,
       opt.part_key as CAPTURE_DATE, 
       TO_CHAR (
       CASE opt.oper_type
       when 'OPTP0000' THEN COALESCE (-vf.oper_amount /100, 0)
       ELSE vf.oper_amount /100
       END,
       '99G999D00') as TRANSACTION_AMOUNT,
       COALESCE(ccu.name,opt.oper_currency) TRANSACTION_CURRENCY,
        case vf.sttl_currency
       when '840' then 'BMD'
       end as SETTLEMENT_CURRENCY, 
   TO_CHAR (
      CASE opt.oper_type
         WHEN 'OPTP0000' THEN COALESCE (-vf.sttl_amount /100, 0)
         ELSE vf.sttl_amount /100
      END,
      '99G999D00') AS SETTLEMENT_AMOUNT,
       opt.merchant_name,
       COALESCE(cc.visa_country_code,opt.merchant_country) merchant_country,
       opt.clearing_sequence_num, 
       opt.clearing_sequence_count, 
       opt.oper_type
FROM   opr_operation opt 
       INNER JOIN opr_participant op
       ON op.oper_id = opt.id
       INNER JOIN opr_card oc
       ON oc.oper_id = opt.id
       INNER JOIN vis_fin_message vf
       ON     opt.id = vf.id
       INNER JOIN  com_country cc
       ON cc.code= opt.merchant_country 
          AND op.card_id = vf.card_id
       INNER JOIN  com_currency ccu
       ON ccu.code= opt.oper_currency 
          AND op.card_id = vf.card_id   
WHERE  opt.clearing_sequence_num > 1
AND    opt.part_key >= DATE '2024-03-01'
AND    opt.msg_type IN ('MSGTPAMC', 'MSGTPACC')
AND    op.participant_type = 'PRTYISS'

输出

"CARD_NUMBER"   "TRANSACTION_TYPE"  "TRANSACTION_DATE"  "ACCOUNT_NUMBER"    "SOURCE_AMOUNT_CURRENCY"    "SOURCE_AMOUNT" "AUTH_CODE" "MCC"   "TERMINAL_ID"   "POS_TERMINAL_CAP"  "POS_ENTRY_MODE"    "CAPTURE_DATE"  "TRANSACTION_AMOUNT"    "TRANSACTION_CURRENCY"  "SETTLEMENT_CURRENCY"   "SETTLEMENT_AMOUNT" "MERCHANT_NAME" "MERCHANT_COUNTRY"  "CLEARING_SEQUENCE_NUM" "CLEARING_SEQUENCE_COUNT"   "OPER_TYPE"
"1234567890123456"  "05-PURCHASE"   25-MAR-24   "004000485011"  "USD"   "    -20.20"    "718515"    "5734"  "TERMID01"  "9" "01"    27-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   25-MAR-24   "006000152511"  "USD"   "    -20.20"    "718523"    "5734"  "TERMID01"  "9" "01"    27-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   25-MAR-24   "006008007501"  "USD"   "    -20.20"    "718529"    "5734"  "TERMID01"  "9" "01"    27-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   26-MAR-24   "004000485011"  "USD"   "    -20.20"    "735762"    "5734"  "TERMID01"  "9" "01"    28-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   26-MAR-24   "006000152511"  "USD"   "    -20.20"    "735770"    "5734"  "TERMID01"  "9" "01"    28-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   26-MAR-24   "006008007501"  "USD"   "    -20.20"    "735776"    "5734"  "TERMID01"  "9" "01"    28-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   26-MAR-24   "006000095511"  "USD"   "    -20.20"    "735780"    "5734"  "TERMID01"  "9" "01"    28-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   28-MAR-24   "006000152511"  "USD"   "    -22.48"    "760673"    "5734"  "TERMID01"  "9" "01"    02-APR-24   "    -20.20"    "EUR"   "BMD"   "    -22.48"    "ACQUIRER NAME            " "DE"    2   2   "OPTP0000"
"1234567890123456""06-CREDIT VOUCHER"   28-MAR-24   "004000485011"  "USD"   "     26.32"    "760660"    "5734"  "TERMID01"  "9" "01"    02-APR-24   "     20.20"    "GBP"   "BMD"   "     26.32"    "ACQUIRER NAME            " "GB"    2   2   "OPTP0020"
"1234567890123456"  "05-PURCHASE"   28-MAR-24   "006000152511"  "USD"   "    -20.20"    "760667"    "5734"  "TERMID01"  "9" "01"    02-APR-24   "    -20.20"    "USD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "US"    2   2   "OPTP0000"

我认为我的部分问题是我有一条规则来代替

  TO_CHAR (
      CASE opt.oper_type
         WHEN 'OPTP0000' THEN COALESCE (-vf.sttl_amount /100, 0)
         ELSE vf.sttl_amount /100
      END,
      '99G999D00') AS SETTLEMENT_AMOUNT,

所以我只是不确定如何引入总和,以便在最后一行下方的 TOTAL = -157.76 的示例中计算 SETTLMENT_AMOUNT。

"CARD_NUMBER"   "TRANSACTION_TYPE"  "TRANSACTION_DATE"  "ACCOUNT_NUMBER"    "SOURCE_AMOUNT_CURRENCY"    "SOURCE_AMOUNT" "AUTH_CODE" "MCC"   "TERMINAL_ID"   "POS_TERMINAL_CAP"  "POS_ENTRY_MODE"    "CAPTURE_DATE"  "TRANSACTION_AMOUNT"    "TRANSACTION_CURRENCY"  "SETTLEMENT_CURRENCY"   "SETTLEMENT_AMOUNT" "MERCHANT_NAME" "MERCHANT_COUNTRY"  "CLEARING_SEQUENCE_NUM" "CLEARING_SEQUENCE_COUNT"   "OPER_TYPE"
"1234567890123456"  "05-PURCHASE"   25-MAR-24   "004000485011"  "USD"   "    -20.20"    "718515"    "5734"  "TERMID01"  "9" "01"    27-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   25-MAR-24   "006000152511"  "USD"   "    -20.20"    "718523"    "5734"  "TERMID01"  "9" "01"    27-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   25-MAR-24   "006008007501"  "USD"   "    -20.20"    "718529"    "5734"  "TERMID01"  "9" "01"    27-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   26-MAR-24   "004000485011"  "USD"   "    -20.20"    "735762"    "5734"  "TERMID01"  "9" "01"    28-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   26-MAR-24   "006000152511"  "USD"   "    -20.20"    "735770"    "5734"  "TERMID01"  "9" "01"    28-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   26-MAR-24   "006008007501"  "USD"   "    -20.20"    "735776"    "5734"  "TERMID01"  "9" "01"    28-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   26-MAR-24   "006000095511"  "USD"   "    -20.20"    "735780"    "5734"  "TERMID01"  "9" "01"    28-MAR-24   "    -20.20"    "BMD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"
"1234567890123456"  "05-PURCHASE"   28-MAR-24   "006000152511"  "USD"   "    -22.48"    "760673"    "5734"  "TERMID01"  "9" "01"    02-APR-24   "    -20.20"    "EUR"   "BMD"   "    -22.48"    "ACQUIRER NAME            " "DE"    2   2   "OPTP0000"
"1234567890123456""06-CREDIT VOUCHER"   28-MAR-24   "004000485011"  "USD"   "     26.32"    "760660"    "5734"  "TERMID01"  "9" "01"    02-APR-24   "     20.20"    "GBP"   "BMD"   "     26.32"    "ACQUIRER NAME            " "GB"    2   2   "OPTP0020"
"1234567890123456"  "05-PURCHASE"   28-MAR-24   "006000152511"  "USD"   "    -20.20"    "760667"    "5734"  "TERMID01"  "9" "01"    02-APR-24   "    -20.20"    "USD"   "BMD"   "    -20.20"    "ACQUIRER NAME            " "US"    2   2   "OPTP0000"
                                                                                                                                                                                                       TOTAL = -157.76    
sql oracle oracle-sqldeveloper
1个回答
0
投票

您可以使用以下内容,其中总计将显示在数据的最后一行

decode(rownum,count(1) over(),SUM(settlement_amount) OVER ()) AS TOTAL_AMOUNT
© www.soinside.com 2019 - 2024. All rights reserved.