2

我想跳过前 5 条记录,然后选择 10 条记录

email在表中有一列user。在这里,我尝试user使用此查询从表中选择前 10 个唯一行

select DISTINCT TOP 10 email from user

现在我正在尝试从跳过前 5 条记录的表中选择前 10 个唯一行

select DISTINCT SKIP 5 TOP 10 email from user

这没有完成并返回错误..谁能帮助我

4

5 回答 5

3
SELECT A.NAME FROM
(SELECT distinct RANK() OVER(ORDER BY NAME) RNK,NAME FROM USERS) A
WHERE A.RNK>4 AND A.RNK<16

使用LIMIT并不能保证您会以正确的顺序获得顶行。如果你使用ANALYTIC函数,它会给你正确的结果。

SQL_LIVE_DEMO

于 2013-01-05T12:07:51.917 回答
2

这是一种方法。我喜欢将公用表表达式用于这样的一些事情,因为它使查询易于理解,尽管这并不是特别复杂。

WITH CTE AS
(
    Select Distinct Email From User
 )
 ,
 CTE1 AS
 (
    Select Email, ROW_NUMBER() over (ORDER BY Email) AS RowNumber
    From CTE
 )

Select Top 10 * From CTE1 Where RowNumber > 5
于 2013-01-05T12:10:25.727 回答
1
with t2 as
(
select t1.*,
 row_number() over (order by id) rn 
from 
  (select email, max(id) as id from [user] group by email) as  t1
)

select * from t2 where rn between 5 and 10
于 2013-01-05T12:10:17.470 回答
1

这个怎么样:

SELECT * 
FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY email) AS row 
      FROM user ) a 
WHERE row > 5 and row <= 10

我认为您使用 SKIP 不正确,它应该是 ORDER BY 子句的一部分。

于 2013-01-05T12:25:29.250 回答
0
SELECT DISTINCT TOP(10) Email FROM TableName WHERE Email not in (SELECT TOP(5) Email From TableName)

您可以尝试此代码,在此查询中获取不同的 10 个电子邮件 ID,如您在此问题中所说,跳过 5 条记录。

于 2019-04-05T10:28:32.630 回答