oracle group by用法

分组第一个:

SELECT * FROM (SELECT ROW_NUMBER() OVER(PARTITION BY dlr_id ORDER BY user_id DESC) rn,
  u.user_id,u.dlr_id FROM tm_user u where dlr_id='1000001812')
  WHERE rn = 1  ;




select user_id,dlr_id from tm_user tu where tu.rowid in (
select max(rowid) from(
select user_id,dlr_id from tm_user)
group by dlr_id) and dlr_id='1000001812';


Oracle分组排名


情境:只查询某个条件下的N条件记录,如每个分类下查询(前)5条


例:(相同商户号只查5条)
[sql] view plaincopy


    select *  
      from (select t.mer_code,  
                   t.li_goods_name,  
                   t.li_id,  
                   dense_rank() over(partition by t.mer_code order by t.li_id) rk  
              from yd_mer_listing t)  
     where rk < 6  




说明:rank()的排名是按over()内order by决定的,如果order by的字段值是一样的,那么rank()排名也一样。


优势:如果用in语句,如 … where t.li_id in (select LI_ID from yd_mer_listing m where rownum < 6 and m.mer_code = t.mer_code and m.LI_STATE = '3' )…效率会很低

你可能感兴趣的:(oracle)