【问题标题】:Query for fuel usage with a subquery使用子查询查询燃料使用情况
【发布时间】:2019-10-22 20:26:52
【问题描述】:

搜索了 Stackoverflow,但无法找到我的问题的答案(也许它在那里,但没有看到)。

具有以下查询,其中列出了存储在 MAIN 表中某个位置的多辆车辆的使用里程、燃料成本和燃料数量。还有一个子查询来计算每英里的成本 - 并且在该子查询中是一个 WHERE 子句,除非fuel_qty > 0(不能除以零,除非你是 Chuck Norris - 哈哈),否则不计算。如果fuel_qty 是零值,还需要显示一个零(在此查询的第3 行中)。我收到此查询的错误 - 说它“不是单组组功能”。有什么我遗漏或没有看到的东西吗?

已尝试将 cost_per_mile 添加到 group by 子句,但收到“无效标识符”错误。然后还在子查询中添加了一个 group by 子句 - 但这也不起作用。

select cost.mileage_useage
     , cost.fuel_cost
     , cost.fuel_qty
     , (select (sum(cost1.mileage_usage / cost1.fuel_qty) * cost1.fuel_cost)
        from cost cost1
        where cost1.fuel_qty > 0) as cost_per_mile
from cost
inner join main on main.equip_no = cost.equip_no
where main.stored_loc = 4411
group by 
     cost.mileage_useage
   , cost.fuel_cost
   , cost.fuel_qty

【问题讨论】:

  • 样本数据、期望的结果以及对您想要做什么的解释都会非常有帮助。
  • @GordonLinoff 样本数据我相信会是这样的......mileage_usage = 410fuel_cost = 3.20fuel_qty = 25 cost_per_mile = 32.48
  • costcost1 是单独的表吗?
  • @BobJarvis - 没有 cost1 是区分 COST 表和子查询中使用的同一个表的子查询。可能没有完全正确地写出来。已更改给定的脚本示例以使其更加清晰。

标签: sql oracle subquery


【解决方案1】:

为什么这不符合你的要求?

select c.mileage_useage, c.fuel_cost, c.fuel_qty,
       (sum(c.mileage_usage) * c.fuel_cost /
        nullif(c.fuel_qty, 0)
       ) as cost_per_mile
from cost c inner join
     main m
     on m.equip_no = c.equip_no
where main.stored_loc = 4411
group by c.mileage_useage, c.fuel_cost, c.fuel_qty

【讨论】:

  • 在某些情况下,mileage_usage 为负数。也许这与此有关?感谢您的回答,但它似乎不起作用
  • 嗯......一旦我在mileage_usage中添加了一个nullif,它就有点工作了。我想我可能需要进一步研究。谢谢@GordonLinoff !!
  • @丽贝卡。 . .我不知道负数是什么意思。也许您应该问另一个问题,处理该列中的负数。
【解决方案2】:

相信我找到了答案 - 感谢您的所有帮助!如果里程使用 = 0 或为负数,则将其考虑在内。此外,如果燃料量 = 0,则等式的该部分不可能除以零值。它可能看起来有点奇怪,但这是可行的!

select cost.mileage_useage
     , cost.fuel_cost
     , cost.fuel_qty
     , ( sum(((CASE WHEN cost.mileage_usage = 0 THEN 1
         WHEN cost.mileage_usage < 0 THEN TO_NUMBER(NULL)
         ELSE cost.mileage_usage END) 
         / DECODE(eq_cost.fuel_qty,0, 1, eq_cost.fuel_qty)) 
         * eq_cost.fuel_cost )) as cost_per_mile
from cost
inner join main on main.equip_no = cost.equip_no
where main.stored_loc = 4411
group by cost.mileage_useage
      , cost.fuel_cost
      , cost.fuel_qty

【讨论】:

    【解决方案3】:

    你可以进一步简化如下:

    select cost.mileage_useage
         , cost.fuel_cost
         , cost.fuel_qty
         , sum((CASE WHEN cost.mileage_usage = 0 THEN eq_cost.fuel_cost
             WHEN cost.mileage_usage > 0 THEN cost.mileage_usage * eq_cost.fuel_cost END) 
             / (case when eq_cost.fuel_qty = 0 then 1 else eq_cost.fuel_qty end)) as cost_per_mile
    from cost
    inner join main on main.equip_no = cost.equip_no
    where main.stored_loc = 4411
    group by cost.mileage_useage
          , cost.fuel_cost
          , cost.fuel_qty;
    

    干杯!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多