sql 取表的前10條記錄,任意中間幾行的記錄

取表的前10條記錄
with a as(select *,row_number()over(order by department)rn from _SucceedStaff )

select * from a where rn<=10

取表的任意中間幾行的記錄eg:10-30
with a as(select *,row_number()over(order by department)rn from _SucceedStaff )
select * from a where rn>=10 and rm<=30

附:常用幾種數據庫,取前10條記錄的sql語句寫法
http://blog.csdn.net/lzz313/archive/2006/12/19/1449572.aspx

access:
select top (10) * from table1 where 1=1

db2:
select column from table where 1=1 fetch first 10 rows only

mysql:
select * from table1 where 1=1 limit 10

sql server:
讀取前10條:select top (10) * from table1 where 1=1
讀取後10條:select top (10) * from table1 order by id desc

oracle:
select * from table1 where rownum<=10


取每日日期的前十條記錄

SQL表列如下:

單號 日期
1 2011-09-10
2 2011-09-10

select * from tb a where 單號 in(select top 10 單號 from tb where 日期=a.日期 order by 單號)

select * from (
select *,rank () over(partition by day(日期) order by 日期) rk from tb
) t
where t.rk<=10

select * from(select *,row_number()over(order by name)rn from _SucceedStaff )t
where t.rn<=10(對比上面) 

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