2017-05-10 41 views
-1

我需要一些帮助,我的MySQL查询。我有一个表,看起来像:mysql sum(column2 - column1)with rollup

enter image description here

然后,我想这个查询选择:

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

我想要的输出,如:

enter image description here

但我的查询给我这样:

enter image description here

请您的帮助,谢谢

+1

我想你可以用更少的行和/或sqlfiddle/rextest来说明问题。 – Strawberry

+0

您可以选择id,class,defaut和input,但只能按其中的两列进行分组。虽然存在功能上的依赖性,但出于ROLLUP的目的,这并不合理。 – Strawberry

回答

2

你需要总结的默认输入和计算的领域,以获得预期的输出,否则rollup会简单地从过去的记录返回值:

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

非常感谢。你的回答非常有帮助 –

0

最终查询:

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

enter image description here

感谢您的帮助。

相关问题