gpt4 book ai didi

mysql - COALESCE 与 SUM 与 Join 表的计算不正确

转载 作者:行者123 更新时间:2023-11-29 06:43:48 24 4
gpt4 key购买 nike

我正在研究将 5 个不同表联合在一起的连接表因为我已经为联合所有表编写了代码,并且无需连接表即可正常工作

正如你所看到的代码

    SELECT  sku,
sum(units_ordered) as units_ordered
FROM (
SELECT sku, units_ordered
FROM tbl_sku_units_order
UNION ALL
SELECT sku, units_ordered
FROM tbl_sku_units_order_de
UNION ALL
SELECT sku, units_ordered
FROM tbl_sku_units_order_es
UNION ALL
SELECT sku, units_ordered
FROM tbl_sku_units_order_fr
UNION ALL
SELECT sku, units_ordered
FROM tbl_sku_units_order_it
) AllMarketplace
group by sku
ORDER BY units_ordered DESC;

正如您可以在 db fiddle 上的第一个链接中看到结果

https://www.db-fiddle.com/f/nbGcxACaf4doHRssJDSyRN/0结果是正确的

enter image description here

您可以看到“H-Root-M012S-Black 87”,这是正确的库存总数。

我需要它们作为左连接与其他表之一连接。这是 db fiddle 中的代码

https://www.db-fiddle.com/f/dxqsU4enjRYqk7du7VEB4d/0

这是我写的代码

SELECT 
ls.sku AS list_sku,
COALESCE(MIN(suo.sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(suo.units_ordered), 0) AS units_ordered
FROM
(SELECT
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order
UNION ALL
SELECT
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_de
UNION ALL
SELECT
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_es
UNION ALL
SELECT
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_fr
UNION ALL
SELECT
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_it
) as t1,
tbl_list_sku AS ls
LEFT JOIN
tbl_sku_units_order AS suo ON suo.sku = ls.sku
GROUP BY ls.sku
ORDER BY units_ordered DESC;

返回正常,但计算错误

enter image description here

我怀疑是因为群组问题?我必须在组上调整它,但所有结果都返回相同的结果以及右连接表或内连接表。

最佳答案

你的 SQL 应该是这样的:

SELECT 
ls.sku AS list_sku,
COALESCE(MIN(suo.sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(suo.units_ordered), 0) AS units_ordered
FROM
(SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_de
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_es
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_fr
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_it
) as t1 join
tbl_list_sku AS ls on t1.sku = ls.sku
LEFT JOIN
tbl_sku_units_order AS suo ON suo.sku = ls.sku
GROUP BY ls.sku
ORDER BY units_ordered DESC;

编辑-1

SELECT 
ls.sku AS list_sku,
COALESCE(MIN(suo.sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(suo.units_ordered), 0) AS units_ordered
FROM tbl_list_sku AS ls left join
(SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_de
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_es
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_fr
UNION ALL
SELECT sku,
COALESCE(MIN(sku), 'Not Sold') AS sold_sku,
COALESCE(SUM(units_ordered), 0) AS units_ordered
FROM tbl_sku_units_order_it
) as t1 on t1.sku = ls.sku
LEFT JOIN
tbl_sku_units_order AS suo ON suo.sku = ls.sku
GROUP BY ls.sku
ORDER BY units_ordered DESC;

关于mysql - COALESCE 与 SUM 与 Join 表的计算不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50387641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com