Query selecting Top 12 Results

pinkpanther6666

Board Regular
Joined
Feb 13, 2008
Messages
194
Office Version
  1. 365
Platform
  1. Windows
I know how to select the top 12 results in my query where .. Say the 12th result has a value of 6 but the 13th result also has a value of 6 ... But my query returns 13 results


How do I only get it return 12 results

Thanks

Steve
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
How does it know which record to return in the event of a tie like that?
What other fields are being returned by the query?
Can you post a sample of what is being returned, and also the SQL code for the query?
 
Upvote 0
I'm not at home so can't post a copy of my query at the moment ... But when when I return I will post a copy
 
Upvote 0
Hi,

My code is listed below





SELECT TOP 12 Main.Team, Main.Name, Sum(([Main].[DD]+[DS])+[DO]+[53]) AS AllRounders
FROM Main
GROUP BY Main.Team, Main.Name
ORDER BY Sum(([Main].[DD]+[DS])+[DO]+[53]) DESC;


Hope you can help

Cheers


Steve
 
Upvote 0
OK, you addressed two of the four things I requested. Can you address the other two (highlighted in red)?
How does it know which record to return in the event of a tie like that?
What other fields are being returned by the query?
Can you post a sample of what is being returned, and also the SQL code for the query?
In regards to the first one, it is more a logic question for you. In the event of a tie, like you mention, how do you make the determination of which record to return and which not to?
 
Upvote 0
joe

Thanks for your reply

How does it know which record to return in the event of a tie like that?

Basically it doesnt but the when the query is in a report it puts teh score highest value first then down to the lowest value and then the name is sorted alphabetically

Can you post a sample of what is being returned,

Name Score
Player 1 25
Player 2 24
Player 3 23
Player 4 21
Player 5 19
Player 6 16
Player 7 15
Player 8 14
Player 9 12
Player 10 9
Player 11 7
Player 12 6
Player 13 6

Hope this helps



Steve

 
Last edited:
Upvote 0
It looks like if you add in a secondary sort field that does NOT end in a tie, it will work (so if you have a primary key or a unique, field that would be a good one to use). Since you are grouping on Team and Name, if we add those fields to the sort, that will work (as Grouping on them guarantees a unique combination in your query).

So that code would just look like:
Code:
SELECT TOP 12 Main.Team, Main.Name, Sum(([Main].[DD]+[DS])+[DO]+[53]) AS AllRounders
FROM Main
GROUP BY Main.Team, Main.Name
ORDER BY Sum(([Main].[DD]+[DS])+[DO]+[53]) DESC, [COLOR=#0000ff]Main.Team, Main.Name[/COLOR];
 
Upvote 0
Solved Re: Query selecting Top 12 Results

Joe

Just tried the code and it works beautifully


Many thanks for your help

Steve
 
Upvote 0
Re: Solved Re: Query selecting Top 12 Results

You are welcome!
 
Upvote 0

Forum statistics

Threads
1,221,818
Messages
6,162,154
Members
451,749
Latest member
zack_ken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top