I need to limit my result to 3 row (an example), but if 3rd result is equal to 4th, print also 4th, and so on.
To explain: from this table
id punteggio
1 100
2 200
3 70
4 100
5 54
6 201
7 200
if I do
SELECT * FROM table ORDER BY punteggio DESC LIMIT 3
i need in every case to print following situation:
id punteggio
6 201
2 200
7 200
1 100
4 100
Because my “three” best points are in reality 5 of 7, cause 2 and 7 have same points, like 1 and 4…
I dont’know in advance min and max points, otherwise i would do
"WHERE punteggio >= 100"
Thank you very much!
UPDATE
Unfortunately my scenario changed:
punteggio born from SUM from another table:
id idPersona punteggio
1 1 30
2 1 -10
3 2 50
4 3 60
5 2 -10
6 3 150
7 1 190
and so on…
i’ve tried do:
SELECT persone.nome,
SUM(transazioni.importoFinale) AS punti
FROM transazioni
INNER JOIN persone ON persone.idPersona = transazioni.idPersona
INNER JOIN
(SELECT DISTINCT(SUM(transazioni.importoFinale)) AS punti,
persone.nome
FROM transazioni
INNER JOIN persone on persone.idPersona = transazioni.idPersona
GROUP BY persone.idPersona
ORDER BY punti DESC
LIMIT 3) subq ON transazioni.punti = subq.punti
ORDER BY punti DESC
but it doens’t function…
Thank you to all!
Use a subquery join to get the
DISTINCTset of 3 greatest values forpunteggioand join it against the main table to retrieve all rows that have those values.Here’s a demo on SQLFiddle.com