有如下一个表 ,
ID VALUE
1 10
2 10
3 10
4 20
5 20
6 20
7 40
我想按每三行加VALUE和,得到的在插入一临时表,得到三个字段如下
想得到如下的结果
ID VALUE sum
1 10 30
2 10 30
3 10 30
4 20 60
5 20 60
6 20 60
7 40 40
请问该用一个什么SQL语句得到?谢谢
---------------------------------------------------------------
select id,value,
(select sum(value) from mytable t0 where (t0.id-1)/3=(t1.id-1)/3) as sum
from mytable t1
或
select t0.id,t0.value,t2.sum
from mytable t0 join
(select (id-1)/3 as id ,sum(value) as sum from mytable t1 group by (id-1)/3 ) t2
on (t0.id-1)/3=t2.id
order by t0.id