SQL Top 3 - More than 3 records shows even if ORDER BY primary key

R

Roger

I'm having a hard time figuring this out.

I have a table called Tgames that looks like this
Index, Game ID, Player, Points
1, 1, Player A, 10
2, 2, Player A, 5
3, 3, Player A, 10
4, 4, Player A, 7
5, 1, Player B, 5
6, 2, Player B, 7
7, 3, Player B, 5
8, 4, Player B, 10
9, 5, Player A, 5
10, 6, Player A, 7
11, 7, Player A, 5
12, 8, Player A, 5
13, 5, Player B, 10
14, 6, Player B, 7
15, 7, Player B, 7
16, 8, Player B, 7

I'm trying to get Top 3 points for each player with this query :

SELECT T.Player, T.Points
FROM Tgames As T
WHERE T.Points In (SELECT TOP 3 X.Points
FROM Tgames As X
WHERE X.[Player] = T.[Player]
ORDER BY X.Points DESC, X.Index)
ORDER BY T.Player

But I get 4 records for Player A, and 6 records for Player B...

What am I doing wrong? Please help!
Thanks!
 
M

Marshall Barton

Roger said:
I'm having a hard time figuring this out.

I have a table called Tgames that looks like this
Index, Game ID, Player, Points
1, 1, Player A, 10
2, 2, Player A, 5
3, 3, Player A, 10
4, 4, Player A, 7
5, 1, Player B, 5
6, 2, Player B, 7
7, 3, Player B, 5
8, 4, Player B, 10
9, 5, Player A, 5
10, 6, Player A, 7
11, 7, Player A, 5
12, 8, Player A, 5
13, 5, Player B, 10
14, 6, Player B, 7
15, 7, Player B, 7
16, 8, Player B, 7

I'm trying to get Top 3 points for each player with this query :

SELECT T.Player, T.Points
FROM Tgames As T
WHERE T.Points In (SELECT TOP 3 X.Points
FROM Tgames As X
WHERE X.[Player] = T.[Player]
ORDER BY X.Points DESC, X.Index)
ORDER BY T.Player

But I get 4 records for Player A, and 6 records for Player B...

What am I doing wrong? Please help!


A has 2 records with 10 and two with 7 while B has two with
10 an four with 7. You need to select the records by using
the field that determines which of the 7 values is the third
one?

It looks like you intended to use the Index field for that
purpose so I think you want the query to be more like:

SELECT T.Player, T.Points
FROM Tgames As T
WHERE T.Index In (SELECT TOP 3 X.Index
FROM Tgames As X
WHERE X.[Player] = T.[Player]
ORDER BY X.Points DESC, X.Index)
ORDER BY T.Player, T.Points
 
R

Roger

Thanks a lot!
That works fine, fast answer. I love you! :)

Marshall Barton said:
Roger said:
I'm having a hard time figuring this out.

I have a table called Tgames that looks like this
Index, Game ID, Player, Points
1, 1, Player A, 10
2, 2, Player A, 5
3, 3, Player A, 10
4, 4, Player A, 7
5, 1, Player B, 5
6, 2, Player B, 7
7, 3, Player B, 5
8, 4, Player B, 10
9, 5, Player A, 5
10, 6, Player A, 7
11, 7, Player A, 5
12, 8, Player A, 5
13, 5, Player B, 10
14, 6, Player B, 7
15, 7, Player B, 7
16, 8, Player B, 7

I'm trying to get Top 3 points for each player with this query :

SELECT T.Player, T.Points
FROM Tgames As T
WHERE T.Points In (SELECT TOP 3 X.Points
FROM Tgames As X
WHERE X.[Player] = T.[Player]
ORDER BY X.Points DESC, X.Index)
ORDER BY T.Player

But I get 4 records for Player A, and 6 records for Player B...

What am I doing wrong? Please help!


A has 2 records with 10 and two with 7 while B has two with
10 an four with 7. You need to select the records by using
the field that determines which of the 7 values is the third
one?

It looks like you intended to use the Index field for that
purpose so I think you want the query to be more like:

SELECT T.Player, T.Points
FROM Tgames As T
WHERE T.Index In (SELECT TOP 3 X.Index
FROM Tgames As X
WHERE X.[Player] = T.[Player]
ORDER BY X.Points DESC, X.Index)
ORDER BY T.Player, T.Points
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

query to get top player 8
Finding the right style field in VBA 0
How smart is excel? 0
sum n scores 1
Ranking (Look for previous ranking) 3
Paragraph numbering in Word 1
Sum Top n Scores 4
Query Help Needed 7

Top