sql之每日五题day02--多表联查/聚合函数/多值判断/函数

sql之每日五题day01--多表联查/聚合函数

    • [where和group by同时出现](#where和group by同时出现)
    • [分别查看&结果不去重--union all](#分别查看&结果不去重--union all)
    • [union all+细节别名](#union all+细节别名)
    • [case when多值判断](#case when多值判断)
    • 数据处理函数

where和group by同时出现

  • SQL24 统计每个用户的平均刷题数

仅查看山东大学的用户在不同难度下的每个用户的平均答题题目数

sql 复制代码
select  university,	difficult_level,	count(qpd.question_id)/count(distinct qpd.device_id) as avg_answer_cnt
from question_practice_detail as qpd
left join user_profile as up on up.device_id=qpd.device_id
left join question_detail as qd on qd.question_id=qpd.question_id
where university='山东大学'	
group by difficult_level

分别查看&结果不去重--union all

  • SQL25 查找山东大学或者性别为男生的信息

分别查看学校为山东大学或者性别为男性的用户的device_id、gender、age和gpa数据,结果不去重

sql 复制代码
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+细节别名

  • SQL26 计算25岁以上和以下的用户数量

现在运营想要将用户划分为25岁以下和25岁及以上两个年龄段,分别查看这两个年龄段用户数量

本题注意:age为null 也记为 25岁以下

sql 复制代码
select '25岁以下' as age_cut,	count(1) as number
from user_profile
where age < 25 or age is null
union all
select '25岁及以上' as age_cut,	count(1) as number
from user_profile
where age >= 25

case when多值判断

  • SQL27 查看不同年龄段的用户明细

现在运营想要将用户划分为20岁以下,20-24岁,25岁及以上三个年龄段,分别查看不同年龄段用户的明细情况,请取出相应数据。(注:若年龄为空请返回其他。)

  • 格式
sql 复制代码
case...when...then...else...end
sql 复制代码
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

数据处理函数

  • SQL28 计算用户8月每天的练题数量

现在运营想要计算出2021年8月每天用户练习题目的数量,请取出相应数据。

sql 复制代码
select day(date) as day,count(question_id) as	question_cnt
from question_practice_detail
where year(date)=2021 and month(date)=8
group by day
相关推荐
蜡笔小炘15 分钟前
LVS -- 利用防火墙标签(FireWall Mark)解决轮询错误
服务器·数据库·lvs
韩立学长19 分钟前
基于Springboot泉州旅游攻略平台d5h5zz02(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·旅游
Re.不晚1 小时前
MySQL进阶之战——索引、事务与锁、高可用架构的三重奏
数据库·mysql·架构
老邓计算机毕设1 小时前
SSM智慧社区信息化服务平台4v5hv(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·ssm 框架·智慧社区、·信息化平台
麦聪聊数据1 小时前
为何通用堡垒机无法在数据库运维中实现精准风控?
数据库·sql·安全·低代码·架构
2301_790300962 小时前
Python数据库操作:SQLAlchemy ORM指南
jvm·数据库·python
m0_736919102 小时前
用Pandas处理时间序列数据(Time Series)
jvm·数据库·python
亓才孓2 小时前
[JDBC]PreparedStatement替代Statement
java·数据库
m0_466525292 小时前
绿盟科技风云卫AI安全能力平台成果重磅发布
大数据·数据库·人工智能·安全
爱学习的阿磊3 小时前
使用Fabric自动化你的部署流程
jvm·数据库·python