I have a table of bank movements of many users that can be repeated throughout it. The table has different fields such as Box, UserName, IDN, Date, Amount ...
What I want to do is a ranking of boxes with more users sorted by the DNI (without being repeated). I mean I have:
Alberto - caja1 - dni1 - fecha1 - importe1
David - caja2 - dni2 - fecha2 - importe2
Carlos - caja3 - dni3 - fecha3 - importe3
Miguel - caja1 - dni4 - fecha4 - importe4
Roberto - caja2 - dni5 - fecha5 - importe5
Luis - caja3 - dni6 - fecha6 - importe6
Nacho - caja1 - dni7 - fecha7 - importe7
Alberto - caja1 - dni1 - fecha8 - importe8
And what I want is to appear:
Caja1 - 3
Caja2 - 2
Caja3 - 2
What I already have done is:
SELECT Caja, COUNT(DISTINCT dni) as Usuarios
FROM miTabla
GROUP BY Caja
But this brings me all the boxes with their respective users and not the ones that have the most users.
Thank you very much!