【SQL进阶】【CASE/IF、COUNT/SUM、多条记录拼接为一个内容】Day03:聚合分组查询
〇、今日内容概述
一、聚合函数
1、SQL类别高难度试卷得分的截断平均值【去最高最低分求平均】
自己的想法
SELECT tag, difficulty, ROUND((SUM(score)-MIN(score)-MAX(score))/(COUNT(score)-2),1) AS clip_avg_score FROM examination_info,exam_record WHERE examination_info.exam_id=exam_record.exam_id AND tag='hard' AND tag='SQL'
报错:
Execution Error SQL_ERROR_INFO: "In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'examination_info.difficulty'; this is incompatible with sql_mode=only_full_group_by"
正确做法
SELECT tag, difficulty, ROUND((SUM(score)-MIN(score)-MAX(score))/(COUNT(score)-2),1) AS clip_avg_score FROM examination_info JOIN exam_record USING(exam_id) WHERE tag='SQL' AND difficulty='hard'
或
SELECT tag, difficulty, ROUND((SUM(score)-MIN(score)-MAX(score))/(COUNT(score)-2),1) AS clip_avg_score FROM examination_info,exam_record WHERE examination_info.exam_id=exam_record.exam_id AND difficulty='hard' AND tag='SQL'
2、统计作答次数
- 自己的想法
SELECT COUNT(*) AS total_pv, SUM((CASE WHEN score IS NULL AND submit_time IS NULL THEN 0 ELSE 1 )) AS complete_pv, COUNT(DISTINCT exam_id) AS complete_exam_cnt FROM exam_record
- 报错
Execution Error SQL_ERROR_INFO: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) AS complete_pv,\n COUNT(DISTINCT exam_id) AS complete_exam_cnt\nFROM exam_re' at line 7"
- 正确做法
SELECT COUNT(*) AS total_pv, SUM((CASE WHEN score IS NULL AND submit_time IS NULL THEN 0 ELSE 1 END )) AS complete_pv, COUNT(DISTINCT exam_id AND score IS NOT NULL AND submit_time IS NOT NULL) AS complete_exam_cnt FROM exam_record
- 原因:CASE ... WHEN ... THEN ... END
- 方法2:使用IF
SELECT COUNT(*) AS total_pv, SUM((CASE WHEN score IS NULL AND submit_time IS NULL THEN 0 ELSE 1 END )) AS complete_pv, COUNT(DISTINCT IF(score IS NOT NULL, exam_id, NULL)) AS complete_exam_cnt FROM exam_record
3、得分不小于平均分的最低分
- 分组的可以在分组内使用join
- 自己的思路
点击查看代码
SELECT
MIN(score) AS min_score_over_avg
FROM exam_record A
JOIN examination_info B
JOIN (SELECT exam_id,AVG(score) AS ex_score
FROM exam_record
GROUP BY exam_id) AVG_E
USING exam_id
WHERE
score<ex_score
AND
tag='SQL'
* 正确答案
点击查看代码
SELECT
MIN(score) AS min_score_over_avg
FROM exam_record er
JOIN examination_info ei
ON er.exam_id=ei.exam_id
WHERE
tag='SQL'
AND score>=
(SELECT AVG(score)
FROM exam_record er
WHERE
tag='SQL'
AND
er.exam_id=ei.exam_id
GROUP BY er.exam_id)
* 方法2:使用over函数☆
# 二、分组函数
## 1、平均**活跃天数**和月活人数
- 自己写的
点击查看代码
SELECT
DATE_FORMAT(submit_time,"%Y%m") AS month,
ROUND(SUM(IF(submit_time IS NOT NULL,1,0))/COUNT(DISTINCT uid),2) AS avg_active_days,
COUNT(DISTINCT uid) AS mau
FROM exam_record
WHERE
submit_time IS NOT NULL
AND
YEAR(submit_time)='2021'
GROUP BY month
* 陷阱在于九月份有个用户同一天做了两种卷子,直接count统计的话活跃天数会多一天,即用户ID和做题日期submit_time要同时去重才能得出正确的活跃天数.
* 正确答案
点击查看代码
SELECT
DATE_FORMAT(submit_time,"%Y%m") AS month,
ROUND(COUNT(DISTINCT uid,DATE_FORMAT(submit_time,"%Y%m%d"))/COUNT(DISTINCT uid),2) AS avg_active_days,
COUNT(DISTINCT uid) AS mau
FROM exam_record
WHERE
submit_time IS NOT NULL
AND
YEAR(submit_time)='2021'
GROUP BY month
## 2、月总刷题数和日均刷题数【拼接未知数据使用UNION】
- 自己写的【错误】:
点击查看代码
SELECT
DATE_FORMAT(submit_time,"%Y%m") AS submit_month,
COUNT(submit_time) AS month_q_cnt,
ROUND(COUNT(submit_time)/(
CASE
WHEN MONTH(submit_time)=1 THEN 31
WHEN MONTH(submit_time)=2 THEN 28
WHEN MONTH(submit_time)=3 THEN 31
WHEN MONTH(submit_time)=4 THEN 30
WHEN MONTH(submit_time)=5 THEN 31
WHEN MONTH(submit_time)=6 THEN 30
WHEN MONTH(submit_time)=7 THEN 31
WHEN MONTH(submit_time)=8 THEN 31
WHEN MONTH(submit_time)=9 THEN 30
WHEN MONTH(submit_time)=10 THEN 31
WHEN MONTH(submit_time)=11 THEN 30
WHEN MONTH(submit_time)=12 THEN 31
END
),3) AS avg_day_q_cnt
FROM practice_record
WHERE
submit_time IS NOT NULL
AND
YEAR(submit_time)=2021
GROUP BY submit_month
ORDER BY submit_month ASC
- 正确答案
点击查看代码
SELECT
DATE_FORMAT(submit_time,"%Y%m") submit_month,
COUNT(submit_time) month_q_cnt,
ROUND(COUNT(submit_time)/MAX(DAY(LAST_DAY(submit_time))),3) avg_day_q_cnt
-- 使用max实现去重
FROM practice_record
WHERE YEAR(submit_time)=2021
GROUP BY submit_month
UNION ALL
SELECT
"2021汇总" submit_month,
COUNT(submit_time) month_q_cnt,
ROUND(COUNT(submit_time)/31,3) avg_day_q_cnt
FROM practice_record
WHERE YEAR(submit_time)=2021
ORDER BY submit_month ASC
3、未完成试卷数大于1的有效用户
点击查看代码
SELECT
uid,
SUM(IF(er.submit_time IS NULL,1,0)) AS incomplete_cnt,
-- COUNT(CASE WHEN er.submit_time IS NULL THEN er.start_time ELSE NULL END) AS incomplete_cnt,
SUM(IF(er.submit_time IS NOT NULL,1,0)) AS complete_cnt,
**GROUP_CONCAT(DISTINCT CONCAT_WS(':',DATE_FORMAT(er.start_time,"%Y-%m-%d"),ei.tag) SEPARATOR ';') **AS detail
FROM exam_record er
LEFT JOIN examination_info ei
ON er.exam_id=ei.exam_id
WHERE YEAR(er.start_time)=2021
GROUP BY er.uid
HAVING
complete_cnt>=1
AND
incomplete_cnt<5
AND
incomplete_cnt>1
ORDER BY incomplete_cnt DESC
【SQL进阶】【CASE/IF、COUNT/SUM、多条记录拼接为一个内容】Day03:聚合分组查询的更多相关文章
- LINQ to SQL 语句(3) 之 Count/Sum/Min/Max/Avg
LINQ to SQL 语句(3) 之 Count/Sum/Min/Max/Avg [1] Count/Sum 讲解 [2] Min 讲解 [3] Max 讲解 [4] Average 和 Agg ...
- oracle通过sql随机取表中的10条记录
oracle通过sql随机取表中的10条记录: SELECT * FROM (SELECT * FROM T_USER ORDER BY DBMS_RANDOM.RANDOM()) WHERE Row ...
- LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg
适用场景:统计数据吧,比如统计一些数据的个数,求和,最小值,最大值,平均数. Count 说明:返回集合中的元素个数,返回INT类型:不延迟.生成SQL语句为:SELECT COUNT(*) FROM ...
- SQL 父子表,显示表中每条记录所在层级
1.sqlserer 中有一张父子关系表,表结构如下: CREATE TABLE [dbo].[testparent]( [ID] [int] IDENTITY(1,1) NOT NULL, [nam ...
- MYSQL实现列拼接,即同一个字段,多条记录拼接成一条
一.首先,新建三张表 DROP TABLE IF EXISTS `article`; CREATE TABLE `article` ( `id` ) unsigned NOT NULL AUTO_IN ...
- 每天努力一点之SQL(二) count sum case when then group by
1. select sum(CASE WHEN A.[STATUS]=0 THEN 1 ELSE 0 end) as a1, sum(CASE A.[STATUS] WHEN 1 THEN 1 EL ...
- SQL —— 获取重复某个字段的第一条记录
----------用来双重排序,且获取唯一 go SELECT ROW_NUMBER() OVER (ORDER BY AScore DESC,ATime ASC) AS Rank, * FROM ...
- 一条sql获取每个类别最新的一条记录
1.初始化数据 create table Products ( id ,), name ), categroy int, addtime datetime , ) insert into Produc ...
- SQL学习笔记:选取第N条记录
Northwind数据库,选取价格第二高的产品. 有两种方法,一个是用Row_Number()函数: SELECT productname FROM ( productname, Row_Number ...
- sql 更新重复数据只取一条记录
select s.* from ( select *, row_number() over (partition by PersonnelAccount order BY Personnel ...
随机推荐
- 使用 Elastic 技术栈构建 K8S 全栈监控 -1:搭建 ElasticSearch 集群环境
文章转载自:https://www.qikqiak.com/post/k8s-monitor-use-elastic-stack-1/ 操作步骤 kubectl create ns elastic k ...
- Filebeat Processors对日志数据应用基本处理和数据增强功能
下面是一个使用drop_fields处理器从Apache访问日志中删除一些字段的示例: filebeat.inputs: - type: log enabled: true fields: apach ...
- 【前端必会】使用indexedDB,降低环境搭建成本
背景 学习前端新框架.新技术.如果需要做一些数据库的操作来增加demo的体验(CURD流程可以让演示的体验根据丝滑) 最开始的时候一个演示程序我们会调用后台,这样其实有一点弊端,就是增加了开发和维护成 ...
- 分布式存储系统之Ceph集群状态获取及ceph配置文件说明
前文我们了解了Ceph的访问接口的启用相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/16727620.html:今天我们来聊一聊获取ceph集群状态和 ...
- C++编程范式(函数)
1 // 2 // main.cpp 3 // test 4 // 5 // Created by Shaojun on 30/5/2020. 6 // Copyright 2020 Shaojun. ...
- strut2 标签加载图表。
//===============================================超市订单量走势图========================================= v ...
- Kafka之概述
Kafka之概述 一.消息队列内部实现原理 (1)点对点模式(一对一,消费者主动拉取数据,消息收到后消息清除) 点对点模型通常是一个基于拉取或者轮询的消息传送模型,这种模型从队列中请求信息,而不是将消 ...
- 齐博x1 APP要实现直播的关键两步
大家务必要注意,缺少这两步,你的APP将不能实现直播, 也即点击直播按钮无法启动直播推流
- 抓包分析 TCP 握手和挥手
前言 首先需要明确的是 TCP 是一个可靠传输协议,它的所有特点最终都是为了这个可靠传输服务.在网上看到过很多文章讲 TCP 连接的三次握手和断开连接的四次挥手,但是都太过于理论,看完感觉总是似懂非懂 ...
- Seata Server 1.5.2 源码学习
Seata 包括 Server端和Client端.Seata中有三种角色:TC.TM.RM,其中,Server端就是TC,TM和RM属Client端.Client端的源码学习上一篇已讲过,详见 < ...