<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ssj.service.mapper.OpenTestMapper">


    <select id="countResult" resultType="com.ssj.bean.mybatis.OpenTestMBDto" parameterType="java.util.Map">
        select count(1)                                            total,
        sum(case when accuracy_rate &lt; #{map.accuracyRate} then 1 else 0 end) accuracyLower,
        sum(case when `time` > #{map.time} then 1 else 0 end)          timeLower
        from tb_test_result
        where grade = #{map.grade}
        and subject = #{map.subject}
        and version = #{map.version}
    </select>

    <select id="findRankingResult" resultType="com.ssj.bean.mybatis.OpenTestMBDto" parameterType="java.util.Map">
        SELECT @rownum := @rownum + 1 AS rankingNo,
    	t.score,
    	(select name from tb_test_user where user_id = t.user_id limit 1) name
        FROM
    	( SELECT @rownum := 0 ) r,
    	tb_test_result t
    	where t.grade = #{map.grade}
        and t.subject = #{map.subject}
        and t.is_delete = 0
        <if test="map.version != null and map.version != '' ">
            and t.version = #{map.version}
        </if>
        <if test="map.unit != null and map.unit != '' ">
            and t.unit = #{map.unit}
        </if>
        <if test="map.type != null and map.type != '' ">
            and t.type = #{map.type}
        </if>
		order by t.score desc,t.create_time
    </select>

</mapper>