I have a simple Postgres dataset that looks like this:
INSERT INTO mytable (day, person)
values
('Monday', 'A'),
('Monday', 'B'),
('Tuesday', 'A'),
('Thursday', 'B');
I then run a query that yields two array_aggs as follows:
SELECT *
FROM (select day as d1,
array_agg(distinct person) as agg1
from mytable
group by day) AS AA
cross join
(select day as d2,
array_agg(distinct person) as agg2
from mytable
group by day) AS BB
which yields this dataset:
Monday, {A,B}, Monday, {A,B}
Monday, {A,B}, Thursday, {B}
Monday, {A,B}, Tuesday, {A}
Thursday, {B}, Monday, {A,B}
Thursday, {B}, Thursday, {B}
Thursday, {B}, Tuesday, {A}
Tuesday, {A}, Monday, {A,B}
Tuesday, {A}, Thursday, {B}
Tuesday, {A}, Tuesday, {A}
I would like to add a fifth column to this query that identifies that number of repeat entries in agg1 and agg2 across each row.
So for example, the first row would be 2 and the second row would be 1. I was hoping to do it as follows, but this gives me a ambiguous syntax error:
SELECT *, count(select unnest(agg1) intersect select unnest(agg2))
FROM (select day as d1,
array_agg(distinct person) as agg1
from mytable
group by day) AS AA
cross join
(select day as d2,
array_agg(distinct person) as agg2
from mytable
group by day) AS BB