I have three tables:
CREATE TABLE foo (
id bigint PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE foo_bar (
id bigint PRIMARY KEY,
foo_id bigint NOT NULL
);
CREATE TABLE tag (
name text NOT NULL,
target_id bigint NOT NULL,
PRIMARY KEY (name, target_id)
);
I'm trying to create a view such that I get all of the fields of table foo
, the count of items in foo_bar
where foo.id = foo_bar.foo_id
, and a text array of all tags where foo.id = tag.target_id
. If we have:
INSERT INTO foo VALUES (1, 'one');
INSERT INTO foo VALUES (2, 'two');
INSERT INTO foo_bar VALUES (1, 1);
INSERT INTO foo_bar VALUES (2, 1);
INSERT INTO foo_bar VALUES (3, 2);
INSERT INTO foo_bar VALUES (4, 1);
INSERT INTO foo_bar VALUES (5, 2);
INSERT INTO tag VALUES ('a', 1);
INSERT INTO tag VALUES ('b', 1);
INSERT INTO tag VALUES ('c', 2);
The result should return:
foo.id | foo.name | count | array_agg
--------------------------------------------------
1 | one | 3 | {a, b}
2 | two | 2 | {c}
This is what I have so far:
SELECT DISTINCT f.id, f.name, COUNT(b.id), array_agg(t.name)
FROM foo AS f, foo_bar AS b, tag AS t
WHERE f.id = t.target_id AND f.id = b.foo_id
GROUP BY f.id, b.id;
These are the results I'm getting (note the count
is incorrect):
foo.id | foo.name | count | array_agg
--------------------------------------------------
1 | one | 2 | {a, b}
2 | two | 1 | {c}
The count
is always the count of tags instead of the count of distinct foo_bar
values. I've tried reordering/modifying the GROUP BY
and the SELECT
clauses which returns different results but not the ones that I'm looking for. I think I'm having trouble with the array_agg()
function, but I'm not sure if that's the case or how to resolve it.