我需要将下表"BRAND"中的一列值与另一列值进行比较。我试着单独考虑两根柱子之间的绳子的长度,但尽管如此。。列中的值不同,因此给出的结果不正确。
表品牌:
ID brand_1 brand_2 Status
---------------------------------------------------
1 SAC SAC True
2 APP BBB BBB APP True
3 ABC OND DEG DEG ABC OND True
4 GIF APP GIF False
5 GHY PPA GHY PPA ABC False
6 MNC CGA IPK GIT ABC ITY False
我需要返回False行,因为列brand_1和brand_2之间没有匹配。数据中不存在"状态"列;我在这里添加它是为了演示哪些行被认为是"false"并且应该返回。输出中也不应该存在此列。
输出:
ID brand_1 brand_2
-------------------------------------------
4 GIF APP GIF
5 GHY PPA GHY PPA ABC
6 MNC CGA IPK GIT ABC ITY
请帮帮我。
这里有一种方法(不是最快的,但易于编写和维护(。其想法是将每个输入字符串拆分为其组件,按字母顺序排序,然后重新组合。我使用lateral
(因此每一行都独立于其他行进行处理(、JSON
技巧来拆分字符串,并使用LISTAGG
将它们重新组合在一起。
with
brand (id, brand_1, brand_2) as (
select 1, 'SAC' , 'SAC' from dual union all
select 2, 'APP BBB' , 'BBB APP' from dual union all
select 3, 'ABC OND DEG', 'DEG ABC OND' from dual union all
select 4, 'GIF' , 'APP GIF' from dual union all
select 5, 'GHY PPA' , 'GHY PPA ABC' from dual union all
select 6, 'MNC CGA IPK', 'GIT ABC ITY' from dual
)
select id, brand_1, brand_2,
case when b1 = b2 then 'True' else 'False' end as status
from brand,
lateral( select listagg(token, ' ') within group (order by token) as b1
from json_table( '["' || replace(brand_1, ' ','","') || '"]',
'$[*]' columns(token varchar2 path '$'))
),
lateral( select listagg(token, ' ') within group (order by token) as b2
from json_table( '["' || replace(brand_2, ' ','","') || '"]',
'$[*]' columns(token varchar2 path '$'))
)
;
ID BRAND_1 BRAND_2 STATUS
-- ----------- ----------- ------
1 SAC SAC True
2 APP BBB BBB APP True
3 ABC OND DEG DEG ABC OND True
4 GIF APP GIF False
5 GHY PPA GHY PPA ABC False
6 MNC CGA IPK GIT ABC ITY False
编辑:要在已编辑的问题中获得所需结果,请从SELECT
子句中删除case
表达式,并在查询末尾添加一个WHERE
子句:... where b1 != b2
(假设输入字符串不能为null;如果可以为null,则必须根据业务需要进行处理(。