ORACLE查詢某張表奇數行或者偶數行數據

說明:

floor函數:向下取整,比如floor(2.5) = 2;floor(3.0) = 3;

查詢:

select floor(e2.r/ 2),floor((e2.r-1)/2),e2.* from
(select rownum r, e1.* from
  (
  select * from nhlh_sto_yggys_mid where f_pkey in (select f_pkey from nhlh_sto_yggys_mid group by f_pkey having count(1) >1) order by f_pkey
  ) e1
) e2 where floor(e2.r/ 2) >floor((e2.r-1)/2) ;

結果:

解釋:因爲表中沒有行數列,所以首先我們查詢加入行數

select rownum r, e1.* from
  (
  select * from nhlh_sto_yggys_mid where f_pkey in (select f_pkey from nhlh_sto_yggys_mid group by f_pkey having count(1) >1) order by f_pkey
  ) e1

然後我們通過取整函數判斷行數除2與行數減1除2的結果對比,從而查詢出所需結果。查詢奇數行也是一樣的道理。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章