如何使用SQL显示具有类别的产品数量

时间:2019-08-18 11:24:33

标签: sql sql-server database

我正在使用以下代码显示类别+产品数量:

SELECT 
    c.category_name, COUNT(p.category_id) AS product_count 
FROM 
    categories AS c
LEFT JOIN 
    products AS p ON p.category_id = c.category_id
WHERE 
    c.sub_category_id = '@variable' 
GROUP BY 
    c.category_name

结果是这样的:

Category_1 (0)
Category_2 (0)

因为您可以单击子类别中的产品类别。这意味着产品在数据库中具有sub_category_id而不是主要的category_id。

  Sub_Category_1 (20)
  Sub_Category_2 (5)

类别表:

category_id, category_name,   sub_category_id
---------------------------------------------
1            Category_1             0
2            Category_2             0
3            Sub_Category_1         1   >> Category_1
4            Sub_Category_2         2   >> Category_2  
5            Sub_Category_1_Sub     3   >> Sub_Category_1
6            Sub_Category_2_Sub     4   >> Sub_Category_2

产品表:

product_id  category_id
------------------------    
1             3
2             4
3             3
4             4
5             5
6             6

我想显示主要类别上的产品总数。

因为您看到Category_1和Category_2返回(0)

有什么想法吗?

预期结果:

Categories (30)
  Sub Categories_1(15)
    Sub_Categories_Sub_1(10)
    Sub_Categories_Sub_2(5)
  Sub Categories_2(15) 
    Sub_Categories_Sub_1(2)
    Sub_Categories_Sub_2(8)
    Sub_Categories_Sub_2(5)

更新 2019年8月18日-17:15

2 个答案:

答案 0 :(得分:1)

首先,您需要获取与特定类别相关的所有子类别。为此,您可以使用递归cte。
然后,在该递归cte和产品表之间使用左联接,并按根类别对产品组进行计数。
这是一个代码示例:

首先,创建并填充示例表(在您将来的问题中为我们保存此步骤):

DECLARE @Categories AS TABLE
(
    category_id int, 
    category_name varchar(50),   
    sub_category_id int
)
INSERT INTO @Categories (category_id, category_name, sub_category_id) VALUES
(1, 'Category_1'        , 0),
(2, 'Category_2'        , 0),
(3, 'Sub_Category_1'    , 1),
(4, 'Sub_Category_2'    , 2),
(5, 'Sub_Category_1_Sub', 3),
(6, 'Sub_Category_2_Sub', 4);

DECLARE @Products AS TABLE
(
    product_id  int,
    category_id int
)

-- comments to make it clear which product belongs which main category
INSERT INTO @Products (product_id, category_id) VALUES
(1, 3), -- Category_1
(2, 4), -- Category_2
(3, 3), -- Category_1
(4, 4), -- Category_2
(5, 5), -- Category_1
(6, 5); -- Category_1

然后,递归cte:

WITH CTE AS
(
    SELECT category_id, category_name, sub_category_id, category_name As MainCategoryName
    FROM @Categories 
    WHERE sub_category_id = 0
    -- If you want to start from a particular subCategory you can change the where condition:
    -- for instance, `where category_id = 3` will count the products the belongs to Sub_Category_1 and Sub_Category_1_Sub
    UNION ALL

    SELECT T.category_id, T.category_name, T.sub_category_id, MainCategoryName
    FROM @Categories AS T
    JOIN CTE
        ON T.sub_category_id = CTE.category_id
)

查询:

SELECT MainCategoryName As CategoryName, COUNT(P.product_id) As NumberOfProducts
FROM CTE
LEFT JOIN @Products As P
    ON P.category_id = CTE.category_id 
GROUP BY MainCategoryName 

结果:

CategoryName    NumberOfProducts
Category_1      4
Category_2      2

You can see a live demo on rextester.

答案 1 :(得分:0)

这是您想要的吗?

您只想在分组依据中添加category_id,然后进行如下计数

   SELECT category_id ,c.category_name, 
  count(*) AS product_count FROM 
     categories AS c
     LEFT JOIN products AS p ON
  p.category_id = c.category_id
  WHERE c.sub_category_id='@variable' 
     GROUP BY category_id, 
     c.category_name