有这样一个问题:
create table test(
int id,
int cid,
int num,
int gid
)数据如下:
id cid num gid
1 1 5 1
2 1 2 1
3 1 3 1
4 2 3 1
5 2 5 1
6 2 6 1
7 3 3 4
8 4 8 4
9 1 3 1
10 2 10 1
11 2 11 1
12 3 2 4
13 3 3 4
14 3 2 4
15 4 2 4
16 4 3 4想用一条sql,根据gid得到这样一个分组,相同cid的num最大值之和
如gid=1时,有1,2两个cid,numzz最大值分别为5,11,它们的和值为16
最终结果为:
gid num
1 16
4 11求该SQL
create table test(
int id,
int cid,
int num,
int gid
)数据如下:
id cid num gid
1 1 5 1
2 1 2 1
3 1 3 1
4 2 3 1
5 2 5 1
6 2 6 1
7 3 3 4
8 4 8 4
9 1 3 1
10 2 10 1
11 2 11 1
12 3 2 4
13 3 3 4
14 3 2 4
15 4 2 4
16 4 3 4想用一条sql,根据gid得到这样一个分组,相同cid的num最大值之和
如gid=1时,有1,2两个cid,numzz最大值分别为5,11,它们的和值为16
最终结果为:
gid num
1 16
4 11求该SQL
解决方案 »
- 急!Mysql 中按成绩降序排名 取名次(每个人的名次都要有)
- mysql,选择某字段出现次数大于1的记录
- MySQL-Front报错
- MYSQL 写php脚本,调试的环境问题
- 写了一个MYSQL的行转列的存储过程。各位给找出不足的地方和更高的效率的方法
- 大家进来讨论一下,如果用MySQL数据库,数据量大时如何才能比较高效的操作数据?
- limit不支持内层嵌套。。。。。。。。。。。。。。
- 求一个sql语句或存储过程
- MySQL获取锁失败求解-Lock wait timeout exceeded; try restarting transaction
- mysql数据写入硬盘延迟问题
- 数据库备份命令怎么老是不正确!!
- 请教Lost connection to MySQL server at 'reading initial communication packet'
[征集]分组取最大N条记录方法征集,及散分....
select gid,cid,max(num) as k
from test
group by gid,cid
) t
group by gid
from (
select gid,max(num) as num
from tb
group by gid,cid
)T
group by gid