0

Need help to create query to compare the data and fetch relevant data from table:

Input

Field1 Field2
abc_ID ID
abc abc
abc_id_test test
abc_id_test abc test
abc_id_test_scenario scenario
abc_id_test_scenario cde test

Output

Field1 Field2
abc_id_test_scenario cde test

If field 2 contains string that matches with field 1 then I need to filter those records.

2
  • I don't understand this: "If field 2 contains only 1 word and it matches with field 1 words then I need to filter it." Your result has one row only and there field2 has two "words" (even this is already an assumption, you didn't tell us what you mean by "word"). Why should the row with field2 = "abc test" not appear in the result? Please put much more effort in your question and explain in detail the logic that should be applied. Commented Feb 16, 2023 at 13:39
  • Apologies @JonasMetzler.Corrected it now. Commented Feb 16, 2023 at 14:07

1 Answer 1

1

You can do it using unnest and string_to_array combined to expand a string to a set of rows. then check rows from Field2 not match Field1

with cte as (
  select Field1, Field2, unnest(string_to_array(Field2, ' ')) as item
  from mytable
)
select field1, Field2
from cte
where Field1 not like concat('%',item,'%')

Demo here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.