【发布时间】:2021-04-02 15:51:51
【问题描述】:
我已经用尽了所有的选择。我不明白我哪里出了问题。请帮我。我不能接受更多的头撞。这就是我所在的地方...
下面是我的表结构的解释。
locations_management表
包含将locations_management_id、season_id、location_id和其他不需要的信息链接在一起的数据
订单表
包含有关订单的数据,包括 location_management_id 和 order_id 以及其他不需要的信息
orders_products 表
通过 order_id 链接到订单的产品数据。此表仅使用以下列:order_product_id、order_id、product_id、piece_qty
orders_adjustments表
用于跟踪对 inv_shipped 的任何调整。此表使用 order_id、product_id、piece_qty 列
这就是我今天所处的位置。下面的查询从上面的表中提取数据。
基本上,我要求从 locations_management 表 WHERE season_id = 12 AND location_id = 35 中获取 location_management_id。可以有多个可能的 location_management_id 同时适合 season_id 和 location_id。然后我需要找到与这些 location_management_id(s) 匹配的订单。找到订单后,我需要使用 order_id(s) 在 orders_products 表中查找与其关联的产品。 这个查询正是这样做的,但是当我进一步合并/求和时 piece_qty 为总 inv_shipped,数字发生了疯狂的事情。
SELECT
locations_management.season_id,
locations_management.location_id,
orders.order_id,
orders_products.product_id,
IFNULL((orders_products.piece_qty), 0) AS inv_shipped,
IFNULL((orders_adjustments.piece_qty), 0) AS inv_adjustments
FROM
locations_management
JOIN orders USING (location_management_id)
LEFT JOIN orders_products USING (order_id)
LEFT JOIN orders_adjustments ON (orders_adjustments.order_id = orders_products.order_id) AND (orders_adjustments.product_id = orders_products.product_id)
WHERE
locations_management.season_id = 12 AND locations_management.location_id = 35
GROUP BY
product_id, orders_products.order_id
当我运行上面的查询时,这就是我得到的......
season_id location_id order_id product_id inv_shipped inv_adjustments
12 35 2127 1 220 0
12 35 2194 1 160 0
12 35 2127 3 312 0
12 35 2127 4 24 0
12 35 2127 5 180 0
12 35 2194 5 24 0
12 35 2127 7 144 0
12 35 2127 7 24 0
这正是我期望得到的。多个 order_id 由 product_id 分组,所有数据都是准确的。所以现在这里成了问题。我想在 product_id 匹配并具有组合的 inv_shipped 时将它们添加/求和。所以 product_id 1 现在 inv_shipped 的总数为 380。
当我从上面获取相同的查询并将 SUM 添加到 inv_shipped 和 inv_adjustments(如下所示)时,我会在下面得到这个数据输出。注意一些值是如何翻倍的,但匹配的 product_id 行也没有合并。
IFNULL(SUM(orders_products.piece_qty), 0) AS inv_shipped,
IFNULL(SUM(orders_adjustments.piece_qty), 0) AS inv_adjustments
season_id location_id order_id product_id inv_shipped inv_adjustments
12 35 2127 1 440 0
12 35 2194 1 160 0
12 35 2127 3 624 0
12 35 2127 4 48 0
12 35 2127 5 360 0
12 35 2194 5 24 0
12 35 2127 7 288 0
12 35 2127 7 24 0
如果我只将 GROUP BY 更改为 product_id,我会得到以下数据:
GROUP BY product_id
season_id location_id order_id product_id inv_shipped inv_adjustments
12 35 2127 1 600 0
12 35 2127 3 624 0
12 35 2127 4 48 0
12 35 2127 5 384 0
12 35 2127 7 312 0
同样,这些 inv_shipped 总数不正确。那我哪里错了?
------------------------------------ 建议 ---------- --------------------------
建议使用以下查询,但 inv_shipped 的数据输出也未正确添加。
SELECT
locations_management.season_id,
locations_management.location_id,
orders.order_id,
products.product_id,
products.inv_shipped
FROM
locations_management
JOIN (SELECT location_management_id, order_id FROM orders group by order_id) AS orders ON orders.location_management_id = locations_management.location_management_id
JOIN (SELECT order_id, product_id, IFNULL(SUM(piece_qty), 0) AS inv_shipped FROM orders_products GROUP BY order_id, product_id) AS products ON products.order_id = orders.order_id
WHERE
locations_management.season_id = 12 AND locations_management.location_id = 35
ORDER BY
product_id, order_id
season_id location_id order_id product_id inv_shipped inv_adjustments
12 35 2127 1 440 0
12 35 2194 1 160 0
12 35 2127 3 624 0
12 35 2127 4 48 0
12 35 2127 5 360 0
12 35 2194 5 24 0
12 35 2127 7 288 0
12 35 2127 7 24 0
【问题讨论】:
-
在
products子查询中聚合(计算每个 product_id 的 SUM),而不是在外部查询中。 -
您好,谢谢您,我已经尝试过此更改。我得到相同的结果。请参阅下面的我的 cmets 到 tcadidot0 的帖子。我将详细介绍该问题以及解决问题时可以看到的内容。
-
请在代码问题中给出minimal reproducible example--cut & paste & runnable code,包括最小的代表性示例输入作为代码;期望和实际输出(包括逐字错误消息);标签和版本;明确的规范和解释。对于包含 DBMS 和 DDL(包括约束和索引)和输入为格式化为表的代码的 SQL。 How to Ask 以意外结果切入第一个子表达式并解释您的期望和原因,并提供理由参考文档。
-
在扩展到问题代码之前(几乎)提供工作代码非常好,但是在您的 2 个连接之后首先使用 select * 来查看您正在聚合的内容。这里的第一条评论是正确的。这有一个常见的错误,人们想要一些连接,每个可能涉及不同的键,一些子查询,每个可能涉及连接和/或聚合,但他们错误地尝试先进行所有连接,然后再进行所有聚合或聚合以前的聚合.在适当的行上写单独的总和和/或总结一个案例语句选择行;加入常见的唯一列集。
-
在给出关系(船舶)/关联或表(基本或查询结果)时,说明其中的一行根据其列值说明了业务情况。查询不需要约束,表含义(根据前面的句子)是足够和必要的。但是,如果查询依赖于某些约束,请给出它们。当你得到一个意外/“错误”的结果时,暂停你的总体目标并调试你的误解。 PS“基本上”或“基本上”或“换句话说”,没有引入或总结清晰、准确和完整的描述,也只是表示“不清楚”。