dongxie3963 2014-07-28 01:52
浏览 192
已采纳

MYSQL复杂的GROUP BY和SUM查询失败

I have this convoluted query that is getting current inventory levels & converting the units to pounds, then looking at outstanding orders & converting the units to pounds, and finally display a summed amount needed of each ingredient for all outstanding orders.

The purpose is to list the ingredients with a inventory level and then an amount needed to fulfill all outstanding order. If the baker does not have enough flour then he can see this and look into sourcing more before he begins baking for the day.

The problem is then when I run this query it is skipping the first row in the group function. When I run it without grouping it gets all the rows correct, but when i run it with a SUM and GROUP BY it skips the first row so it under calculates that items amount needed value. Any tips? Thanks!

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
  • 写回答

2条回答 默认 最新

  • douduan9391 2014-07-28 08:43
    关注

    Group by does not skip rows or anything. Your query structure seems just wrong. Have a try with this:

    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
    

    If this is really the right query for what you're looking for, we can't tell without sample data and desired result. Above query is just a guess based on your query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动