当前位置:网站首页>Advanced conditional statements of common SQL operations
Advanced conditional statements of common SQL operations
2022-07-05 01:46:00 【stitchshaw】
List of articles
topic 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 Master table
left join practice_record as p_r using(uid)
where
achievement between 1200 and 2500
and nick_name like " Cattle guest % Number "
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 ' Cattle guest % Number '
and achievement between 1200 and 2500
and (
uid in(
select uid
from exam_record
where date_format(submit_time, '%Y%m') = '202109')
or uid in(
select uid
from practice_record
where date_format(submit_time, '%Y%m') = '202109')
)
topic 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 " Cattle guest % Number " 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
# Regular expressions
where nick_name rlike '^ Cattle guest [0-9]+ Number $'
or nick_name rlike '^[0-9]+$'
)
and exam_id in (select exam_id
from examination_info
# wildcard
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
# Regular expressions
where nick_name rlike '^ Cattle guest [0-9]+ Number $'
or nick_name rlike '^[0-9]+$'
)
and exam_id in (select exam_id
from examination_info
# wildcard
where tag like 'C%'
or tag like 'c%'
)
and avg_score IS NOT NULL # Notice if score is not null, Then newspaper "Unknown column 'score' in 'having clause'"
order by uid, avg_score;
topic 3
https://www.nowcoder.com/practice/f72d3fc27dc14f3aae76ee9823ccca6b?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0
with…as Create a temporary table
WITH t_tag_count as (
SELECT uid, `level`,
COUNT(start_time) - COUNT(submit_time) as incomplete_cnt, -- Outstanding
ROUND(IFNULL(1 - COUNT(submit_time) / COUNT(start_time), 0), 3) as incomplete_rate, -- This person's unfinished rate
COUNT(start_time) as total_cnt -- Total number of answers
# FROM exam_record RIGHT JOIN user_info USING(uid)
FROM user_info LEFT JOIN exam_record USING(uid) #user_info Master table
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 ALL
SELECT 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 > 0
ORDER BY incomplete_rate;
Sub table method :
SELECT uid, incomplete_cnt, incomplete_rate
FROM (
select
ui.uid uid, level, ### Must be ui.uid Talent and level One-to-one correspondence
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_cnt
from exam_record er
right join user_info ui on er.uid=ui.uid
group by uid
) as a
WHERE EXISTS ( # Judgment for There is any one 0 The number of uncompleted test papers of level 1 users is greater than 2
SELECT uid
FROM (
select
ui.uid uid, level, ### Must be ui.uid Talent and level One-to-one correspondence
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_cnt
from exam_record er
right join user_info ui on er.uid=ui.uid
group by uid
) as a
WHERE `level` = 0 AND incomplete_cnt > 2 )
AND level = 0 # Output 0 Unfinished number and unfinished rate of level users
union all ## In both cases union all Connect
SELECT uid, incomplete_cnt, incomplete_rate
FROM (
select
ui.uid uid, level, ### Must be ui.uid Talent and level One-to-one correspondence
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_cnt
from exam_record er
right join user_info ui on er.uid=ui.uid
group by uid
) a
WHERE not EXISTS ( # Judgment for No one 0 The number of uncompleted test papers of level 1 users is greater than 2
SELECT uid
FROM (
select
ui.uid uid, level, ### Must be ui.uid Talent and level One-to-one correspondence
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_cnt
from exam_record er
right join user_info ui on er.uid=ui.uid
group by uid
) a
WHERE `level` = 0 AND incomplete_cnt > 2 )
AND total_cnt >0 # Screen users with response records That is, the total answer is greater than 0 that will do
order by incomplete_rate asc
Other :
WITH target_user AS (
SELECT
user_info.uid,
COUNT(1) AS incomplete_cnt
FROM exam_record LEFT JOIN user_info ON exam_record.uid = user_info.uid
WHERE user_info.level = 0 AND submit_time IS NULL
GROUP BY user_info.uid
HAVING incomplete_cnt > 2
), target_user_exist AS (
SELECT COUNT(1) AS `exist` FROM target_user)
, total_summary AS (
SELECT
user_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_submit
FROM user_info LEFT JOIN exam_record
ON user_info.uid = exam_record.uid
GROUP BY user_info.uid
)
SELECT
uid,
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
topic 4
https://www.nowcoder.com/practice/ebff819fd38c46db8a42dfe43ca7b33a?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0
This topic is about case…when…then…[else]…end There are many ways of writing :
(case when score<60 then ' Bad '
when score<75 then ' in '
when score<90 then ' good '
else ' optimal ' end) as score_grade
(case when score >= 90 then ' optimal '
when score >= 75 then ' good '
when score >= 60 then ' in '
else ' Bad ' end) as score_grade
(case when score>=90 then " optimal "
when score between 75 and 89 then " good "
when score between 60 and 74 then " in "
else " Bad " end) as score_grade
(case when score>=90 then ' optimal '
when score>=75 and score<90 then ' good '
when score>=60 and score<75 then ' in '
else ' Bad ' end) as score_grade
Law 1:
select
level, score_grade,
round(count(uid) / total, 3) as ratio
from (
select u_i.uid,
exam_id, score, level,
(case when score >= 90 then ' optimal '
when score >= 75 then ' good '
when score >= 60 then ' in '
else ' Bad ' end) as score_grade,
count(*) over(partition by level) as total
from user_info u_i join exam_record e_r on u_i.uid = e_r.uid
where score is not null
) as user_grade_table
group by level, score_grade
order by level desc, ratio desc
Law 2:
with t1 as
(select level,
case when score<60 then ' Bad '
when score<75 then ' in '
when score<90 then ' good '
else ' optimal ' end as score_grade
from exam_record join user_info on exam_record.uid=user_info.uid
where score is not null)
select t1.level, score_grade, round(count(score_grade) / ct,3) as cnt
from t1 join (select level,count(level) ct
from t1
group by level) as t2
on t1.level=t2.level
group by t1.level,score_grade
order by t1.level desc,cnt desc;
Rewrite the above method :
with t1 as
(select level,
case when score<60 then ' Bad '
when score<75 then ' in '
when score<90 then ' good '
else ' optimal ' end as score_grade
from exam_record join user_info on exam_record.uid=user_info.uid
where score is not null),
t2 as
(select level,count(level) as ct
from t1
group 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;
Law 3:
with t as
(select u_i.uid,exam_id,score,level,
case when score>=90 then ' optimal '
when score>=75 and score<90 then ' good '
when score>=60 and score<75 then ' in '
else ' Bad ' end as score_grade,
count(*) over (partition by level) as total
from 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
边栏推荐
- 如何做一个炫酷的墨水屏电子钟?
- Using openpyxl module to write the binary list into excel file
- Kibana installation and configuration
- MySQL backup and recovery + experiment
- 微信小程序:全网独家小程序版本独立微信社群人脉
- 微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
- What is the current situation and Prospect of the software testing industry in 2022?
- phpstrom设置函数注释说明
- 线上故障突突突?如何紧急诊断、排查与恢复
- 【CTF】AWDP总结(Web)
猜你喜欢
微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
MySQL regexp: Regular Expression Query
R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口
【LeetCode】88. Merge two ordered arrays
JS implementation determines whether the point is within the polygon range
PowerShell:在代理服务器后面使用 PowerShell
One plus six brushes into Kali nethunter
Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide
Nebula importer data import practice
phpstrom设置函数注释说明
随机推荐
Heartless sword English translation of Xi Murong's youth without complaint
Outlook: always prompt for user password
Incremental backup? db full
Interesting practice of robot programming 15- autoavoidobstacles
Mysql database | build master-slave instances of mysql-8.0 or above based on docker
Wechat applet: independent background with distribution function, Yuelao office blind box for making friends
Exploration and practice of integration of streaming and wholesale in jd.com
Jcenter () cannot find Alibaba cloud proxy address
Win: add general users to the local admins group
Database postragesq PAM authentication
Wechat applet: exclusive applet version of the whole network, independent wechat community contacts
Introduction to redis (1)
Win:将一般用户添加到 Local Admins 组中
The MySQL team development specifications used by various factories are too detailed. It is recommended to collect them!
Game 280 of leetcode week
Luo Gu Pardon prisoners of war
Database postragesql lock management
Five ways to query MySQL field comments!
Runc hang causes the kubernetes node notready
es使用collapseBuilder去重和只返回某个字段