当前位置:网站首页>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
边栏推荐
- Codeforces Global Round 19 ABC
- JS implementation determines whether the point is within the polygon range
- R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口
- Delaying wages to force people to leave, and the layoffs of small Internet companies are a little too much!
- MATLB|多微电网及分布式能源交易
- Pytorch common code snippet collection
- Do you know the eight signs of a team becoming agile?
- R language uses logistic regression and afrima, ARIMA time series models to predict world population
- Win:使用组策略启用和禁用 USB 驱动器
- PHP 约瑟夫环问题
猜你喜欢

A simple SSO unified login design

"2022" is a must know web security interview question for job hopping

PowerShell: use PowerShell behind the proxy server

Win: use PowerShell to check the strength of wireless signal

JVM's responsibility - load and run bytecode

Express routing, express middleware, using express write interface

JS implementation determines whether the point is within the polygon range

Li Kou Jianzhi offer -- binary tree chapter

PHP wechat official account development

Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
随机推荐
官宣!第三届云原生编程挑战赛正式启动!
Abacus mental arithmetic test
PHP Basics - detailed explanation of DES encryption and decryption in PHP
Kibana installation and configuration
Word processing software
19. Delete the penultimate node of the linked list
Database postragesq peer authentication
Redis(1)之Redis简介
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
微信小程序:微群人脉微信小程序源码下载全新社群系统优化版支持代理会员系统功能超高收益
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
He was laid off.. 39 year old Ali P9, saved 150million
Codeforces Round #770 (Div. 2) ABC
172. Zero after factorial
Vulnstack3
Outlook:总是提示输入用户密码
如何搭建一支搞垮公司的技術團隊?
MySQL regexp: Regular Expression Query
Numpy library introductory tutorial: basic knowledge summary
C basic knowledge review (Part 3 of 4)