出纳员可以在字段C40_DATA中输入3个不同的值(9962 **********,id_invoice,网络订单)
我希望得到什么:
当我创建这个(如下)时,结果包含所有3个,但我需要1 +(如果存在2,如果找不到3)
select * from web_data p
inner join POS_DATA re on
(case
when substr(re.C40_DATA,5,8) = p.id_invoice and substr(re.C40_DATA,1,4) ='9962' then 1 else
(case
when re.C40_DATA = p.id_web_order and p.d_mnozstvi < '0' then 1 else
(case
when re.C40_DATA = p.id_invoice and p.d_mnozstvi < '0' then 1 else 0 END)
END)
END=1)
您能否帮助如何在JOIN中进行竞争-如果满足一个条件,则停止并找不到下一个条件?
谢谢
答案 0 :(得分:0)
您尝试查询的方式最好是这样写:
select * from web_data p
inner join POS_DATA re on
substr(re.C40_DATA, 5, 8) = p.id_invoice and substr(re.C40_DATA, 1, 4) = '9962'
or re.C40_DATA = p.id_web_order and p.d_mnozstvi < '0'
or re.C40_DATA = p.id_invoice and p.d_mnozstvi < '0'
如果您确实需要使用case
表达式来完成此操作,则可以按照以下说明进行操作。情况按顺序进行:
case
when substr(re.C40_DATA, 5, 8) = p.id_invoice and substr(re.C40_DATA, 1, 4) ='9962' then 1
when re.C40_DATA = p.id_web_order and p.d_mnozstvi < '0' then 1
when re.C40_DATA = p.id_invoice and p.d_mnozstvi < '0' then 1
end = 1
就仅在单个条件下匹配而言,我认为您需要这样的东西:
with data as (
select *,
case
when re.C40_DATA = '9962' + p.id_invoice then 1
when re.C40_DATA = p.id_web_order and p.d_mnozstvi < '0' then 2
when re.C40_DATA = p.id_invoice and p.d_mnozstvi < '0' then 3
end as match_rank,
min(case
when re.C40_DATA = '9962' + p.id_invoice then 1
when re.C40_DATA = p.id_web_order and p.d_mnozstvi < '0' then 2
when re.C40_DATA = p.id_invoice and p.d_mnozstvi < '0' then 3
) over (partition by p.id_invoice, p.web_order) as min_match_rank
from web_data p
inner join POS_DATA re on
re.C40_DATA = '9962' + p.id_invoice
or re.C40_DATA = (p.id_web_order, p.id_invoice) and p.d_mnozstvi < '0'
)
select * from data where match_rank = min_match_rank;
如果这些选项对您有用,我会随意重写您的某些条件。另外,我不确定< '0'
是什么意思。最后,由于不确定id_invoice
和id_web_order
之间的关系,我不确定要对哪些值进行分区。