萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> sql server教程 >> SQL 讀取不連續的第30到40之間的數據

SQL 讀取不連續的第30到40之間的數據

有時在讀取數據時,會從數據庫中讀取從某個位置到某個位置的數據,但是這個ID已經被破壞,不連續時主要采用row_number()函數。。。 

方法一:

select row_num,* from
(
 select *, row_number() over (order by I_VoucherMaterialID) as row_num from dbo.I_VoucherMaterial
) tt
where row_num between 30 and 40

注釋:row_number()函數必須要接一個over對它排序。

 

方法二:

WITH OrderedOrders AS
(
    SELECT SalesOrderID, OrderDate,
    ROW_NUMBER() OVER (order by OrderDate)as RowNumber
    FROM Sales.SalesOrderHeader
)
SELECT *
FROM OrderedOrders
WHERE RowNumber between 30 and 40;

 

注釋:指定臨時命名的結果集,這些結果集稱為公用表表達式 (CTE)。該表達式源自簡單查詢,並且在 SELECT、INSERT、UPDATE 或 DELETE 語句的執行范圍內定義。該子句也可用在 CREATE VIEW 語句中,作為該語句的 SELECT 定義語句的一部分。公用表表達式可以包括對自身的引用。這種表達式稱為遞歸公用表表達式。

格式:with Cust as
(
 select * from Employee
)
select * from Cust

方法三:
select * from
(select top 40 * from I_VoucherMaterial order by I_VoucherMaterialID) a
where I_VoucherMaterialID not in
(select top 30 I_VoucherMaterialID from I_VoucherMaterial order by I_VoucherMaterialID)

關鍵詞:SQL  數據 

copyright © 萬盛學電腦網 all rights reserved