MSSQL的ORDER BY与OFFSET和FETCH NEXT组合使用时无法正常工作

huangapple go评论61阅读模式
英文:

MSSQL ORDER BY with OFFSET and FETCH NEXT do not work properly

问题

以下是您要翻译的内容:

我有这个查询:

SELECT TOP (1000) [id]
      ,[name]
      ,[dob]
FROM [Production].[dbo].[test]
ORDER by [dob] ASC

结果:

id name dob
1 Danny Small 1/1/2009
3 Victor Brady 1/1/2009
4 Maximus Hoffman 1/1/2009
9 Rosa Braun 1/1/2009
10 Marley Chapman 1/1/2009
11 Cristian Chaney 1/1/2009
6 Shayla Farrell 1/1/2009
7 Tom Summers 1/1/2009
15 Madison Reid 1/1/2009
16 Nelson Green 1/1/2009
18 Dalton Duran 1/1/2009
24 Karina Giles 1/1/2009
25 Jon Jon 1/1/2009
17 Riya Webster 1/3/2009
12 Alexus Cisneros 1/3/2009
13 Ann Mcclure 1/3/2009
14 Aryan Cortez 1/3/2009
5 Stefan Tamburyn 1/3/2009
23 Dalton Duran 1/3/2009
2 Eva Hobbs 1/5/2009
8 Elliott Acosta 1/5/2009
22 Kadin Mcclure 1/5/2009

现在我想获取前10条记录:

SELECT [id]
      ,[name]
      ,[dob]
FROM [Production].[dbo].[test]
ORDER by [dob] ASC
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;

结果:

id name dob
1 Danny Small 1/1/2009
11 Cristian Chaney 1/1/2009
10 Marley Chapman 1/1/2009
9 Rosa Braun 1/1/2009
4 Maximus Hoffman 1/1/2009
3 Victor Brady 1/1/2009
7 Tom Summers 1/1/2009
6 Shayla Farrell 1/1/2009
15 Madison Reid 1/1/2009
16 Nelson Green 1/1/2009

现在我想获取第二组10条记录:

SELECT [id]
      ,[name]
      ,[dob]
FROM [Production].[dbo].[test]
ORDER by [dob] ASC
OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;

结果:

id name dob
7 Tom Summers 1/1/2009
6 Shayla Farrell 1/1/2009
25 Jon Jon 1/1/2009
5 Stefan Tamburyn 1/3/2009
14 Aryan Cortez 1/3/2009
13 Ann Mcclure 1/3/2009
12 Alexus Cisneros 1/3/2009
17 Riya Webster 1/3/2009
23 Dalton Duran 1/3/2009
22 Kadin Mcclure 1/5/2009

正如您所看到的,记录:

id name dob
7 Tom Summers 1/1/2009
6 Shayla Farrell 1/1/2009

在前10条记录和第二组10条记录中重复出现。我理解ORDER BY可能会引起混乱。

解决方法可以是添加另一个ORDER BY,但如果只能使用一列来进行ORDER BY,而且一些单元格可能具有相同的值,那该怎么办?

英文:

I have this query:

SELECT TOP (1000) [id]
      ,[name]
      ,[dob]
FROM [Production].[dbo].[test]
ORDER by [dob] ASC

Result:

id name dob
1 Danny Small 1/1/2009
3 Victor Brady 1/1/2009
4 Maximus Hoffman 1/1/2009
9 Rosa Braun 1/1/2009
10 Marley Chapman 1/1/2009
11 Cristian Chaney 1/1/2009
6 Shayla Farrell 1/1/2009
7 Tom Summers 1/1/2009
15 Madison Reid 1/1/2009
16 Nelson Green 1/1/2009
18 Dalton Duran 1/1/2009
24 Karina Giles 1/1/2009
25 Jon Jon 1/1/2009
17 Riya Webster 1/3/2009
12 Alexus Cisneros 1/3/2009
13 Ann Mcclure 1/3/2009
14 Aryan Cortez 1/3/2009
5 Stefan Tamburyn 1/3/2009
23 Dalton Duran 1/3/2009
2 Eva Hobbs 1/5/2009
8 Elliott Acosta 1/5/2009
22 Kadin Mcclure 1/5/2009

Now I want to get first 10 records:

SELECT [id]
      ,[name]
      ,[dob]
FROM [Production].[dbo].[test]
ORDER by [dob] ASC
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;

Result:

id name dob
1 Danny Small 1/1/2009
11 Cristian Chaney 1/1/2009
10 Marley Chapman 1/1/2009
9 Rosa Braun 1/1/2009
4 Maximus Hoffman 1/1/2009
3 Victor Brady 1/1/2009
7 Tom Summers 1/1/2009
6 Shayla Farrell 1/1/2009
15 Madison Reid 1/1/2009
16 Nelson Green 1/1/2009

Now I want to get second 10 records:

SELECT [id]
      ,[name]
      ,[dob]
FROM [Production].[dbo].[test]
ORDER by [dob] ASC
OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;

Result:

id name dob
7 Tom Summers 1/1/2009
6 Shayla Farrell 1/1/2009
25 Jon Jon 1/1/2009
5 Stefan Tamburyn 1/3/2009
14 Aryan Cortez 1/3/2009
13 Ann Mcclure 1/3/2009
12 Alexus Cisneros 1/3/2009
17 Riya Webster 1/3/2009
23 Dalton Duran 1/3/2009
22 Kadin Mcclure 1/5/2009

As you can see records:

7 Tom Summers 1/1/2009
6 Shayla Farrell 1/1/2009

are duplicated in first 10 records and second 10 records.
I understand that ORDER BY is messing around.

Solution could be adding another ORDER BY but what if have to use only one column to ORDER BY and some cells may have same value?

答案1

得分: 1

你必须确保ORDER BY子句为序列提供了一个唯一的排序方式。

在你的情况下,dob不是表的唯一键。添加一个使其唯一的排序条件:在[dob]字段之后加入[id]

例如:

SELECT [id]
      ,[name]
      ,[dob]
FROM [Production].[dbo].[test]
ORDER BY [dob] ASC, [id]
OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;
英文:

You must assure the ORDER BY clause gives a unique order to the sequence.

In your case, dob is not a unique key for the table. Add an order condition that makes it unique: [id] after the [dob] field.

eg.

SELECT [id]
      ,[name]
      ,[dob]
FROM [Production].[dbo].[test]
ORDER by [dob] ASC, [id]
OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;

答案2

得分: 1

如果只能使用一列进行ORDER BY,而某些单元格可能具有相同的值,那就是你需要移除的人为约束。

关系数据库明确放弃了保留数据任何固有或自然顺序的责任。没有任何默认或回退的排序。数据库可以自由地以它认为对于特定情况最有效的顺序返回数据,如果没有显式的ORDER BY指令,在某些情况下,它们可能会在同一查询和源数据的运行之间返回不同的顺序。

英文:

> what if [I] have to use only one column to ORDER BY and some cells may have same value?

That's an artificial constraint you'll need to remove.

Relational databases explicitly disclaim any responsibility at all for preserving any inherent or natural order for data. There is no such thing as any default or fallback ordering. A database is free to return data in the order it deems most efficient for the situation, and without explicit ORDER BY instructions they can and will in some circumstances return different orders for the same query and source data from run to run.

huangapple
  • 本文由 发表于 2023年4月13日 22:04:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定