1

我需要一个 sql,它将按照特定考试类型(例如 CAT)中得分的顺序给出学生的位置!only。下面的sql给出了学生的位置,但不区分examtyp。它在不考虑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(*) 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));
4

4 回答 4

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 小提琴

| 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

SQL 小提琴

| RID | ADMNO | EXAMTYP | MARKS |
---------------------------------
|   2 |  2129 |    CAT1 |   525 |
于 2013-05-09T05:50:15.167 回答
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
于 2013-05-08T18:13:38.383 回答
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 人获得相同的分数,他们都是获得相同的位置。

SQL Fiddle 示例

请注意,在小提琴的底部窗格中,结果AVERAGE是相等的,因此两者都获得 position = 1

于 2013-05-09T08:11:21.683 回答
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=?;
于 2013-05-09T05:59:10.493 回答