SET sql_mode = 'NO_UNSIGNED_SUBTRACTION'; select * ,exam_cnt_rank_21-exam_cnt_rank_20 as rank_delta from( select *,concat(round((exam_cnt_21-exam_cnt_20)*100/exam_cnt_20,1),'%') as growth_rate, rank()over(order by exam_cnt_20 desc) as exam_cnt_rank_20, rank()over(order by exam_cnt_21 desc) as exam_cnt_rank_21 from( select i.tag,count(if(year(submit_time)=2020 and month(submit_time)<=6,r.id,null)) as exam_cnt_20, count(if(year(submit_time)=2021 and month(submit_time)<=6,r.id,null)) as exam_cnt_21 from exam_record r left join examination_info i on r.exam_id=i.exam_id group by i.tag ) a )b where exam_cnt_20*exam_cnt_21>0 order by growth_rate desc,exam_cnt_rank_21 desc ;
时间: 2023-09-13 07:04:39 浏览: 153
gpio_return_gpio_set_value_GPIO_gpio_set_
5星 · 资源好评率100%
这是一个MySQL查询语句,用于计算考试记录的增长率和排名差异。同时,查询中设置了sql_mode为'NO_UNSIGNED_SUBTRACTION',以避免无符号数相减时出现错误。
请注意,这是一个假设你已经创建了相关的表并插入了数据的查询语句。如果你还没有创建表或插入数据,请确保在运行此查询之前进行相应的准备工作。
以下是查询语句:
```
SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';
SELECT *, exam_cnt_rank_21 - exam_cnt_rank_20 AS rank_delta
FROM (
SELECT *,
CONCAT(ROUND((exam_cnt_21 - exam_cnt_20) * 100 / exam_cnt_20, 1), '%') AS growth_rate,
RANK() OVER (ORDER BY exam_cnt_20 DESC) AS exam_cnt_rank_20,
RANK() OVER (ORDER BY exam_cnt_21 DESC) AS exam_cnt_rank_21
FROM (
SELECT i.tag,
COUNT(IF(YEAR(submit_time) = 2020 AND MONTH(submit_time) <= 6, r.id, NULL)) AS exam_cnt_20,
COUNT(IF(YEAR(submit_time) = 2021 AND MONTH(submit_time) <= 6, r.id, NULL)) AS exam_cnt_21
FROM exam_record r
LEFT JOIN examination_info i ON r.exam_id = i.exam_id
GROUP BY i.tag
) a
) b
WHERE exam_cnt_20 * exam_cnt_21 > 0
ORDER BY growth_rate DESC, exam_cnt_rank_21 DESC;
```
这个查询将返回按照考试记录的增长率和2021年上半年考试数量排名的结果集。其中,growth_rate表示增长率,exam_cnt_rank_20表示2020年上半年考试数量的排名,exam_cnt_rank_21表示2021年上半年考试数量的排名,rank_delta表示排名差异。查询结果按照增长率降序和2021年上半年考试数量排名降序排序。同时,通过`WHERE exam_cnt_20 * exam_cnt_21 > 0`条件过滤掉考试数量为零的记录。
请根据你的实际需求和数据表结构进行适当的调整和修改。
阅读全文