gpt4 book ai didi

MySQL:分组

转载 作者:行者123 更新时间:2023-11-30 21:44:15 30 4
gpt4 key购买 nike

我想按 m.ID 分组,并对每个 m.id 求和 (pm.amount_construction* prod.anzahl)

实际上我有以下结果:

Meterial_id | amount_construction | Anzahl_der_productionen | Total_amount---------------------------------------------------------------------------181         | 1                   | 10                      | 10181         | 2                   | 20                      | 40181         | 1                   | 100                     | 100182         | 1                   | 10                      | 10182         | 1                   | 20                      | 20183         | 1                   | 100                     | 100283         | 1                   | 5                       | 5283         | 1                   | 10                      | 10283         | 2                   | 100                     | 200283         | 10                  | 55                      | 550

What I need is the following (column 2 and 3 are not necessary)

Meterial_id | Total_amount---------------------------181         | 150182         | 30 183         | 100283         | 765

I tried Group By m.id and did sum(pm.amount_construction * prod.anzahl)but I got strange results

Please see my code below:

SELECT
m.id As Meterial_id,
pm.amount_construction AS amount_construction,
prod.anzahl as Anzahl_der_productionen,
(pm.amount_construction* prod.anzahl) as Total_amount

FROM produktion prod
JOIN projekt p
ON prod.projekt = p.id
JOIN kunden k
ON k.id = p.kunde
LEFT JOIN bauteil b
ON b.produktion = prod.id
JOIN product_material pm
JOIN material m
ON m.id = pm.material_id
JOIN product_configurationelement pce
ON pce.id = pm.product_configurationelement_id
JOIN product_configurationelement_value pcev
ON pcev.product_configurationelement_id = pm.product_configurationelement_id
AND pcev.value_nr = pm.value_nr
JOIN product_type pt
JOIN produkt pd
ON pd.id = prod.produkt
JOIN product_generation pg
ON pg.product_type_id = pt.id
JOIN product_modification pmm
ON pmm.product_generation_id = pg.id
WHERE (prod.anzahl > 0 and pt.id = pd.product_type_id AND pm.product_generation_id = pd.product_generation_id AND pg.generation = pd.generation AND p.starttermin <= '2018-07-05')
ORDER BY m.id

最佳答案

许多加入。其中一些无效。 JOININNER JOIN 的缩写,需要一个 ONUSING 子句。然而,当您忘记该子句时,MySQL 会默默地将 JOIN 转换为 CROSS JOIN。然后可能会导致可怕的错误,但是您在 WHERE 子句中隐藏了条件,因此据我所知,所有表似乎都已正确连接。

无论如何,很多连接是没有必要的。例如,为什么加入 bauteil?每个produktion 可以有很多bauteil,因此您将结果乘以bauteil 的数量。我猜与 product_configurationelementproduct_configurationelement_valueproduct_generationproduct_modification 相同。然后是 kundenprodukt 等多余的连接。

如果我没记错的话,这应该是您需要的:所有 product_materialproduktion 结合,项目匹配日期范围,按 material_id< 分组 为每种 Material 获取一个结果行。

SELECT
pm.material_id,
SUM(pm.amount_construction * produktion.anzahl) as total_amount
FROM product_material pm
JOIN produkt p ON p.product_generation_id = pm.product_generation_id
JOIN produktion ON produktion.produkt = p.id and produktion.anzahl > 0
WHERE produktion.projekt IN (SELECT id FROM projekt WHERE starttermin <= date '2018-07-05')
GROUP BY pm.material_id
ORDER BY pm.material_id;

关于MySQL:分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50180389/

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