搜索

首页  >  问答  >  正文

将递归 CTE 结果合并到辅助 SQL Select 中

我有一个复杂的 SQL 查询,可以成功提取组件产品记录的成本并计算父/捆绑产品的总体成本。当每个组件都有供应商成本并且本身不是父/捆绑产品时,这种方法就有效。

查询#1

SET @parentid = 36;
SELECT
    sub.product_sku AS product_sku,
    sub.product_label AS product_label,
    c2p.bundle_parentid AS bundle_parentid,
    c2p.componentid AS comp_product_id,
    sub.qty AS qty,
    sub.preferred AS preferred,
    sub.supply_qty AS supply_qty,
    sub.cost AS cost,
    ROUND((SELECT cost)/(SELECT supply_qty),2) AS adjusted_cost
    FROM products AS p
    JOIN component2bundle AS c2p ON c2p.componentid = p.product_id
    JOIN (
        /* Get the preferred/cheapest supplier date for this component */
        SELECT
            p2.product_sku AS product_sku,
            p2.product_label AS product_label,
            IFNULL(s2p2.cost, NULL) AS cost,
            s2p2.productid AS product_id,
            s2p2.supplier_preferred AS preferred,
            s2p2.product_quantity AS supply_qty,
            c2p2.componentid AS comp_product_id,
            c2p2.component_quantity AS qty,
            c2p2.bundle_parentid AS bundle_parentid
            FROM products AS p2
                INNER JOIN supplier2product AS s2p2 ON s2p2.productid = p2.product_id
                INNER JOIN component2bundle AS c2p2 ON s2p2.productid = c2p2.componentid
            WHERE c2p2.bundle_parentid = @parentid
                 AND c2p2.c2p_archive = 0
                AND COALESCE(s2p2.s2p_archive,0) = 0
            ORDER BY c2p2.componentid ASC, s2p2.supplier_preferred DESC, s2p2.cost ASC
    ) AS sub
    ON (sub.product_id = c2p.componentid)
    WHERE c2p.bundle_parentid = @parentid;

我的目标是调整或重写查询,以便它可以消除任何捆绑组件的成本,因此递归 CTE 查询似乎是继续的方法。

我已经成功编写了一个 CTE 查询,该查询可以从显示父 -> 子关系的表中提取每个组件产品 ID,并为每个组件分配层次结构中的级别。我正在努力解决的是如何将两者整合起来。

CTE查询

WITH RECURSIVE components AS 
(
    SELECT componentid, 1 AS level
    FROM component2bundle
    WHERE bundle_parentid = 'target_productID'
    UNION ALL
        SELECT c2b.componentid, c.level+1
        FROM components c, component2bundle c2b
        WHERE c2b.bundle_parentid = c.componentid
)
SELECT * FROM components ORDER BY level DESC;

我在这里创建了一个 MySQL 8.0 fiddle 以帮助提供更好的上下文:

https://dbfiddle.uk/M6HT_R13

注意:我已经削减了查询#1,使其更容易处理,因此可以忽略小提琴中的某些字段。

*编辑:在fiddle中设置parentid变量以查看当前查询如何拉取:

一些附加说明。

查询#1中的子查询旨在从supplier2cost表中提取首选或(如果未设置)最低的供应商成本,而我不确定如何在其中实现这个子查询CTE 上下文(如果有的话)。

如果其他上下文有帮助,请询问,我将编辑查询以提供该信息。

预期/预期输出

ProductSKU 产品标签 BundleParentID Component_ID 级别 数量 首选 供应数量 成本 调整后的成本
子组件#1 CMP#2 36 35 2 1 1 1 费用 每单位成本
子组件#2 CMP#3 36 37 2 1 1 1 费用 每单位成本
子组件#3 CMP#4 36 38 2 1 1 1 费用 每单位成本
组件#1 CMP#1 34 33 1 1 1 1 费用 每单位成本
子包 外滩#1 36 33 1 1 1 1 费用 每单位成本

数据最终将用于提供如下组件成本表:

P粉573809727P粉573809727321 天前394

全部回复(1)我来回复

  • P粉675258598

    P粉6752585982024-01-17 10:16:36

    您可能想要这样的东西:

    在第二个 cte 上,添加了一个条件,将您的主体查询与递归查询连接起来,以仅提取选定的查询

    SET @parentid = 34;
    WITH RECURSIVE components AS 
    (
        SELECT componentid, p.product_sku, 1 AS level
        FROM component2bundle
        JOIN products p ON componentid = p.product_id
        WHERE bundle_parentid = @parentid
        UNION ALL
            SELECT c2b.componentid, product_sku, c.level+1
            FROM components c, component2bundle c2b
            WHERE c2b.bundle_parentid = c.componentid
    ),
    CTE AS (
        SELECT
        sub.product_sku AS product_sku,
        sub.product_label AS product_label,
        c2p.bundle_parentid AS bundle_parentid,
        c2p.componentid AS comp_product_id,
        sub.qty AS qty,
        sub.preferred AS preferred,
        sub.supply_qty AS supply_qty,
        sub.cost AS cost,
        ROUND((SELECT cost)/(SELECT supply_qty),2) AS adjusted_cost,
        c.level
        FROM products AS p
        JOIN component2bundle AS c2p ON c2p.componentid = p.product_id
        JOIN components c on c.componentid = c2p.componentid
        JOIN (
            /* Get the preferred/cheapest supplier date for this component */
            SELECT
                p2.product_sku AS product_sku,
                p2.product_label AS product_label,
                IFNULL(s2p2.cost, NULL) AS cost,
                s2p2.productid AS product_id,
                s2p2.supplier_preferred AS preferred,
                s2p2.product_quantity AS supply_qty,
                c2p2.componentid AS comp_product_id,
                c2p2.component_quantity AS qty,
                c2p2.bundle_parentid AS bundle_parentid
                FROM products AS p2
                    INNER JOIN supplier2product AS s2p2 ON s2p2.productid = p2.product_id
                    INNER JOIN component2bundle AS c2p2 ON s2p2.productid = c2p2.componentid
                WHERE c2p2.c2p_archive = 0
                    AND COALESCE(s2p2.s2p_archive,0) = 0
                ORDER BY c2p2.componentid ASC, s2p2.supplier_preferred DESC, s2p2.cost ASC
        ) AS sub
        ON (sub.product_id = c2p.componentid)
    )
    SELECT *
    FROM CTE c
    WHERE preferred = 1

    此处演示

    回复
    0
  • 取消回复