【问题标题】:mysql sum(column2 - column1) with rollupmysql sum(column2 - column1) 与汇总
【发布时间】:2017-05-10 10:02:06
【问题描述】:

我的 mysql 查询需要一些帮助。我有一张看起来像的桌子:

然后我想用这个查询进行选择:

select id, class, defaut, input, round(input - defaut) test from table1
group by class, id with rollup

我想要这样的输出:

但是我的查询给了我这样的结果:

请帮忙,谢谢

【问题讨论】:

  • 我认为你可以用更少的行和/或 sqlfiddle/rextest 来说明问题。
  • 您选择 id、class、defaut 和 input,但仅按其中两列分组。虽然存在功能依赖,但对于 ROLLUP 而言,这并不完全合理。

标签: mysql sum formula rollup


【解决方案1】:

您需要将默认、输入和计算字段相加才能得到预期的输出,否则rollup 将简单地返回最后一条记录的值:

select id, class, sum(defaut), sum(input), sum(round(input - defaut)) test from table1
group by class, id with rollup

【讨论】:

    【解决方案2】:

    最终查询:

    select id, class, sum(defaut), sum(input), sum(input - defaut) test from
     table1 
    group by class, id with rollup
    

    感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      相关资源
      最近更新 更多