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

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 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号