Skipping Ranking in Rows which do not meet required value from Mysql sorted ranking with ties?

StackOverflow https://stackoverflow.com/questions/22611717

  •  20-06-2023
  •  | 
  •  

Question

I have googled and searched through this stack site also and could not find which atleast give me some ideas on how I could achieve this. I calculated ranking from sum of totalscore by a student and determined ties with sql variable. Each student has five subjects and can have tests more than once in a week. I want to skip a student who score less than 35% of totalscored in each subject from ranking. For example, a student gets 40 out of 100 in English, 59 out of 100 in MIL, 110 out of 130 in Science, 98 out of 120 in Mathematics, 33 out of 100 in SS. If fullmark for each subject is 100, I can calculate using the following query:

  SELECT regd,
        English, MIL,  
        Mathematics, SS,
        Science, 
        score, fmscore, perc, Rank 
        FROM 
        (
        SELECT t.*, IF(@p = score, @n, @n := @n + 1) AS Rank, @p := score 
        FROM
        (
        SELECT regd, 
            SUM(IF(Subject = 'English'    , Mark_score, 0)) English,
            SUM(IF(Subject = 'MIL'       , Mark_score, 0)) MIL,
            SUM(IF(Subject = 'Mathematics', Mark_score, 0)) Mathematics, 
            SUM(IF(Subject = 'SS'         , Mark_score, 0)) SS,
            SUM(IF(Subject = 'Science'    , Mark_score, 0)) Science,
            SUM(Full_mark) fmscore,
            SUM(Mark_score) score, 
            SUM(Mark_score) / SUM(Full_mark) * 100 perc 
        FROM exam e1, (SELECT @n := 0, @p := 0) n 
        WHERE NOT EXISTS (SELECT null from exam e2 WHERE e1.regd = e2.regd
        AND e2.Mark_score/e2.Full_mark<0.35)
        GROUP BY regd 
        ORDER BY score DESC
        ) t
        ) r;

Because fullmark for each subject is different and not 100, the code could not correctly skip ranks for those students who get less than 35% of full mark in one or more subjects. If this could not be achieved using mysql alone, will there be any possibilities using php array? In the exam table id is primary key, and regd is indexed. This is my project and I have been stucked here for a month now. Please help me.

Était-ce utile?

La solution

You need to first group the data regd with subject wise. So that you can able to calculate the percentage against each subject and remove those who are less than defined limit. Once you achive this, you can carry this output and use to show and create Rank. You can use something like below query.

 select regd, SUM(IF(Subject = 'English'    , Mark_score, 0)) English,
            SUM(IF(Subject = 'MIL'       , Mark_score, 0)) MIL,
            SUM(IF(Subject = 'Mathematics', Mark_score, 0)) Mathematics, 
            SUM(IF(Subject = 'SS'         , Mark_score, 0)) SS,
            SUM(IF(Subject = 'Science'    , Mark_score, 0)) Science,
            Mark_score, Perc
            from (
        SELECT regd, Subject,(SUM(Mark_score) / SUM(Full_mark) * 100) Perc , (SUM(Mark_score)) Mark_score
        FROM exam e1
        GROUP BY regd, Subject
        having perc > 35

        ) as t 
         order by Mark_score;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top