多选查询中的 CASE

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

我正在运行以下查询,该查询工作正常,但我想添加进一步的更改。我一直在尝试使用 CASE 为我提供 opt.oper_type 的更好描述,但我不确定是否可以将其嵌入到我的选择查询中。

select 
oc.card_number,
opt.oper_date as "TRANSACTION_DATE",
op.account_number, 
vf.sttl_currency as "SOURCE_AMOUNT_CURRENCY", 
vf.sttl_amount as "SOURCE_AMOUNT", 
op.auth_code, 
opt.mcc, 
opt.terminal_number as "TERMINAL_ID", 
vf.pos_terminal_cap, 
vf.pos_entry_mode, 
opt.oper_type, 
opt.part_key as "CAPTURE_DATE", 
opt.oper_amount as "TRANSACTION_AMOUNT",
opt.oper_currency as "TRANSACTION_CURRENCY", 
opt.merchant_name, 
opt.merchant_country, 
opt.clearing_sequence_num, 
opt.clearing_sequence_count
    from opr_operation opt, opr_participant op, opr_card oc , vis_fin_message vf 
    where
        op.oper_id=opt.id 
        and opt.id =vf.id
        and opt.clearing_sequence_num > 1 
        and opt.part_key >= '28-MAR-24' 
        and op.card_id= vf.card_id
        and op.participant_type='PRTYISS'
        and oc.oper_id=opt.id
        and opt.msg_type in ('MSGTPAMC','MSGTPACC');

这给了我以下输出:

CARD_NUMBER              TRANSACTI ACCOUNT_NUMBER                   SOU SOURCE_AMOUNT AUTH_C MCC  TERMINAL_ID      POS_TERM PO OPER_TYP CAPTURE_D TRANSACTION_AMOUNT TRA MERCHANT_NAME                                                                                                                                                                                            MER CLEARING_SEQUENCE_NUM CLEARING_SEQUENCE_COUNT
------------------------ --------- -------------------------------- --- ------------- ------ ---- ---------------- -------- -- -------- --------- ------------------ --- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- --------------------- -----------------------
****************         28-MAR-24 006000152511                     840          2020 760667 5734 TERMID01         9        01 OPTP0000 02-APR-24               2020 840 ACQUIRER NAME

但是我想在我的查询中添加如下所示的 CASE 规则,以提供另一列,显示 TransactionType 为“05”(如果它满足匹配 OPTP0000 的条件)。

select 
oper_type, 
CASE 
    when oper_type = 'OPTP0000' THEN '05' ELSE 'other'
     END AS TransactionType   
     from opr_operation;

OPER_TYP TRANS
-------- -----
OPTP0000 05  

这是否可能,或者除了 CASE 之外还有其他方法可以做到这一点吗?我知道我的 SQL 不是最好的,但这只是为了让我在一些内部测试系统上运行,这样我就可以轻松识别其中的一些细节,所以我提前道歉。

oracle-sqldeveloper
1个回答
0
投票

如果列 oper_type 出现在多个表中,请将表名称指定为列引用的一部分:

select 
  oc.card_number,
  ...
  opt.clearing_sequence_count,
  case when opt.oper_type = 'OPTP0000' then '05' else 'other' end as TransactionType   
from opr_operation opt, opr_participant op, opr_card oc , vis_fin_message vf 
where
    op.oper_id=opt.id 
    and opt.id =vf.id
    and opt.clearing_sequence_num > 1 
    and opt.part_key >= '28-MAR-24' 
    and op.card_id= vf.card_id
    and op.participant_type='PRTYISS'
    and oc.oper_id=opt.id
    and opt.msg_type in ('MSGTPAMC','MSGTPACC');
© www.soinside.com 2019 - 2024. All rights reserved.