MYSQL复杂GROUP BY和SUM查询失败

零和一

我有一个复杂的查询,它获取当前的库存水平并将单位转换为磅,然后查看未完成的订单并将单位转换为磅,最后显示所有未完成的订单每种成分所需的总金额。

目的是列出具有库存水平的成分,然后列出满足所有未完成订单所需的数量。如果面包师没有足够的面粉,那么他可以看到这一点,并在开始一天的烘烤之前考虑寻找更多的面粉。

问题是,当我运行此查询时,它正在跳过组函数中的第一行。当我不对其进行分组而运行时,它会使所有行正确,但是当我使用SUM和GROUP BY运行它时,它会跳过第一行,因此它会根据计算得出所需的项目数量。有小费吗?谢谢!

SELECT 
    inventory.id, 
    inventory.title, 
    products.weight, 
    products.id AS product_id, 
    (orders_items.quantity - orders_items.quantity_baked) AS quantity_to_be_baked,
    (SELECT @inventory_pounds:=
        CASE inventory.units
            WHEN  'kilograms'
            THEN 2.20462 * inventory.quantity
            WHEN  'pounds'
            THEN 1 * inventory.quantity
            WHEN  'ounces'
            THEN 0.0625 * inventory.quantity
            WHEN  'grams'
            THEN 0.00220462 * inventory.quantity
        END ) as inventory_pounds,
    (SELECT @dough_recipe_ingredient_pounds:=
        CASE dough_recipes.units
            WHEN 'kilograms'
            THEN 2.20462 * dough_recipes.amount
            WHEN 'pounds'
            THEN 1 * dough_recipes.amount
            WHEN 'ounces'
            THEN 0.0625 * dough_recipes.amount
            WHEN 'grams'
            THEN 0.00220462 * dough_recipes.amount
        END ) AS dough_recipe_ingredient_pounds, 
    (orders_items.quantity - orders_items.quantity_baked) AS num_loaves_needed,
    ( SELECT @dough_recipe_yield_pounds:=
        CASE doughs.units
            WHEN 'kilograms'
            THEN 2.20462 * doughs.yield
            WHEN 'pounds'
            THEN 1 * doughs.yield
            WHEN 'ounces'
            THEN 0.0625 * doughs.yield
            WHEN 'grams'
            THEN 0.00220462 * doughs.yield
        END ) AS dough_recipe_yield_pounds,
    (SELECT SUM( @dough_recipe_ingredient_pounds / @dough_recipe_yield_pounds * weight * (orders_items.quantity - orders_items.quantity_baked))) as amount_needed_for_orders
    FROM inventory
    LEFT JOIN dough_recipes ON inventory.id = dough_recipes.inventory_id
    LEFT JOIN products ON dough_recipes.dough_id = products.dough_id
    LEFT JOIN orders_items ON products.id = orders_items.product_id AND (orders_items.quantity - orders_items.quantity_baked) > 0
    LEFT JOIN doughs ON doughs.id = products.dough_id
    GROUP BY id
花哨的裤子

Group by不跳过行或任何内容。您的查询结构似乎是错误的。尝试一下:

SELECT SUM(dough_recipe_ingredient_pounds / dough_recipe_yield_pounds * weight * (quantity - quantity_baked))) as amount_needed_for_orders
FROM (
    SELECT 
    inventory.id, 
    inventory.title, 
    products.weight, 
    products.id AS product_id, 
    orders_items.quantity,
    orders_items.quantity_baked,
    (orders_items.quantity - orders_items.quantity_baked) AS quantity_to_be_baked,
        CASE inventory.units
            WHEN  'kilograms'
            THEN 2.20462 * inventory.quantity
            WHEN  'pounds'
            THEN 1 * inventory.quantity
            WHEN  'ounces'
            THEN 0.0625 * inventory.quantity
            WHEN  'grams'
            THEN 0.00220462 * inventory.quantity
        END as inventory_pounds,
        CASE dough_recipes.units
            WHEN 'kilograms'
            THEN 2.20462 * dough_recipes.amount
            WHEN 'pounds'
            THEN 1 * dough_recipes.amount
            WHEN 'ounces'
            THEN 0.0625 * dough_recipes.amount
            WHEN 'grams'
            THEN 0.00220462 * dough_recipes.amount
        END  AS dough_recipe_ingredient_pounds, 
    (orders_items.quantity - orders_items.quantity_baked) AS num_loaves_needed,
        CASE doughs.units
            WHEN 'kilograms'
            THEN 2.20462 * doughs.yield
            WHEN 'pounds'
            THEN 1 * doughs.yield
            WHEN 'ounces'
            THEN 0.0625 * doughs.yield
            WHEN 'grams'
            THEN 0.00220462 * doughs.yield
        END AS dough_recipe_yield_pounds
    FROM inventory
    LEFT JOIN dough_recipes ON inventory.id = dough_recipes.inventory_id
    LEFT JOIN products ON dough_recipes.dough_id = products.dough_id
    LEFT JOIN orders_items ON products.id = orders_items.product_id AND (orders_items.quantity - orders_items.quantity_baked) > 0
    LEFT JOIN doughs ON doughs.id = products.dough_id
) sq
GROUP BY id

如果这确实是您所要查找的正确查询,那么没有样本数据和所需结果我们就无法分辨。以上查询只是基于您的查询的猜测。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

mysql JOIN与GROUP_CONCAT在复杂的查询中

来自分类Dev

MYSQL查询以获取Group BY中的SUM和LAST记录值

来自分类Dev

`使用SUM和GROUP BY将mySQL查询转换为SQLite

来自分类Dev

MySQL中的复杂SUM

来自分类Dev

MySql:复杂的聚合查询

来自分类Dev

复杂的MySQL删除查询

来自分类Dev

MySql:复杂的聚合查询

来自分类Dev

复杂的mysql查询DISTINCT

来自分类Dev

复杂的MySQL查询代码

来自分类Dev

MySQL中复杂的联接和MAX()查询

来自分类Dev

Django Group_By和SUM查询

来自分类Dev

SUM和GROUP BY使用子查询

来自分类Dev

SUM和GROUP BY使用子查询

来自分类Dev

查询中的 SUM、GROUP BY 和 LEFT JOIN

来自分类Dev

MySQL SUM 在虚拟子查询字段(带有另一个 SUM)和 GROUP BY

来自分类Dev

group by和partition逻辑复杂

来自分类Dev

使用IF,SUM和乘法的MySQL Select查询

来自分类Dev

MYSQL查询多重条件和SUM函数

来自分类Dev

MySQL GROUP BY和SUM排名

来自分类Dev

具有SQL COUNT和SUM的复杂查询

来自分类Dev

SUM和COUNT在复杂的SQL查询中不起作用

来自分类Dev

在同一个 Mysql 查询中的 MySql GROUP BY 和 SUM() 中

来自分类Dev

MySQL嵌套查询和GROUP BY

来自分类Dev

MySQL查询GROUP BY和ORDER by

来自分类Dev

复杂的mySQL两表查询

来自分类Dev

在MySQL中运行复杂的查询

来自分类Dev

多对多的复杂MySQL查询

来自分类Dev

复杂的MySQL会话组查询?

来自分类Dev

编写复杂的MySQL Join查询