本文已参与「新人创作礼」活动,一起开启掘金创作之路
25.题目:现在运营想要分别查看学校为山东大学或者性别为男性的用户的device_id、gender、age和gpa数据,请取出相应结果,结果不去重。
SELECT device_id,gender,age,gpa
FROM user_profile
where university='山东大学'
union all
SELECT device_id,gender,age,gpa
FROM user_profile
where gender='male'
两种条件。union all结果不去重,union结果去重。
26.题目:现在运营想要将用户划分为25岁以下和25岁及以上两个年龄段,分别查看这两个年龄段用户数量
本题注意:age为null 也记为 25岁以下
SELECT
CASE
WHEN age<25 or age is null THEN '25岁以下'
WHEN age>=25 THEN '25岁及以上'
END age_cut,count(*) as number
FROM user_profile
group by age_cut
CASE
WHEN 情况 THEN 处理方式
END 统计结果的标题
最后要按照25岁上下的情况进行分组,才能出现两种情况。
27.题目:现在运营想要将用户划分为20岁以下,20-24岁,25岁及以上三个年龄段,分别查看不同年龄段用户的明细情况,请取出相应数据。(注:若年龄为空请返回其他。)
select device_id,gender,
case
when age<20 then '20岁以下'
when age between 20 and 24 then '20-24岁'
when age>=25 then '25岁及以上'
else '其他'
end age_cut
from user_profile
比26多了一个else分支
28.题目:现在运营想要计算出2021年8月每天用户练习题目的数量,请取出相应数据。
select
day(date) as day,
count(question_id) as question_cnt
from question_practice_detail
where month(date)=8
group by day
day()可以把日期只留最后的日,去掉年月。
把月提取出来用month() ,因此提取年用year()
DAYOFWEEK 返回日期的星期索引。WEEKDAY。前者从星期日=1开始,后者从星期一=0开始。
也可以用date_format(date, "%Y-%m")="202108" Y表示年,m表示月