Skip to main content

The recommended approach to improve performance is to:

a) Use the database to do the heavy lifting for counting, computations (where possible)

b) Reduce the number of trips to the database and the amount of data brought back on each trip

In that light, I am not sure what you want to use the rank count for, but the query below will return the data ordered by rank, but a "repeated" column with the number of records for each rank is also included

SELECT t.rank, col1, col2, rankcount  
FROM tablename t 
  INNER JOIN 
     (SELECT rank, COUNT(id) as rankcount FROM tablename t2 GROUP BY rank) AS t3 
    ON (t1t.rank = t3.rank)   
ORDER BY t.rank

The recommended approach to improve performance is to:

a) Use the database to do the heavy lifting for counting, computations (where possible)

b) Reduce the number of trips to the database and the amount of data brought back on each trip

In that light, I am not sure what you want to use the rank count for, but the query below will return the data ordered by rank, but a "repeated" column with the number of records for each rank is also included

SELECT rank, col1, col2, rankcount FROM tablename t INNER JOIN (SELECT rank, COUNT(id) as rankcount FROM tablename t2 GROUP BY rank) AS t3 ON (t1.rank = t3.rank)  ORDER BY t.rank

The recommended approach to improve performance is to:

a) Use the database to do the heavy lifting for counting, computations (where possible)

b) Reduce the number of trips to the database and the amount of data brought back on each trip

In that light, I am not sure what you want to use the rank count for, but the query below will return the data ordered by rank, but a "repeated" column with the number of records for each rank is also included

SELECT t.rank, col1, col2, rankcount  
FROM tablename t 
  INNER JOIN 
     (SELECT rank, COUNT(id) as rankcount FROM tablename t2 GROUP BY rank) AS t3 
    ON t.rank = t3.rank   
ORDER BY t.rank
Source Link

The recommended approach to improve performance is to:

a) Use the database to do the heavy lifting for counting, computations (where possible)

b) Reduce the number of trips to the database and the amount of data brought back on each trip

In that light, I am not sure what you want to use the rank count for, but the query below will return the data ordered by rank, but a "repeated" column with the number of records for each rank is also included

SELECT rank, col1, col2, rankcount FROM tablename t INNER JOIN (SELECT rank, COUNT(id) as rankcount FROM tablename t2 GROUP BY rank) AS t3 ON (t1.rank = t3.rank)  ORDER BY t.rank