1. 程式人生 > >數據庫常用語句

數據庫常用語句

chan nbsp 也不會 rank 排序 分析函數 必須 create rownum

1.實現取值轉換

select decode(t.trans_sub_type, ‘Q‘, ‘1‘, ‘D‘, ‘2‘, ‘T1‘, ‘3‘,‘D1‘, ‘4‘) as trans_sub_type,t.trans_seq,t.trans_date from trans_info_log t where trans_status=‘04‘ order by create_time desc

2.查詢100個中排名第67的做法,三重嵌套

select * from (
select a.*,rownum from
( select t.* from aab t order by t.xinshui desc ) a
where rownum<=67 order by rownum desc
) where rownum=1

有排序,那麽就是三重嵌套rownum或者分析函數兩重搞定
分析函數如下實現:
select * from
(select a.*, rank() over(order by xinshui desc) as lev from aab a)
where lev=2


rownum的使用必須要包括第一條記錄, 也就是 rownum >= 1 這個語句可以,
而rownum > 1 則不可以, 因為這個條件中rownum為1時就不成立了.
所以直接使用 rownum=2 也不會有結果顯示, 因為 1 != 2.
在這種情況下就是利用嵌套查詢來實現了!~

3.查重復

select merchant_code from route_merch_pool_map where merchant_code in (select merchant_code from route_merch_pool_map group by merchant_code having count(1) >= 2)

數據庫常用語句