当前位置:网站首页>SQL 26 calculation under 25 years of age or older and the number of users
SQL 26 calculation under 25 years of age or older and the number of users
2022-07-30 13:22:00 【java factory manager】
SQL 26计算25岁以上和以下的用户数量
携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第2天,点击查看活动详情
1、题目
现在运营想要将用户划分为25岁以下和25岁及以上两个年龄段,分别查看这两个年龄段用户数量
本题注意:age为null 也记为 25岁以下
示例:user_profile
| id | device_id | gender | age | university | gpa | active_days_within_30 | question_cnt | answer_cnt |
|---|---|---|---|---|---|---|---|---|
| 1 | 2138 | male | 21 | 北京大学 | 3.4 | 7 | 2 | 12 |
| 2 | 3214 | male | 复旦大学 | 4 | 15 | 5 | 25 | |
| 3 | 6543 | female | 20 | 北京大学 | 3.2 | 12 | 3 | 30 |
| 4 | 2315 | female | 23 | 浙江大学 | 3.6 | 5 | 1 | 2 |
| 5 | 5432 | male | 25 | 山东大学 | 3.8 | 20 | 15 | 70 |
| 6 | 2131 | male | 28 | 山东大学 | 3.3 | 15 | 7 | 13 |
| 7 | 4321 | male | 26 | 复旦大学 | 3.6 | 9 | 6 | 52 |
根据示例,你的查询应返回以下结果:
| age_cut | number |
|---|---|
| 25岁以下 | 4 |
| 25岁及以上 | 3 |
示例1
输入:
drop table if exists `user_profile`;
drop table if exists `question_practice_detail`;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` int ,
`answer_cnt` int
);
CREATE TABLE `question_practice_detail` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`question_id`int NOT NULL,
`result` varchar(32) NOT NULL
);
CREATE TABLE `question_detail` (
`id` int NOT NULL,
`question_id`int NOT NULL,
`difficult_level` varchar(32) NOT NULL
);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);
INSERT INTO question_practice_detail VALUES(1,2138,111,'wrong');
INSERT INTO question_practice_detail VALUES(2,3214,112,'wrong');
INSERT INTO question_practice_detail VALUES(3,3214,113,'wrong');
INSERT INTO question_practice_detail VALUES(4,6543,111,'right');
INSERT INTO question_practice_detail VALUES(5,2315,115,'right');
INSERT INTO question_practice_detail VALUES(6,2315,116,'right');
INSERT INTO question_practice_detail VALUES(7,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(8,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(9,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(10,2131,113,'right');
INSERT INTO question_practice_detail VALUES(11,5432,113,'wrong');
INSERT INTO question_practice_detail VALUES(12,2315,115,'right');
INSERT INTO question_practice_detail VALUES(13,2315,116,'right');
INSERT INTO question_practice_detail VALUES(14,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(15,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(16,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(17,2131,113,'right');
INSERT INTO question_practice_detail VALUES(18,5432,113,'wrong');
INSERT INTO question_practice_detail VALUES(19,2315,117,'wrong');
INSERT INTO question_practice_detail VALUES(20,5432,117,'wrong');
INSERT INTO question_practice_detail VALUES(21,5432,112,'wrong');
INSERT INTO question_practice_detail VALUES(22,2131,113,'right');
INSERT INTO question_practice_detail VALUES(23,5432,113,'wrong');
INSERT INTO question_detail VALUES(1,111,'hard');
INSERT INTO question_detail VALUES(2,112,'medium');
INSERT INTO question_detail VALUES(3,113,'easy');
INSERT INTO question_detail VALUES(4,115,'easy');
INSERT INTO question_detail VALUES(5,116,'medium');
INSERT INTO question_detail VALUES(6,117,'easy');
输出:
25岁以下|4
25岁及以上|3
复制代码2、思路🧠
问题分解:
- Age is divided into two groups:
"25岁及以上", "25岁以下" - 统计用户数量:count,Each segment is counted separately,用
group by age_cut分组 - Of course this question can also be usedUNION ALL解决,用if更直观
解决方法:
UNION 组合查询(去重),UNION ALL组合查询(不去重)
IF条件查询,
IF(age>=25, "25岁及以上", "25岁以下")CASE函数查询,是一种多分支的函数,可以根据条件列表的值返回多个可能的结果表达式中的一个;可用在任何允许使用表达式的地方,但不能单独作为一个语句执行.分为:简单CASE函数、搜索CASE函数
#简单CASE函数 CASE 测试表达式 WHEN 简单表达式1 THEN 结果表达式1 WHEN 简单表达式2 THEN 结果表达式2 …… WHEN 简单表达式n THEN 结果表达式n [ ELSE 结果表达式n+1 ] END #搜索CASE函数 CASE WHEN 布尔表达式1 THEN 结果表达式1 WHEN 布尔表达式2 THEN 结果表达式2 …… WHEN 布尔表达式n THEN 结果表达式n [ ELSE 结果表达式n+1 ] END 复制代码
3、代码
commit AC
SELECT '25岁以下' as age_cut,COUNT(*)
FROM user_profile
WHERE age < 25 OR age is null
UNION ALL
SELECT '25岁及以上' as age_cut,COUNT(*)
FROM user_profile
WHERE age >= 25
SELECT IF(age>=25,'25岁及以上','25岁以下') AS age_cut,COUNT(*)
FROM user_profile
GROUP BY age_cut
SELECT (case when age>=25 then '25岁及以上' else '25岁以下' end) AS age_cut,COUNT(*)
FROM user_profile
GROUP BY age_cut
复制代码4、总结
Right on the topicSQLgrammar and basics,学会使用UNIONto perform a combined query,IF、CASEOther conditional queries will also be used,like inner join、外连接、左连接、There must be a relevant understanding of the right join and so on,Second when you write a lotSQL之后,Just learn to do itSQL的优化,This will greatly reduce the data query time.
️来自专栏《Mysql每日一题》欢迎订阅️
厂长写博客目的初衷很简单,希望大家在学习的过程中少走弯路,多学一些东西,对自己有帮助的留下你的赞赞或者关注都是对我最大的支持,你的关注和点赞给厂长每天更文的动力.
对文章其中一部分不理解,都可以评论区回复我,我们来一起讨论,共同学习,一起进步!
边栏推荐
猜你喜欢
随机推荐
学习笔记——七周成为数据分析师《第一周:数据分析思维》
js背景切换时钟js特效代码
【软考软件评测师】基于规则说明的测试技术上篇
R语言向前或者向后移动时间序列数据(自定义滞后或者超前的期数):使用dplyr包中的lag函数将时间序列数据向后移动一天(设置参数n为负值)
Raja Koduri澄清Arc GPU跳票传闻 AXG年底前新推四条产品线
Using Baidu EasyDL to realize the recognition of the chef's hat of the bright kitchen
【语音识别】基于GMM-HMM的语音识别系统
[PostgreSQL] - Storage structure and cache shared_buffers
奇异值分解(SVD)原理与在降维中的应用(附带例题讲解)(纯理论)
SyntaxError: EOL while scanning string literal
shell脚本流程控制语句
学习笔记——七周成为数据分析师《第二周:业务》:业务分析指标
基于空洞补全的动态SLAM方法
jsArray array copy method performance test 2207292307
Smart pointer implementation conjecture
忆联:激活数据要素价值潜能,释放SAS SSD创新红利
EasyNVR更新版本至(V5.3.0)后页面不显示通道配置该如何解决?
CV-Model【2】:MobileNet v1
一本通循环结构的程序设计题解(2)
第十四天笔记








