当前位置:网站首页>When MySQL uses GROUP BY to group the query, the SELECT query field contains non-grouping fields
When MySQL uses GROUP BY to group the query, the SELECT query field contains non-grouping fields
2022-08-11 07:27:00 【zero round】
When writing SQL today, I found that when GROUP BY is used to group query, the SELECT query field contains non-grouping fields, which will cause the field information to not match. Write this record and introduce a solution.
Description of the problem:
In Nioke SQL33 to find out the students with the lowest GPA in each school, this question has the user_profile table with the following fields and data:
It is required to find the students with the lowest gpa in each school and output in the following way:
Personal thoughts (mistakes)
The first thing that comes to my mind is to use group by to group the data according to the school, and then use the min function to get the students with the lowest gpa in each school, that is, the following code:
SELECT `device_id`,`university`,MIN(`gpa`)FROM `user_profile`GROUP BY `university`ORDER BY `university` ASC;
But I found that it will generate errors at runtime. After the query, it is found that the school and the student have a one-to-many relationship. When the field in the select is neither a grouping field nor an aggregation function, use this queryThe method will aggregate all the information of the same field of multiple students into one piece, and this piece of information will be randomly selected from all the information, as shown below:
This device_id and gpa are the data of each school, randomly select a record of a classmate as the data, and then use the min function to find the classmate with the smallest gpa in each school, although we can get the smallest gpa, but the student with the smallest gpaThe device_id and the device_id in the school after the party may not correspond (here it just queries the minimum gpa of each school according to the school grouping, and uses the minimum gpa as the gpa field in the grouping table, but the device_id in the grouping table isIt will not be replaced according to the minimum gpa, it is still the original device_id), you can see the difference from the following figure:
We can see that although the minimum gpa of Peking University is obtained, the device_id does not correspond.This is because in the case of one-to-many, the selected field uses a non-grouping field (non-aggregation function) to aggregate the data of the field, and only one random field value is retained at the end, resulting in data mismatch.
Solution:
In order to solve this problem, we usually have a way to solve it:
1. Subquery
First find the minimum gpa of each school through a subquery. This subquery only contains the university and MIN(gpa) fields, because the two are grouping fields and aggregation functions, and will not cause data aggregation, so you can get normal resultsthe minimum gpa corresponding to each school in theThe minimum gpa field is now (this can also be achieved through a sliding window)
SELECT a.device_id,a.university,a.gpaFROM user_profile aJOIN (SELECT university,min(gpa) gpaFROM user_profileGROUP BY university) bon a.university=b.university and a.gpa=b.gpaORDER BY university;
But what if there are multiple same minimum gpa in the same school, I have not solved this problem.
Summary:
When using grouping query, the field after select is either a grouping field or an aggregation function
边栏推荐
- 获取拼多多商品信息操作详情
- Douyin share password url API tool
- 实现通用的、高性能排序和快排优化
- 每日sql -用户两天留存率
- HCIP BGP neighbor building, federation, and aggregation experiments
- 什么是Inductive learning和Transductive learning
- Daily sql: request for friend application pass rate
- Daily sql - judgment + aggregation
- Trill keyword search goods - API
- 你是如何做好Unity项目性能优化的
猜你喜欢
My meeting of the OA project (meeting seating & review)
每日sql-员工奖金过滤和回答率排序第一
HCIP BGP neighbor building, federation, and aggregation experiments
导航定位中的坐标系
知识蒸馏Knownledge Distillation
unable to extend table xxx by 1024 in tablespace xxxx
Redis测试
daily sql - query for managers and elections with at least 5 subordinates
【@网络工程师:用好这6款工具,让你的工作效率大翻倍!】
Douyin get douyin share password url API return value description
随机推荐
buu—Re(5)
MySQL导入导出&视图&索引&执行计划
Strongly recommend an easy-to-use API interface
MySQL之CRUD
亚马逊获得AMAZON商品详情 API 返回值说明
求过去半年内连续30天以上每天都有1000元以上成交的商铺
获取拼多多商品信息操作详情
技能在赛题解析:交换机防环路设置
每日sql-统计各个专业人数(包括专业人数为0的)
矩阵分析——Jordan标准形
抖音API接口
HCIP MGRE\OSPF Comprehensive Experiment
OA project meeting notice (query & whether attending & feedback for details)
Waldom Electronics宣布成立顾问委员会
《猪猪1984》NFT 作品集将上线 The Sandbox 市场平台
HCIA experiment
HCIP BGP built adjacent experiment
抖音关键词搜索商品-API工具
Douyin API interface
Taobao sku API interface (PHP example)