Mysql Sql statement to Get Position of Student Ranked in order of marks scored -


i need sql give position of student ranked in order of marks scored in specific examtyp e.g. cat! only.the sql below gives position of student not distinguish examtyp.it ranks without considering examtyp.

res_id admno stream examtyp termid  marks   grade   points  year    1    2129    0   cat1      1     525       c      62     2013    2    4093    0   cat1      1     569       b+     69     2013    3    2129    0   cat2      1     550       b+     67     2013    4    4093    0   cat2      1     556       b+     68     2013    6    2129    0   final     1     559       b+     68     2013    7    2129    0   average   1     545       b      66     2013    7    4093    0   final     1     581       b+     70     2013    8    4093    0   average   1     569       b+     69     2013     $sql = "select 1 + (select count(*) $table          a.total_marks > b.total_marks ) rank          $table b admno=? , examcategory=? , termid=? , year=?           order rank limit 1"; $res = $this->db->query($sql, array($admno, $examcategory, $term, $year)); 

this should work you:

select  res_id,         admno,         stream,         examtyp,         termid,         grade,         points,         `year`,         position    (   select  @r:= case when @e = examtyp @r + case when @p = points 0 else @i end else 1 end position,                     @i:= case when @p = points @i + 1 else 1 end incr,                     @e:= examtyp,                     @p:= points,                     res_id,                     admno,                     stream,                     examtyp,                     termid,                     grade,                     points,                     `year`                t,                     (select @e:= '') e,                     (select @r:= 0) r,                     (select @p:= 0) p,                     (select @i:= 0)             order examtyp, points         ) t   t.admno = 4093 ,     t.examtyp = 'cat1' 

it uses same principle of using variables has been suggested, partitions examtyp, resetting position 0 each new exam type, records previous points deal ties, if 3 people same mark same position.

example on sql fiddle

note in bottom pane of fiddle results average equal both position = 1


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -