sql聚合分组查询-聚合函数
文章目录
- 知识
- 题目1
- 常规解法1:
- 解法2:
- 解法3:
- 解法4:
- 解法5(利用专用窗口函数:rank、dense_rank、row_number):
- 题目2
- 解1:用if条件函数
- 解2:在聚合函数中添加条件语句
- 解3:用case when...then...end
- 题目3:
- 解1:聚合函数求出平均值,作为表中的一个筛选条件
- 解2:解1的另一种写法
- 解3:用窗口函数直接求出avg,作为一列。然后score与avg比较
- 解4:解3的另写(在部分字段前加上了表名)
- 解5:错误写法:报错"Table 't' doesn't exist"
- 题目4:
- 解1:
- 解2:子查询
- 解3:基于解2
知识
聚合函数:max、min、count、avg、sum
分组函数:group by
- 聚合函数会自动忽略值为null的行
- 聚合函数只能直接加到
SELECT, HAVING, GROUP BY
后面
题目1
https://www.nowcoder.com/practice/a690f76a718242fd80757115d305be45?tpId=240&tqId=2180959&ru=/ta/sql-advanced&qru=/ta/sql-advanced/question-ranking
常规解法1:
select tag, difficulty, round( (sum(score)-max(score)-min(score)) / (count(score)-2) ,1) as clip_avg_score
from examination_info as t1inner joinexam_record as t2on t1.exam_id=t2.exam_id
wheretag="SQL" and difficulty="hard";
解法2:
select tag, difficulty, round( (sum(score)-max(score)-min(score)) / (count(score)-2) ,1) as clip_avg_score
from examination_infojoin # 默认是inner joinexam_record using(exam_id)
wheretag="SQL" and difficulty="hard";
注意:
using
只能在相同字段名的判等使用。
解法3:
select tag, difficulty, round(avg(score), 1) as clip_avg_score
from exam_record as t1left join # inner join 也对 examination_info as t2 using(exam_id)
wheretag="SQL" and difficulty="hard"and score not in (select max(score) from exam_record)and score not in (select min(score) from exam_record);
注意:
- 例如最后的
score not in (select max(score) from exam_record)
不能写为score is not max(score)
是因为聚合函数只能直接加到SELECT, HAVING, GROUP BY
后面,因此要写一个子查询,先把最大、最小的score查找出来。 - 例如最后的
score not in (select max(score) from exam_record)
的表名只能是原表名,不能是t1。否则报错:“Table ‘t1’ doesn’t exist”。
解法4:
select tag, difficulty, round(avg(score), 1) as clip_avg_score
from exam_record as t1left join # inner join 也对 examination_info as t2 using(exam_id)
wheretag="SQL" and difficulty="hard"and score < (select max(score) from exam_record) and score > (select min(score) from exam_record)
解法5(利用专用窗口函数:rank、dense_rank、row_number):
select tag, difficulty, round(avg(score), 1) as clip_avg_score
from(select-- exam_record.id ,exam_id, tag, difficulty, score, exam_id, tag, difficulty, score, # 这儿的注意点见下面dense_rank() over(order by score asc) as score_asc_ranking,dense_rank() over(order by score desc) as score_desc_rankingfrom exam_record left join examination_info using(exam_id)where tag="SQL" and difficulty="hard" and score is not null) AS t
where t.score_asc_ranking!=1 and t.score_desc_ranking!=1
group bytag;
注意:
exam_id, tag, difficulty, score,
1.若写为 *
,会报错:“Duplicate column name ‘id’”,因为id列是二表都有的,而虽然exam_id也是二表都有的,但它是连结字段,因此可以直接写exam_id。
2.若写为 id ,exam_id, tag, difficulty, score,
,会报错:“Column ‘id’ in field list is ambiguous”,跟上面1.
的报错原理其实一样。
3.若写为exam_record.id ,exam_id, tag, difficulty, score,
,即将id指定表->exam_record.id,就OK。
题目2
https://www.nowcoder.com/practice/45a87639110841b6950ef6a12d20175f?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0
解1:用if条件函数
SELECTcount(start_time) as total_pv,count(submit_time) as complete_pv,count(distinct if(submit_time is not null, exam_id, NULL)) as complete_exam_cnt
FROMexam_record;
解2:在聚合函数中添加条件语句
SELECTcount(start_time) as total_pv,count(submit_time) as complete_pv,count(DISTINCT exam_id and score IS NOT NULL) as complete_exam_cnt,
FROMexam_record;
要注意count(DISTINCT exam_id and score IS NOT NULL)
,聚合函数中也可以添加条件语句.
解3:用case when…then…end
SELECTcount(start_time) as total_pv,count(submit_time) as complete_pv,
# count(distinct case when submit_time is not null then exam_id else null end ) as complete_exam_cntcount(distinct case when score is not null then exam_id else null end ) as complete_exam_cnt
FROMexam_record;
题目3:
https://www.nowcoder.com/practice/3de23f1204694e74b7deef08922805b2?tpId=240&tags=&title=&difficulty=0&judgeStatus=0&rp=0
解1:聚合函数求出平均值,作为表中的一个筛选条件
select min(score) as min_score_over_avg
FROM exam_record as erjoinexamination_info as eion er.exam_id=ei.exam_id
where tag="SQL" andscore>=(select avg(score) from exam_record er join examination_info ei on er.exam_id=ei.exam_idwhere tag='SQL')
解2:解1的另一种写法
select min(score) as min_score_over_avg
FROM exam_record joinexamination_info using(exam_id)
where tag="SQL" andscore>=(select avg(score) from exam_record er join examination_info ei on er.exam_id=ei.exam_idwhere tag='SQL')
解3:用窗口函数直接求出avg,作为一列。然后score与avg比较
selectmin(t.score) as min_score_over_avg
from(selectscore,avg(score) over() as avg_score from
# exam_record
# inner join # left
# examination_info
# using(exam_id)exam_record as erleft joinexamination_info as eion er.exam_id=ei.exam_id wheretag="SQL" andscore is not null) as t
where t.score>=t.avg_score;
解4:解3的另写(在部分字段前加上了表名)
selectmin(t.score) as min_score_over_avg
from(selecter.score,avg(er.score) over() as avg_score from exam_record as erleft joinexamination_info as eion er.exam_id=ei.exam_idwhereei.tag="SQL" ander.score is not null) as t
where t.score>=t.avg_score;
解5:错误写法:报错"Table ‘t’ doesn’t exist"
目前咱不知道为啥报错。。。
select min(t.score) as min_score_over_avg
FROM (select scorefrom exam_record inner join # inner/left join examination_info using(exam_id) where tag="SQL" and score is not null) as t
where t.score>=(select avg(t.score) from t);
题目4:
https://www.nowcoder.com/practice/9e2fb674b58b4f60ac765b7a37dde1b9?tpId=240&tqId=2183005&ru=/practice/3de23f1204694e74b7deef08922805b2&qru=/ta/sql-advanced/question-ranking
解1:
SELECTDATE_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
FROMexam_record
WHEREsubmit_time is not NULLand year(submit_time)=2021
GROUP BYDATE_FORMAT(submit_time, "%Y%m")
# month; 也ok
解2:子查询
selectSUBSTR(ymd, 1, 6) as month,round(count(1) / count(distinct uid), 2) as avg_active_days,count(distinct uid) as mau
FROM(SELECT DISTINCT uid, DATE_FORMAT(submit_time, "%Y%m%d") as ymdfrom exam_recordwhere submit_time is not NULL and YEAR(submit_time)=2021 # "2021"也OK) as t
GROUP BYSUBSTR(ymd, 1, 6);
# month; 也ok
注意:
- count(1)就相当于count(*),对总行数计数,其效率更高。
- FROM(…) as t,必须有as t,否则报错: ‘Every derived table must have its own alias’–“每个派生表必须有自己的别名”。
解3:基于解2
selectt.Ym as month,round(count(*) / count(distinct t.uid), 2) as avg_active_days,count(distinct t.uid) as mau
FROM(SELECT DISTINCT uid, DATE_FORMAT(submit_time, "%Y%m") as Ym,DATE_FORMAT(submit_time, "%Y%m%d") as ymdfrom exam_recordwhere submit_time is not NULL and YEAR(submit_time)=2021 # "2021"也OK) as t
GROUP BYt.Ym;
或:
selectYm as month,round(count(*) / count(distinct uid), 2) as avg_active_days,count(distinct uid) as mau
FROM(SELECT DISTINCT uid, DATE_FORMAT(submit_time, "%Y%m") as Ym,DATE_FORMAT(submit_time, "%Y%m%d") as ymdfrom exam_recordwhere submit_time is not NULL and YEAR(submit_time)=2021 # "2021"也OK) as t
GROUP BYYm;
sql聚合分组查询-聚合函数相关推荐
- Python之Pandas:利用Pandas函数实现对表格文件的查之高级查询(类似sql,分组查询等)之详细攻略
Python之Pandas:利用Pandas函数实现对表格文件的查之高级查询(类似sql,分组查询等)之详细攻略 目录 利用Pandas函数实现对表格文件的查之高级查询(类似sql) 1.分组查询
- 《天池龙珠 - SQL训练营》02.SQL基础:查询与排序-select、运算符、聚合分组查询等
本笔记为阿里云天池龙珠计划SQL训练营的学习内容,链接为:https://tianchi.aliyun.com/specials/promotion/aicampsql 目录 一.SELECT语句基础 ...
- day056-58 django多表增加和查询基于对象和基于双下划线的多表查询聚合 分组查询 自定义标签过滤器 外部调用django环境 事务和锁...
一.多表的创建 from django.db import models# Create your models here. class Author(models.Model):id = model ...
- Django聚合分组查询(F与Q查询|ORM查询优化|常见字段参数)
文章目录 一.正反向查询进阶操作 二.聚合查询 三.分组查询 四.ORM再次添加字段 五.F与Q查询 五.ORM查询优化 六.事务操作 七.模型层常见字段 八.ORM常见字段参数 九.多对多三种创建方 ...
- day67 ORM模型之高阶用法整理,聚合,分组查询以及F和Q用法,附练习题整理
归纳总结的笔记: day67ORM特殊的语法一个简单的语法 --翻译成--> SQL语句语法:1. 操作数据库表 创建表.删除表.修改表2. 操作数据库行 增.删.改.查怎么连数据库:需要手动创 ...
- sql server——分组查询
sql server里分组查询通常用于配合聚合函数,达到分类汇总统计的信息.而其分类汇总的本质实际上就是先将信息排序,排序后相同类别的信息会聚在一起,然后通过需求进行统计计算. 使用Group BY ...
- SQL Server分组查询
分组查询 1.分组查询语句 分组查询语句中,只能是分组列和聚合函数 2.分组查询语句中,如果不是分组列也不是聚合函数则会报以下错误 3.group by子句可以对某列或者多列进行分组查询 4.orde ...
- SQL数据库——分组查询GROUP BY
一.分组查询GROUP BY 1.语法 SELECT (列名) as '自由命名' , COUNT(*) as '自由命名'; from (表名); GROUP BY (列名); 2. * 号里可以为 ...
- sql语句分组查询最新的一次记录
目录 1 需求 2 xml里面的代码 1 需求 A为任务表,B为日志表.一个任务可能有多个日志,一次任务调度就是一个日志: 现在想要拿到最新一次的调度:也就是单个任务的最新一次调度的信息: 2 xml ...
最新文章
- diy直立双足机器人_速看!近期国内外机器人资讯大汇总
- 人工智能产业展望:2018年三大难题如何破解?
- Office 2003出现发送错误报告怎么办
- httphandlers 与 httpmodules
- wordpress建立php站点地图,wordpress自动网站地图
- 从Var Tick角度来对CE电源管理
- linux c语言内核函数,2014-1-5_linux内核学习(1)_C语言基础
- (一)flask-sqlalchemy的安装和配置
- Python+OpenCV:色彩空间转换
- 计算机系统操作工中级工试卷,计算机系统操作工中级理论试题及答案.doc
- python流水灯程序_单片机流水灯汇编语言源代码大全(六款流水灯汇编语言源代码)...
- AD选择板边覆铜教程
- 给站长推荐几本IT书
- 快速入门Maxwell基本操作流程(2D部分)
- 基于SVM,KNN,CNN的数字图像识别
- C# NPOI Excel 合并单元格和取消单元格
- 通过Vue解决跨域问题(proxy配置代理)
- wifi+遥控器+android,基于安卓手机wifi的智能遥控器设计
- Android麦克风探测器
- 不受更新影响的微信PC客户端N开补丁