当前位置: 代码迷 >> SQL >> SQL兑现先排序,后分组
  详细解决方案

SQL兑现先排序,后分组

热度:73   发布时间:2016-05-05 14:32:15.0
SQL实现先排序,后分组

实现功能:按t1分组,在每组中,把最大的t2所对应的一整条数据取出来。

1.表Table:
t1???????????? t2????????? t3
--------------------------
aaa??????????? 100????????? ==
bbb??????????? 80?????????? ==
aaa??????????? 90
ccc??????????? 70
ccc??????????? 500????????? ==
ccc??????????? 20
bbb??????????? 30
bbb??????????? 40
bbb??????????? 50

2.语句:
select A.* from Table A,(select t1,max(t2) as t2 from Table group by t1) B where A.t1=B.t1?? and A.t2=B.t2 order by A.t1 desc

3.结果:
t1???????????? t2????????? t3
--------------------------
ccc??????????? 500????????? ==
aaa??????????? 100????????? ==
bbb??????????? 80?????????? ==

?

  相关解决方案