读书人

同一张表中对数据分组求某列最大值的有

发布时间: 2012-05-15 14:35:29 作者: rapoo

同一张表中对数据分组求某列最大值的问题
同一张表中怎么对数据分组求某列最大值,如果最大值有并列项则取id最小的那一行数据

[解决办法]
假设以name分组

SQL code
select * from tb twhere not exists(select 1 from tb where name=t.name and (value>t.value or value=t.value and id<t.id)
[解决办法]
SQL code
-----------刚刚看错题目了create table stu(id int,[name] varchar(10),val int)insert into stuselect 1,'a',2 union allselect 2,'a',2 union allselect 3,'b',4 union allselect 4,'b',4 select * from stu where id in(select id from (select min(id) as id,max(val) as val from stu group by val)tmp)--------------------------------------------id          name       val----------- ---------- -----------1           a          23           b          4(2 行受影响) 

读书人网 >SQL Server

热点推荐