sql常用操作之高级条件语句
文章目录
- 题1
- 题2
- 题3
- with..as创建临时表方式
- 子表方式:
- 题4
题1
https://www.nowcoder.com/practice/2ed07ff8f67a474d90523b88402e401b?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0
select uid,nick_name,achievement
FROM user_info left join exam_record using(uid) # user_info作主表left join practice_record as p_r using(uid)
where achievement between 1200 and 2500and nick_name like "牛客%号"and (start_time like "_____09%" or month(p_r.submit_time)=9 )
group by uid
select uid,nick_name,achievement
from user_info
where nick_name like '牛客%号'
and achievement between 1200 and 2500
and (uid in(select uidfrom exam_recordwhere date_format(submit_time, '%Y%m') = '202109')or uid in(select uidfrom practice_recordwhere date_format(submit_time, '%Y%m') = '202109')
)
题2
https://www.nowcoder.com/practice/1c5075503ccf4de1882976b2fff2c072?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0
select uid,exam_id,round(avg(score), 0) as avg_score
from user_info left join exam_record using(uid)left join examination_info using(exam_id)
where (nick_name like "牛客%号" or nick_name rlike "^[0-9]+$" )and tag rlike "^(c|C).*"and score is not null
group BY uid,exam_id
order by uid,avg_score
select uid, exam_id, round(avg(score), 0) as avg_score
from exam_record
where uid in (select uid from user_info# 正则表达式where nick_name rlike '^牛客[0-9]+号$'or nick_name rlike '^[0-9]+$')and exam_id in (select exam_idfrom examination_info# 通配符where tag like 'C%'or tag like 'c%')and score IS NOT NULL
group by uid, exam_id
order by uid, avg_score;
select uid, exam_id, round(avg(score), 0) as avg_score
from exam_record
group by uid, exam_id
having uid in (select uid from user_info# 正则表达式where nick_name rlike '^牛客[0-9]+号$'or nick_name rlike '^[0-9]+$')and exam_id in (select exam_idfrom examination_info# 通配符where tag like 'C%'or tag like 'c%')and avg_score IS NOT NULL # 注意这里若是score is not null,则报"Unknown column 'score' in 'having clause'"
order by uid, avg_score;
题3
https://www.nowcoder.com/practice/f72d3fc27dc14f3aae76ee9823ccca6b?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0
with…as创建临时表方式
WITH t_tag_count as (SELECT uid, `level`,COUNT(start_time) - COUNT(submit_time) as incomplete_cnt, -- 未完成数ROUND(IFNULL(1 - COUNT(submit_time) / COUNT(start_time), 0), 3) as incomplete_rate, -- 此人未完成率COUNT(start_time) as total_cnt -- 总作答数
# FROM exam_record RIGHT JOIN user_info USING(uid) FROM user_info LEFT JOIN exam_record USING(uid) #user_info 作主表GROUP BY uid
)SELECT uid, incomplete_cnt, incomplete_rate
FROM t_tag_count
WHERE EXISTS ( SELECT uid FROM t_tag_count WHERE `level` = 0 AND incomplete_cnt > 2 ) AND `level` = 0 UNION ALLSELECT uid, incomplete_cnt, incomplete_rate
FROM t_tag_count
WHERE NOT EXISTS (SELECT uid FROM t_tag_count WHERE `level` = 0 AND incomplete_cnt > 2 ) AND total_cnt > 0ORDER BY incomplete_rate;
子表方式:
SELECT uid, incomplete_cnt, incomplete_rate
FROM (select ui.uid uid, level, ###一定要是ui.uid 才能和level一一对应count(start_time)-count(submit_time) incomplete_cnt,round(ifnull((count(start_time)-count(submit_time))/count(start_time),0),3) incomplete_rate,COUNT(start_time) as total_cntfrom exam_record erright join user_info ui on er.uid=ui.uidgroup by uid) as a
WHERE EXISTS ( #判断为 有任意一个0级用户未完成试卷数大于2SELECT uid FROM (select ui.uid uid, level, ###一定要是ui.uid 才能和level一一对应count(start_time)-count(submit_time) incomplete_cnt,round(ifnull((count(start_time)-count(submit_time))/count(start_time),0),3) incomplete_rate,COUNT(start_time) as total_cntfrom exam_record erright join user_info ui on er.uid=ui.uidgroup by uid) as aWHERE `level` = 0 AND incomplete_cnt > 2 )
AND level = 0 #输出0级用户的未完成数和未完成率union all ##两种情况用union all 连接SELECT uid, incomplete_cnt, incomplete_rate
FROM (select ui.uid uid, level, ###一定要是ui.uid 才能和level一一对应count(start_time)-count(submit_time) incomplete_cnt,round(ifnull((count(start_time)-count(submit_time))/count(start_time),0),3) incomplete_rate,COUNT(start_time) as total_cntfrom exam_record erright join user_info ui on er.uid=ui.uidgroup by uid) a
WHERE not EXISTS ( #判断为 没有任意一个0级用户未完成试卷数大于2SELECT uid FROM (select ui.uid uid, level, ###一定要是ui.uid 才能和level一一对应count(start_time)-count(submit_time) incomplete_cnt,round(ifnull((count(start_time)-count(submit_time))/count(start_time),0),3) incomplete_rate,COUNT(start_time) as total_cntfrom exam_record erright join user_info ui on er.uid=ui.uidgroup by uid) aWHERE `level` = 0 AND incomplete_cnt > 2 )
AND total_cnt >0 #筛选有作答记录的用户 即总作答数大于0即可
order by incomplete_rate asc
其它:
WITH target_user AS (SELECT user_info.uid, COUNT(1) AS incomplete_cntFROM exam_record LEFT JOIN user_info ON exam_record.uid = user_info.uidWHERE user_info.level = 0 AND submit_time IS NULLGROUP BY user_info.uidHAVING incomplete_cnt > 2
), target_user_exist AS (
SELECT COUNT(1) AS `exist` FROM target_user)
, total_summary AS (SELECTuser_info.uid,MAX(user_info.level) AS level,SUM(IF(submit_time IS NULL AND start_time IS NOT NULL, 1, 0)) AS incomplete_cnt,SUM(IF(submit_time IS NULL AND start_time IS NOT NULL, 1, 0)) / COUNT(1) AS incomplete_rate,SUM(IF(start_time IS NOT NULL, 1, 0)) AS has_submitFROM user_info LEFT JOIN exam_recordON user_info.uid = exam_record.uidGROUP BY user_info.uid
)SELECTuid,incomplete_cnt,ROUND(incomplete_rate, 3)
FROM total_summary LEFT JOIN target_user_exist ON 1=1
WHERE (exist=0 AND has_submit>0) OR (exist=1 AND level=0)
ORDER BY incomplete_rate ASC
题4
https://www.nowcoder.com/practice/ebff819fd38c46db8a42dfe43ca7b33a?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0
本题关于case…when…then…[else]…end有多种写法:
(case when score<60 then '差'when score<75 then '中'when score<90 then '良'else '优' end) as score_grade(case when score >= 90 then '优'when score >= 75 then '良'when score >= 60 then '中'else '差' end) as score_grade(case when score>=90 then "优"when score between 75 and 89 then "良"when score between 60 and 74 then "中"else "差" end) as score_grade (case when score>=90 then '优'when score>=75 and score<90 then '良'when score>=60 and score<75 then '中'else '差' end) as score_grade
法1:
selectlevel, score_grade, round(count(uid) / total, 3) as ratio
from (select u_i.uid,exam_id, score, level,(case when score >= 90 then '优'when score >= 75 then '良'when score >= 60 then '中'else '差' end) as score_grade,count(*) over(partition by level) as totalfrom user_info u_i join exam_record e_r on u_i.uid = e_r.uidwhere score is not null ) as user_grade_table
group by level, score_grade
order by level desc, ratio desc
法2:
with t1 as(select level,case when score<60 then '差'when score<75 then '中'when score<90 then '良'else '优' end as score_gradefrom exam_record join user_info on exam_record.uid=user_info.uidwhere score is not null)select t1.level, score_grade, round(count(score_grade) / ct,3) as cnt
from t1 join (select level,count(level) ctfrom t1group by level) as t2
on t1.level=t2.level
group by t1.level,score_grade
order by t1.level desc,cnt desc;
上面方法的改写:
with t1 as(select level,case when score<60 then '差'when score<75 then '中'when score<90 then '良'else '优' end as score_gradefrom exam_record join user_info on exam_record.uid=user_info.uidwhere score is not null),
t2 as (select level,count(level) as ctfrom t1group by level)select t1.level, score_grade, round(count(score_grade) / ct,3) as cnt
from t1 join t2
on t1.level=t2.level
group by t1.level,score_grade
order by t1.level desc,cnt desc;
法3:
with t as (select u_i.uid,exam_id,score,level,case when score>=90 then '优' when score>=75 and score<90 then '良'when score>=60 and score<75 then '中'else '差' end as score_grade,count(*) over (partition by level) as totalfrom user_info u_i join exam_record using(uid)where score is not NULL)select level,score_grade,round(count(*) / total,3) as ratio
from t
group by level, score_grade
order by level desc, ratio desc
sql常用操作之高级条件语句相关推荐
- MYSQL 中 SQL 常用操作
SQL常用操作大全 1.SQL简单的操作 -- 常见SQL-- 查询 SELECT id,username,password FROM admin;-- 插入 INSERT INTO admin(us ...
- MySQL中级优化教程(一)——SQL常用优化工具及explain语句的使用
序言: 说来惭愧,java学了两年,期间虽在博客上记了一些东西,可也不曾写过什么系统的教程,前一段时间开始学习MySQL数据库优化相关的知识,就想着趁着这个机会好好整理一份电子档出来,即方便自己之后回 ...
- Python 炫技操作(01):条件语句的七种写法
首发于微信公众号:Python编程时光 系列导读 Python 炫技操作:条件语句的七种写法 Python 炫技操作:合并字典的七种方法 Python 炫技操作:连接列表的八种方法 有的人说 Pyth ...
- mysql删除所有男生_My SQL常用操作汇总
写这篇随笔的目的是我发现了在上一篇关于My SQL的随笔中存在一些不严谨的代码问题,在这里再次简单的总结一下并加以改进,以代码为主. # !每行命令必须以分号(;)结尾 先通过命令行进入数据库客户端 ...
- sql常用操作(含指定位置添加字段、修改到指定位置后等)
1) 创建用户表(user) 要求:字段 类型 长度uid intuname varchar 20password varchar 20birthday date create table user( ...
- [FineReport]高级条件分组、斜线、自动查询、控件编辑属性、条件属性
通常我们在做按某一条件分组汇总数据的时候,都是透过SQL去处理.如果您不熟悉SQL怎么办?FR高级条件分组可以帮您实现. 一.新建数据集 select A0188 AS ID, CAST(FLOOR( ...
- MySQL基础学习(三)————SQL语句的常用操作
文章目录 1.库 1.1库的创建 1.2 库的删除 1.3 库的修改 1.4 库的查找 2.表 2.1 表的创建 2.2 表的删除 2.3 表的修改 2.4 表的查找 3.数据或者记录 3.1 数据的 ...
- 【数据库】第一章 数据库的分类、SQL、数据库、表和表记录的常用操作
[数据库]第一章 数据库的分类.SQL.数据库与表的常用操作 文章目录 [数据库]第一章 数据库的分类.SQL.数据库与表的常用操作 一.数据库的分类 1.关系型数据库 2.非关系型数据库 3.MyS ...
- 数据库常用操作语句总结
数据库常用操作语句总结 一.基础 1.select 语句 2.select distinct 语句 3.where 子句 4.and 和 or 运算符 5.order by 语句 6.insert i ...
最新文章
- 剑指offer:包含min函数的栈 python实现
- javascript与jQuery对照学习总结(一)(一些常规操作)
- python 编程入门-python编程入门(第3版)
- 通过libVirt抓取kvm虚拟机监控指标数据
- 小白学数据分析-----聚类分析理论之K-means理论篇
- Python词频对比并导入CSV文件
- 面试题08(C++)
- 将一个对象拆开拼接成URL
- 蓝桥杯c语言a组2015,2015第七届蓝桥杯决赛C语言A组--穿越雷区(DFS)
- 基于JAVA+SpringBoot+Mybatis+MYSQL的疫情信息管理系统
- 华为手机连电脑_移动办公利器华为MatePad Pro:能写会画有键盘,能当电脑连手机...
- HTML之组件margin、padding
- (转)一种可以穿透还原卡和还原软件的代码
- 世界以痛吻我,我要报之以歌
- php manual 下载,PHP - Manual手册 - Download下载
- 中国“神威•太湖之光”蝉联世界超算冠军
- 第三届全国大学生算法设计与编程挑战赛 (冬季赛)部分题解
- 06-1-SVM原理
- python - 输入某年某月某日,判断这一天是这一年的第几天?
- A005-AS项目结构解析