当前位置:网站首页>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
边栏推荐
- Unity程序员如何提升自己的能力
- Douyin API interface
- 损失函数——交叉熵
- Especially the redis
- 常见激活函数及其导数
- sql--Users who have purchased more than 3 times (inclusive) within 7 days (including the current day), and the purchase amount in the past 7 days exceeds 1,000
- 每日sql -用户两天留存率
- Daily SQL - find each of the students school gpa minimum (window)
- 详解BLEU的原理和计算
- redis + lua实现分布式接口限流实现方案
猜你喜欢

ROS 服务通信理论模型

Daily sql-statistics of the number of professionals (including the number of professionals is 0)

mysql视图与索引

Taobao API interface reference

常见激活函数及其导数

1688 product interface

JD.com product details API call example explanation

Redis源码:Redis源码怎么查看、Redis源码查看顺序、Redis外部数据结构到Redis内部数据结构查看源码顺序

My meeting of the OA project (meeting seating & review)

淘宝API接口参考
随机推荐
一个小时快速熟悉MySQL基本用法
矩阵分析——Jordan标准形
京东商品详情API调用实例讲解
拼多多API接口大全
Douyin API interface
Open Set Domain Adaptation 开集领域适应
Trill keyword search goods - API
Daily sql-seek the sum of successful investments in 2016
Taobao API interface reference
李沐d2l(十)--卷积层Ⅰ
矩阵分析——微分、积分、极限
技术分享 | 实战演练接口自动化如何处理 Form 请求?
Attitude solution - gyroscope + Euler method
My meeting of the OA project (meeting seating & review)
concept noun
知识蒸馏Knownledge Distillation
HCIP WPN experiment
Amazon Get AMAZON Product Details API Return Value Description
MySQL导入导出&视图&索引&执行计划
MySQL 版本升级心得