0

非常标准的设置,包含产品、类别和类别中的产品表,但我想知道是否有更好/更有效的方法来获取产品的几个顶级类别(我真的不想把它们单独带回来行,因此内部连接不可行)。

我目前拥有的 SQL 是:

 SELECT p.*,
  (SELECT TOP 1 category_name FROM categories c INNER JOIN products_and_categories pc ON pc.category_id = c.category_id WHERE p.product_id = pc.product_id AND category_parent = 100 ORDER BY sort) AS cat_1,
  (SELECT TOP 1 category_name FROM categories c INNER JOIN products_and_categories pc ON pc.category_id = c.category_id WHERE p.product_id = pc.product_id AND category_parent = 200 ORDER BY sort) AS cat_2,
  (SELECT TOP 1 category_name FROM categories c INNER JOIN products_and_categories pc ON pc.category_id = c.category_id WHERE p.product_id = pc.product_id AND category_parent = 500 ORDER BY sort) AS cat_3,
  (SELECT TOP 1 category_name FROM (SELECT TOP 2 c.* FROM categories c INNER JOIN products_and_categories pc ON pc.category_id = c.category_id WHERE p.product_id = pc.product_id AND category_parent = 500  ORDER BY sort) c1 ORDER BY sort DESC) AS cat_4,
  (SELECT TOP 1 category_name FROM categories c INNER JOIN products_and_categories pc ON pc.category_id = c.category_id WHERE p.product_id = pc.product_id AND category_parent = 50 ORDER BY sort) AS cat_5,
  (SELECT TOP 1 category_name FROM (SELECT TOP 2 c.* FROM categories c INNER JOIN products_and_categories pc ON pc.category_id = c.category_id WHERE p.product_id = pc.product_id AND category_parent = 50  ORDER BY sort) c1 ORDER BY sort DESC) AS cat_6
FROM products AS p

我很高兴以这种方式写了几篇专栏文章,但想知道是否还有其他方法可以做到这一点?通过加入 PIVOT 表或其他我没有想到的东西,或者我是否应该接受这样做。

还有一些其他限制(可能重要也可能不重要):

  • 一些子查询可能不会带回任何结果
  • 我需要前 2 个类别的一些行(如上面的代码所示)
4

1 回答 1

0

由于您的相关子查询只有简单的等于条件,您可以将它们移动到连接中,而不是使用TOP 1您可以ROW_NUMBER()用来对类别进行排序,然后只为每个 product_ID/Parent_category 组合选择前 1 个,或者在您的两个字段,选择第二个类别。

我认为这对你有用:

WITH ProductCategories AS
(   SELECT  pc.Product_ID,
            c.category_name,
            category_parent,
            RowNum = ROW_NUMBER() OVER(PARTITION BY pc.Product_ID, category_parent ORDER BY Sort)
    FROM    categories c 
            INNER JOIN products_and_categories pc 
                ON pc.category_id = c.category_id   
), MaxProductCategories AS
(   SELECT  Product_ID,
            [Cat_1] = MAX(CASE WHEN RowNum = 1 AND category_parent = 100 THEN category_name END),
            [Cat_2] = MAX(CASE WHEN RowNum = 1 AND category_parent = 200 THEN category_name END),
            [Cat_3] = MAX(CASE WHEN RowNum = 1 AND category_parent = 500 THEN category_name END),
            [Cat_4] = MAX(CASE WHEN RowNum = 2 AND category_parent = 500 THEN category_name END),
            [Cat_5] = MAX(CASE WHEN RowNum = 1 AND category_parent = 50 THEN category_name END),
            [Cat_6] = MAX(CASE WHEN RowNum = 2 AND category_parent = 50 THEN category_name END)
    FROM    ProductCategories
    WHERE   RowNum IN (1, 2)
    AND     category_parent IN (50, 100, 200, 500)
    GROUP BY Product_ID
)
SELECT  p.*,
        mpc.Cat_1,
        mpc.Cat_2,
        mpc.Cat_3,
        mpc.Cat_4,
        mpc.Cat_5,
        mpc.Cat_6
FROM    products p
        LEFT JOIN MaxProductCategories mpc
            ON mpc.Product_ID = p.Product_ID;
于 2013-06-11T08:36:35.223 回答