Reputation: 467
My table looks like this:
id | top_id | name_important | name | another_column |
---|---|---|---|---|
111 | [AAA, BBB] | null | ||
222 | 111 | [AAA] | X |
What I would like to check is that top_id
and name_important
are not empty. And that value in array name_important
are in array name
based on top_id
(those arrays might be empty or might not exisit). In this case there is the relation between id
222 and 111 because id
222 has 111 in column top_id
. I would like to return true
or false
. Could you help me?
I would like to use case when clause since I have other cases to include, something like this:
with test as (
select 111 as id, null as top_id, null as name_important, ["AAA", "BBB"] as name, null as another_column union all
select 222, 111, ["AAA"], null, X
)
select id,
case when another_column is not null then true
when (question in the topic) then true
else false end result
from test
Upvotes: 0
Views: 618
Reputation: 10162
Maybe something like this could fit:
with test as (
select 111 as id, null as top_id, null as name_important, ["AAA", "BBB"] as name, null as another_column union all
select 222, 111, ["AAA"], null, "X"
)
select
id,
case
when another_column is not null then true
when intersected then true
else false
end result
from (
select test.*, ifnull(intersected, false) as intersected
from test
left join (
select secondary.id, true as intersected
from (
select test.id, name
from test, test.name as name
) as main
join (
select test.id, test.top_id, name_important
from test, test.name_important as name_important
) as secondary
on main.id = secondary.top_id and main.name = secondary.name_important
) using (id)
)
Upvotes: 1
Reputation: 1269663
It is not clear from your example if you need all values in the array to match or any values. If you want any value, then:
with test as (
select 111 as id, null as top_id, null as name_important, ['AAA', 'BBB'] as name union all
select 222, 111, ['AAA'], null
)
select t2.*, t1.name
from test t1 join
test t2
on t1.top_id = t2.id
where t1.name_important is not null and
exists (select 1
from unnest(t2.name) n1 join
unnest(t1.name_important) n2
on n2 = n1
)
If all need to match:
with test as (
select 111 as id, null as top_id, null as name_important, ['AAA', 'BBB'] as name union all
select 222, 111, ['AAA'], null
)
select t1.*, t2.name
from test t1 join
test t2
on t1.top_id = t2.id
where t1.name_important is not null and
not exists (select 1
from unnest(t1.name_important) n1 left join
unnest(t2.name) n2
on n2 = n1
where n2 is null
);
Upvotes: 1