日期:2014-05-18  浏览次数:20434 次

随便发个,看看有没有提高效率的可能~
A:
aa bb cc
1 001 20070901 1.5
2 001 20070903 1.3
3 001 20070908 1.7
4 002 20070905 1.5
5 002 20070910 1.6
6 003 20070909 1.9

结果:取每个aa的BB最大的CC,显示出来

3 001 20070908 1.7
5 002 20070910 1.6
6 003 20070909 1.9


我写的是:
select   a.aa,b.bb,a.cc   from   A   a,(select   aa,max(bb)   as   bb   from   A   group   by   aa)   b
where   a.aa=b.aa   and   a.bb=b.bb
还有更好的写法吗?请大虾赐教!

------解决方案--------------------
--try 1:
select * from A
where not exists(select 1 from A as B where aa=A.aa and bb> A.bb)

--try 2:
select * from A
where bb=(select max(bb) from A as B where aa=A.aa )


------解决方案--------------------
--try 3:
select * from A
where bb=(select top 1 bb from A as B where aa=A.aa order by bb desc)
------解决方案--------------------
如果你写的没有问题这个应该也是可行的
select aa,max(bb),stuff(max(bb+rtrim(cc)),1,8, ' ') as bb from A group by aa