当前位置:网站首页>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
边栏推荐
- batchnorm. Py this file single GPU operation error solution
- 增量备份 ?db full
- Codeforces Round #770 (Div. 2) ABC
- Word processing software
- [flutter topic] 64 illustration basic textfield text input box (I) # yyds dry goods inventory #
- Introduction to redis (1)
- 如何做一个炫酷的墨水屏电子钟?
- 微信小程序;胡言乱语生成器
- Wechat applet: Xingxiu UI v1.5 WordPress system information resources blog download applet wechat QQ dual end source code support WordPress secondary classification loading animation optimization
- Win:使用 PowerShell 检查无线信号的强弱
猜你喜欢

官宣!第三届云原生编程挑战赛正式启动!

Introduction to redis (1)

Exploration and Practice of Stream Batch Integration in JD

Phpstrom setting function annotation description

Application and Optimization Practice of redis in vivo push platform

Lsblk command - check the disk of the system. I don't often use this command, but it's still very easy to use. Onion duck, like, collect, pay attention, wait for your arrival!

DOM basic syntax

微信小程序:全网独家小程序版本独立微信社群人脉

MATLB | multi micro grid and distributed energy trading

MySQL REGEXP:正则表达式查询
随机推荐
RichView TRVUnits 图像显示单位
es使用collapseBuilder去重和只返回某个字段
Es uses collapsebuilder to de duplicate and return only a certain field
增量备份 ?db full
JVM's responsibility - load and run bytecode
19. Delete the penultimate node of the linked list
JS implementation determines whether the point is within the polygon range
"2022" is a must know web security interview question for job hopping
Outlook:总是提示输入用户密码
When the industrial Internet era is truly developed and improved, it will witness the birth of giants in every scene
Database postragesq peer authentication
C basic knowledge review (Part 3 of 4)
JVM - when multiple threads initialize the same class, only one thread is allowed to initialize
PowerShell:在代理服务器后面使用 PowerShell
Comment mettre en place une équipe technique pour détruire l'entreprise?
After reading the average code written by Microsoft God, I realized that I was still too young
Package What is the function of JSON file? What do the inside ^ angle brackets and ~ tilde mean?
The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
Interesting practice of robot programming 15- autoavoidobstacles
Database postragesql client authentication