我需要一个sql,它会根据特定考试中得分的顺序给出学生的位置,例如:猫!只有。下面的sql给出了学生的位置,但是没有考虑考试,就没有区分examtyp.it排名。
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(*) FROM $table a
WHERE a.total_marks > b.total_marks ) AS rank
FROM $table b WHERE admNo=? AND examCategory=? AND termId=? AND year=?
ORDER BY rank LIMIT 1";
$res = $this->db->query($sql, array($admNo, $examCategory, $term, $year));
答案 0 :(得分:0)
尝试Query
SET @rank=0;
select
@rank := @rank+1 AS rank
result_id,
marks_scored,
admNo,
Aggregate_points,
year
from tale_name
order by marks_scored DESC
答案 1 :(得分:0)
尝试此查询
查询1 :
select
@rn:=if(@prv=examtyp, @rn+1, 1) as rId,
admNo,
@prv:=examtyp as exmtyp,
marks
from table1
join
(select @rn:=0,@prv:='') tmp
order by exmtyp, marks desc
<强> SQL FIDDLE 强>:
| RID | ADMNO | EXMTYP | MARKS |
---------------------------------
| 1 | 4093 | AVERAGE | 569 |
| 2 | 2129 | AVERAGE | 545 |
| 1 | 4093 | CAT1 | 569 |
| 2 | 2129 | CAT1 | 525 |
| 1 | 4093 | CAT2 | 556 |
| 2 | 2129 | CAT2 | 550 |
| 1 | 4093 | FINAL | 581 |
| 2 | 2129 | FINAL | 559 |
查询1 :
select * from (
select
@rn:= @rn+1 as rId,
admNo,
examtyp,
marks
from table1
join
(select @rn:=0) tmp
where examtyp='CAT1'
order by examtyp, marks desc
) tmp where tmp.admNo=2129
| RID | ADMNO | EXAMTYP | MARKS |
---------------------------------
| 2 | 2129 | CAT1 | 525 |
答案 2 :(得分:0)
试试这个 -
SELECT q1.rownum
FROM
(
SELECT *, @rownum:=@rownum + 1 AS rownum
FROM $table t, (SELECT @rownum:=0) r
WHERE examtyp = 'CAT1'
ORDER BY marks
) q1
WHERE q1.admNo=?
2)由于你修改了相同标记获得相同等级的要求,你可能需要做这样的事情 -
SELECT q1.rownum
FROM
(
SELECT *, @rownum:=@rownum + 1 AS rownum
FROM
(SELECT DISTINCT marks FROM table1 t WHERE t.examtyp = 'CAT1' ORDER BY t.marks) q2,
(SELECT @rownum:=0) r
) q1,
table1 t2
WHERE
t2.examtyp = 'CAT1'
AND t2.marks=q1.marks
AND t2.admNo=?;
以上,您需要在两个地方更改examCategory
。
这不是最优化的查询..但它会做你的工作。
3)按照你的第三个要求增加下一个学生的数量,这可能会成功 -
SELECT ROWNUM
FROM
(
SELECT q1.marks, min(q1.rownum) AS rownum
FROM
(
SELECT t1.marks, @rownum:=@rownum + 1 AS rownum
FROM
table1 t1,
(SELECT @rownum:=0) r
WHERE
t1.examtyp='CAT1'
ORDER BY t1.marks asc
) q1
GROUP BY q1.marks
) q2,
table1 t2
WHERE
t2.examtyp = 'CAT1'
AND t2.marks=q2.marks;
AND t2.admNo=?;
答案 3 :(得分:0)
这应该适合你:
SELECT res_ID,
admNo,
stream,
examtyp,
termId,
grade,
points,
`year`,
Position
FROM ( SELECT @r:= CASE WHEN @e = examtyp THEN @r + CASE WHEN @p = points THEN 0 ELSE @i END ELSE 1 END Position,
@i:= CASE WHEN @p = points THEN @i + 1 ELSE 1 END incr,
@e:= Examtyp,
@p:= points,
res_ID,
admNo,
stream,
examtyp,
termId,
grade,
points,
`year`
FROM T,
(SELECT @e:= '') e,
(SELECT @r:= 0) r,
(SELECT @p:= 0) p,
(SELECT @i:= 0) i
ORDER BY examtyp, points
) T
WHERE T.admNo = 4093
AND T.Examtyp = 'CAT1'
它使用与建议使用的变量相同的原则,但也使用examtyp
分区,为每个新的检查类型重置位置0,它还记录以前的点来处理关系,所以如果3个人得到相同的标记他们都得到相同的位置。
<强> Example on SQL Fiddle 强>
请注意,在小提琴的底部窗格中AVERAGE
的结果是相等的,因此两者都得到位置= 1