t1
id|entity_type
9|3
9|4
9|5
2|3
2|5
t2
id|entity_type
1|3
1|4
1|5
SELECT t1.id, array_agg(t1.entity_type)
FROM t1
GROUP BY
t1.id
HAVING ARRAY_AGG(t1.entity_type ORDER BY t1.entity_type) =
(SELECT ARRAY_AGG(t2.entity_type ORDER BY t2.entity_type)
FROM t2
WHERE t2.id = 1
GROUP BY t2.id);
Result:
t1.id = 9|array_agg{3,4,5}
I have two tables t1
and t2
. I want to get value of t1.id
where t1.entity_type
array equals t2.entity_type
array.
In this scenario everything works fine. For t2.id = 1
I receive t1.id = 9
.
Both have the same array of entity_type
: {3,4,5}
Now I'd like to get t1.id
not only for equal sets, but also for smaller sets.
If I modify t2
this way:
t2
id|entity_type
1|3
1|4
and modify query this way:
SELECT t1.id, array_agg(t1.entity_type)
FROM t1
GROUP BY
t1.id
HAVING ARRAY_AGG(t1.entity_type ORDER BY t1.entity_type) >= /*MODIFICATION*/
(SELECT ARRAY_AGG(t2.entity_type ORDER BY t2.entity_type)
FROM t2
WHERE t2.id = 1
GROUP BY t2.id);
I don't receive the expected result:
t1.id = 1 has {3, 4, 5}
t2.id = 1 has {3, 4}
Arrays in t1
that contain the array in t2
should qualify. I expect to receive results as in first case but I get no rows.
Is there any method like: ARRAY_AGG
contains another ARRAY_AGG
?