sql语句问题,取最大值问题
表结构      yd_ls_db 
 hh                        dfny         sfpc 
 20003093	200505	1 
 20003093	200506	1 
 20003093	200507	1 
 20003093	200510	1 
 20003093	200704	1 
 20002994	200001	1 
 20002994	200111	1 
 20002994	200112	1 
 20002994	200201	1 
 20002994	200202	1 
 20002994	200203	1 
 ---------------------------- 
 现在我想根据hh      取最大的dfny   的一条记录   ,比如上面的查询结果就是 
 20003093	200704	1 
 20002994	200203	1 
 -------------------------------------------- 
 yd_ls_db   中数据量比较大,基本上在500万左右,我用这个语句 
 select   *   from   yd_ls_db   where   (hh,dfny)   in   ( 
 select   hh,max(dfny)   dfny   from   yd_ls_db   group   by   hh); 
 速度非常慢. 
 用这个语句          
 select   hh,dfny,sfpc    
 from   ( 
          select   hh,dfny,sfpc, 
                   rank()   over   (order   by   dfny   desc)   add_rank 
          from   yd_ls_db 
 ) 
 where   add_rank   =   1; 
 速度也好不到哪里去,哪个高人指导一下 
 ----------------------- 
------解决方案--------------------把你的in换成exists试试,不能保证能快,和你表的索引设置也有关系,只能试试吧. 
 select * from yd_ls_db t1 where exists (select  'X ' from 
 (select hh,max(dfny) dfny from yd_ls_db group by hh ) 
 where hh=t1.hh and dfny=t1.dfny);
------解决方案--------------------索引建了吗
------解决方案--------------------这样不行吗 
 select t.hh,max(t.dfny),max(t.sfpc) from sql_0829 t group by t.hh