Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have an issue that the mysql query result is incorrect, that is instead of 1,2,3 it return 1,2,4

Values in Table are as:

id  academic_year_id	class_id	avg_grade_point
1	   6	          1	         4.0
2	   6              1          4.13
3	   6              1          4.25
4	   6              1          4.0
5	   6              1          4.13

Please help.

What I have tried:

SQL
SELECT id, avg_grade_point, FIND_IN_SET( avg_grade_point, 
                ( SELECT GROUP_CONCAT( avg_grade_point ORDER BY avg_grade_point DESC )
                FROM final_results WHERE academic_year_id = 6 AND class_id = 1  ) ) AS rank 
                FROM final_results 
                WHERE academic_year_id = 6 AND class_id = 1
Posted
Updated 19-Feb-21 3:24am
v3

Follow @OriginalGriff's excellent advice and run the query
SQL
SELECT GROUP_CONCAT( avg_grade_point ORDER BY avg_grade_point DESC )
                FROM final_results WHERE academic_year_id = 6 AND class_id = 1 
You will see the results
4.25,4.13,4.13,4.00,4.00
So you have a "joint second" placing with 4.13. That means there is no "third place" to award, it jumps straight to fourth. If you add in an extra line of data e.g.
(6,6,1,3.13);
you only get rankings 1,2,4 and 6 because there is also a tied place scenario with 4.00

I think you are getting confused between Rank and Row Number. This article might help to explain it better Similarities and Differences among RANK, DENSE_RANK and ROW_NUMBER Functions – {coding}Sight[^]
 
Share this answer
 
Comments
nyt1972 19-Feb-21 10:36am    
Thanks for your replies, I tried the below and it worked:

SELECT id, avg_grade_point, FIND_IN_SET( avg_grade_point, 
                ( SELECT GROUP_CONCAT( DISTINCT avg_grade_point ORDER BY avg_grade_point DESC ) AS Rank
                FROM final_results WHERE academic_year_id = 6 AND class_id = 1 ) ) AS rank 
                FROM final_results 
                WHERE academic_year_id = 6 AND class_id = 1
Either your query is not as shown, or your data isn't: runnin that query on that data will give you no rows returned even if we assume that academic_year is actually academic_year_id and class_d is actually class_id
Why not? Because there are no rows in your data which match the condition:
SQL
WHERE academic_year_id = 6
All your rows contain "1", not "6".

So start by looking closely at your tables and the data they contain; then run your query in parts to find out exactly what each returns before "bolting them together" with FIND_IN_SET
 
Share this answer
 
Comments
nyt1972 19-Feb-21 8:53am    
sorry it was typing mistake. its academic_year_id and class_id and academic_year_id is 6 not 1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900