2014-03-04 236 views
0

如何在SQL2005上對此查詢應用偏移量?SQL SERVER 2005 OFFSET

SELECT [name],[phone] 
FROM (
     SELECT *,Row_Number() Over (Partition By phone Order By (Select Null)) AS RN 
     FROM Table1 
    ) AS T 
Where RN = 1 

感謝

+0

'分區通過電話排序(選擇爲空)'不保證任何特定的順序。因此,絕對不能保證'RN BETWEEN 10 AND 20'不會返回與'RN BETWEEN 20 AND 30'相同的行' –

回答

1

抵消MSSQL 2005不支持,但你可以用以下approuch做到這一點。 如果你選擇90〜100這是按名稱排序前10的記錄,你可以用下面的查詢

;with data as 
(
    SELECT [name],[phone] 
    FROM (
     SELECT *,Row_Number() Over (Partition By phone Order By (Select Null)) AS RN 
     FROM Table1 
    ) AS T 
    Where RN = 1 
) 
select * from 
    ( select top(10) * from 
      ( select top(100) * from data order by name asc) 
     t1 order by name desc 
    ) t2 order by name 
+0

'如何讓記錄從20到30那麼呢? –

+0

更改選擇頂部(100)以選擇頂部(30) – fastobject

0

我自己解決了這個。我使用這個查詢(使用不同的列名)

with CTE as (
    SELECT cFullname,BuyerPhoneNumber 
    FROM (
     SELECT *,Row_Number() Over (Partition By BuyerPhoneNumber Order By (Select Null)) AS RN 
     FROM [Order] 
    ) AS T 
    Where RN = 1 
) 
SELECT * 
FROM (
SELECT Row_Number() OVER (ORDER BY cFullname ASC) AS CI_offset_row_number, * 
FROM CTE 
) AS A 
WHERE A.CI_offset_row_number BETWEEN (21) AND (30)