SQL语句实现表中字段的组合累加排序
有表如下:
id
in
out
index
1
10
12
2
2
9
8
2
3
12
8
3
1
9
9
3
1
12
0
3
2
5
18
2
2
20
0
2 这三个字段均为integer类型
现要求如下: www.2cto.com
1、按index=2查找
2、将所有id相同的和在一起,数据要加在一起
3、最后呈现的数据要按in+out进行排序,按由大到小
为满足以上的要求:设计的SQL语句如下(假设表名为tab)
select id,sum(in) as in,sum(out) as out from tab where index=2 group by id order by in + out desc;
作者 meaijojo