日期:2014-05-17  浏览次数:20760 次

如何取分组后符合条件的记录???
表table1有3个字段a,b,c,表内容如下:
1     2     'a '
1     3     'b '
1     4     'c '
2     5     'd '
2     6     'e '
2     7     'f '
3     1     'g '

其中a字段可能重复
现在要得到内容是
1     4     'c '
2     7     'f '
3     1     'g '

即在第一个字段相同时,取第二个字段中最大的那些记录
select   a,max(b),c   from   table1   group   by   a
不合法.

请问这样的sql如何写?   thanks.

------解决方案--------------------
select t1.a,t1.b,t1.c
from table1 t1
inner join
(seelct a,max(b) as b from table1 group by a) t2
on t1.a=t2.a and t1.b=t2.b

------解决方案--------------------
select distinct t1.a,t1.b,t1.c
from table1 t1,
(select a,max(b) as b from table1 group by a) t2
where t1.a =t 2.a and t1.b = t2.b

------解决方案--------------------
两个都对
------解决方案--------------------
select a,b,c
from(
select a,b,c
,row_number()over(partition by a order by b desc) rn
from table1
)
where rn=1


这样可能更快些!
------解决方案--------------------
up,来晚了。