当前位置:网站首页>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
边栏推荐
- Heartless sword English translation of Xi Murong's youth without complaint
- Hedhat firewall
- [Chongqing Guangdong education] National Open University spring 2019 1042 international economic law reference questions
- Educational Codeforces Round 122 (Rated for Div. 2) ABC
- 微信小程序:全新独立后台月老办事处一元交友盲盒
- PHP wechat official account development
- MySQL REGEXP:正则表达式查询
- Visual explanation of Newton iteration method
- DOM basic syntax
- One plus six brushes into Kali nethunter
猜你喜欢
Armv8-a programming guide MMU (3)
Codeforces Global Round 19 ABC
Yyds dry goods inventory [Gan Di's one week summary: the most complete and detailed in the whole network]; detailed explanation of MySQL index data structure and index optimization; remember collectio
Phpstrom setting function annotation description
Nebula Importer 数据导入实践
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
Exploration and Practice of Stream Batch Integration in JD
微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
Win:使用 Shadow Mode 查看远程用户的桌面会话
After reading the average code written by Microsoft God, I realized that I was still too young
随机推荐
La jeunesse sans rancune de Xi Murong
Hedhat firewall
Win:将一般用户添加到 Local Admins 组中
How to build a technical team that will bring down the company?
线上故障突突突?如何紧急诊断、排查与恢复
Restful Fast Request 2022.2.1发布,支持cURL导入
Win:使用 Shadow Mode 查看远程用户的桌面会话
力扣剑指offer——二叉树篇
Luo Gu Pardon prisoners of war
官宣!第三届云原生编程挑战赛正式启动!
Huawei machine test question: longest continuous subsequence
How to safely eat apples on the edge of a cliff? Deepmind & openai gives the answer of 3D security reinforcement learning
【CTF】AWDP总结(Web)
微信小程序:最新wordpress黑金壁纸微信小程序 二开修复版源码下载支持流量主收益
Nebula Importer 数据导入实践
Li Kou Jianzhi offer -- binary tree chapter
Redis' hyperloglog as a powerful tool for active user statistics
無心劍英譯席慕容《無怨的青春》
JS implementation determines whether the point is within the polygon range
Win: enable and disable USB drives using group policy