1

机がある:

CREATE TABLE product_categories (
id INT NOT NULL PRIMARY KEY,
parent INT NOT NULL,
name varchar(50) NOT NULL,
isProduct boolean NOT NULL
);

各カテゴリーの製品数を計算する方法はありますか?

あれは:

INSERT INTO product_categories VALUES (1, NULL, 'Main', 'no');

INSERT INTO product_categories VALUES (2, 1, 'Plant', 'no');
INSERT INTO product_categories VALUES (3, 2, 'Cactus', 'yes');
INSERT INTO product_categories VALUES (4, 2, 'Spruce', 'yes');
INSERT INTO product_categories VALUES (5, 2, 'Birch', 'yes');
INSERT INTO product_categories VALUES (6, 2, 'Pine', 'yes');

INSERT INTO product_categories VALUES (7, 1, 'Stock', 'no');
INSERT INTO product_categories VALUES (8, 7, 'Spade', 'yes');
INSERT INTO product_categories VALUES (9, 7, 'Watering can', 'yes');

受け取る必要があります:

Category | Count
Main     | 6
Plant    | 4
Stock    | 2
4

1 に答える 1

4

再帰共通テーブル式を使用する必要があります

WITH RECURSIVE Parents AS
(   SELECT  ID, Parent, Name, IsProduct
    FROM    product_categories
    WHERE   Parent IS NOT NULL
    UNION ALL
    SELECT  c.ID, p.Parent, c.Name, c.IsProduct
    FROM    product_categories c
            INNER JOIN Parents p
                ON p.ID = c.Parent
)
SELECT  pc.Name, 
        COUNT(*) AS Products, 
        ARRAY_AGG(p.Name) AS ProductList
FROM    product_categories pc
        INNER JOIN Parents p
            ON p.Parent = pc.ID
WHERE   p.IsProduct = 'yes'
GROUP BY pc.Name

実例

于 2012-07-20T13:54:50.423 に答える