| |

VerySource

 Forgot password?
 Register
Search
View: 840|Reply: 8

sql top problem

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-14 13:00:01
| Show all posts |Read mode
select top 30 class, name, physics where class>= and class<= 22 from x order by physics desc; find out that the record is 33. This is correct, because students with the same score, I still want to match the class Do not re-order, use order by physical desc, class by asc, only 30 records are found, what is the problem?
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-12-14 14:15:01
| Show all posts
There should be only 30 repetitions
Reply

Use magic Report

0

Threads

40

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-12-14 17:15:01
| Show all posts
select all top 30 class, name, physics where class>= and class<= 22 from x order by physics desc;
Add a keyword ALL
Reply

Use magic Report

0

Threads

40

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-12-14 18:00:01
| Show all posts
select top 30 WITH TIES class, name, physics where class>= and class<= 22 from x order by physics desc;

This should work
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-12-20 13:15:01
| Show all posts
Brotherxclzc17, your method, I debugged it but it didn't work. In fact, my biggest doubt is: Why does a single order by physical desc have more records than an order by physical desc, and class asc?
Reply

Use magic Report

0

Threads

40

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-12-21 10:45:01
| Show all posts
Maybe the environment is different
I tried it with select top 30 WITH TIES.
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 United States

Post time: 2020-12-21 16:30:01
| Show all posts
Can the order of where and from be messed up?
Reply

Use magic Report

0

Threads

40

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-12-21 16:45:01
| Show all posts
Haha, sorry, the copy above is wrong
Reply

Use magic Report

0

Threads

13

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-12-22 09:15:01
| Show all posts
1. Please confirm that the database used is sqlserver?
2. In sqlserver, if you follow the original host's wording, it does not match the original host's result. The top query of sqlserver does not have with ties by default, so top 30 will not return 33 data. I think to get 33 pieces of data, first add with ties when querying
3. Regarding the different results after sorting, it is normal. You should add with ties to both queries for comparison. For queries with top and order by, in your case, sort first, and then extract the first n data. So after you add the sort condition, in the case of with ties, the previous sort position may be duplicate records, but now it is not the same position. Naturally, no additional data is returned.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list