Table tree
is a sample table with ancestors array in PostgreSQL 8.3+:
----+-----------
id | ancestors
----+-----------
1 | {}
2 | {1}
3 | {1,2}
4 | {1}
5 | {1,2}
6 | {1,2}
7 | {1,4}
8 | {1}
9 | {1,2,3}
10 | {1,2,5}
for to get each id count number of descendant, I can do this:
SELECT 1 AS id, COUNT(id) AS descendant_count FROM tree WHERE 1 = ANY(ancestors)
UNION
SELECT 2 AS id, COUNT(id) AS descendant_count FROM tree WHERE 2 = ANY(ancestors)
UNION
SELECT 3 AS id, COUNT(id) AS descendant_count FROM tree WHERE 3 = ANY(ancestors)
UNION
SELECT 4 AS id, COUNT(id) AS descendant_count FROM tree WHERE 4 = ANY(ancestors)
UNION
SELECT 5 AS id, COUNT(id) AS descendant_count FROM tree WHERE 5 = ANY(ancestors)
UNION
SELECT 6 AS id, COUNT(id) AS descendant_count FROM tree WHERE 6 = ANY(ancestors)
UNION
SELECT 7 AS id, COUNT(id) AS descendant_count FROM tree WHERE 7 = ANY(ancestors)
UNION
SELECT 8 AS id, COUNT(id) AS descendant_count FROM tree WHERE 8 = ANY(ancestors)
UNION
SELECT 9 AS id, COUNT(id) AS descendant_count FROM tree WHERE 9 = ANY(ancestors)
UNION
SELECT 10 AS id, COUNT(id) AS descendant_count FROM tree WHERE 10 = ANY(ancestors)
and get result as:
----+------------------
id | descendant_count
----+------------------
1 | 9
2 | 5
3 | 1
4 | 1
5 | 1
6 | 0
7 | 0
8 | 0
9 | 0
10 | 0
I guess it should exist that shorter or smart query statement to get same result, is it possible? Maybe like WITH RECURSIVE
or create function with loop to generate query?