鉴于以下结构
CREATE TABLE products (
id integer NOT NULL,
subcategory_id integer,
stack_id integer,
)
CREATE TABLE subcategories (
id integer NOT NULL,
name character varying(255)
)
其中products.stack_id是回归产品的自我参照关系。
我基本上试图计算子类别加入产品
products.subcategory_id = subcategories.id
但是每个不同的堆栈组将计数限制为一次。
样本子类别表
id name
1 subcategory_1
2 subcategory_2
3 subcategory_3
样本产品表
id subcategory_id stack_id
1 1 NULL
2 1 1
3 2 1
4 3 1
5 2 NULL
6 2 5
7 2 5
8 2 NULL
9 3 8
10 3 8
示例所需的输出
id name total
1 subcategory_1 1 (row 1)
2 subcategory_2 3 (row 1 + row 5 + row 8)
3 subcategory_3 2 (row 1 + 8)
输出说明
子类别ID 1
如果我与产品进行简单的连接,我会得到产品(1,2)。我只想要不同父对象的数量(stack_id为null),因此1个计数和2个已经计数的引用1因此不会增加计数。
子类别ID 2
加入将是(3,5,6,7,8)。 3的stack_id是1所以它计数1.产品5,6和7参考5,因此计数1.产品8计数1.
子类别3
加入是(4,9,10)。 4参考文献1,9和10都参考了8.
更新
删除了额外可能令人困惑的列,添加了样本数据和输出
答案 0 :(得分:2)
如果引用的最大深度是一个级别,那么这个简单的查询就完成了这项工作:
select subcategory_id, name, count(*)
from (
select distinct subcategory_id, coalesce(stack_id, id) stack_id
from products
) sub
join subcategories s on s.id = sub.subcategory_id
group by 1, 2
order by 1, 2;
subcategory_id | name | count
----------------+---------------+-------
1 | subcategory_1 | 1
2 | subcategory_2 | 3
3 | subcategory_3 | 2
(3 rows)
此递归查询也适用于深度超过一个级别的引用:
with recursive pr(id, subcategory_id, stack_id, stack) as (
select id, subcategory_id, stack_id, array[id]
from products
union
select pr.id, pr.subcategory_id, products.stack_id, pr.stack_id || pr.stack
from pr
join products on pr.stack_id = products.id
)
select distinct on (id) id, subcategory_id, stack
from pr
order by id, array_length(stack, 1) desc
id | subcategory_id | stack
----+----------------+--------
1 | 1 | {1}
2 | 1 | {1,2}
3 | 2 | {1,3}
4 | 3 | {1,4}
5 | 2 | {5}
6 | 2 | {5,6}
7 | 2 | {5,7}
8 | 2 | {8}
9 | 3 | {8,9}
10 | 3 | {8,10}
(10 rows)
使用上述数据集加入子类别:
select subcategory_id, name, count(*)
from (
select distinct subcategory_id, stack[1]
from (
with recursive pr(id, subcategory_id, stack_id, stack) as (
select id, subcategory_id, stack_id, array[id]
from products
union
select pr.id, pr.subcategory_id, products.stack_id, pr.stack_id || pr.stack
from pr
join products on pr.stack_id = products.id
)
select distinct on (id) id, subcategory_id, stack
from pr
order by id, array_length(stack, 1) desc
) sub
) sub
join subcategories s on s.id = sub.subcategory_id
group by 1, 2
order by 1, 2
subcategory_id | name | count
----------------+---------------+-------
1 | subcategory_1 | 1
2 | subcategory_2 | 3
3 | subcategory_3 | 2
(3 rows)