当前位置:网站首页>After SQL group query, get the first n records of each group
After SQL group query, get the first n records of each group
2020-11-09 10:51:00 【osc A 3gbrdm】
Catalog
One 、 background
lately , There is a functional requirement in development . The system has an information query module , Ask for information to be presented in card form . Here's the picture :
Show the cards according to the project team , Each project group shows the most read TOP2.
Demand analysis : Group by project group , Then take the top of each group with the most reading 2 strip .
Two 、 Practical analysis
be based on Mysql database
The table definition
1、 The project team :team
id |
Primary key |
name |
Project team name |
2、 Information sheet :info
id |
Primary key |
team_id |
The project team id |
title |
Information name |
pageviews |
Browse volume |
content |
Information content |
info Table data is shown in the figure below :
Let's preview Select Basic knowledge of
Writing order :
select *columns*
from *tables*
where *predicae1*
group by *columns*
having *predicae1*
order by *columns*
limit *start*, *offset*;
Execution order :
from *tables*
where *predicae1*
group by *columns*
having *predicae1*
select *columns*
order by *columns*
limit *start*, *offset*;
count( Field name ) # Returns the total number of records in this field in the table
DISTINCT Field name # Filter duplicate records in the field
First step : First find out the top two readings in the information sheet
info Information table self correlation
SELECT a.*
FROM info a
WHERE (
SELECT count(DISTINCT b.pageviews)
FROM info b
WHERE a.pageviews < b.pageviews AND a.team_id= b.team_id
) < 2 ;
At first glance, it's hard to understand , Here's an example
for instance :
When the amount of reading pageviews a = b = [1,2,3,4]
a.pageviews = 1,b.pageviews It can take [2,3,4],count(DISTINCT b.pageviews) = 3
a.pageviews = 2,b.pageviews It can take [3,4],count(DISTINCT b.pageviews) = 2 # Yes 2 strip , That's the third place
a.pageviews = 3,b.pageviews It can take [4],count(DISTINCT b.pageviews) = 1 # Yes 1 strip , That's the second place
a.pageviews = 4,b.pageviews It can take [],count(DISTINCT b.pageviews) = 0 # Yes 0 strip , That is, the biggest The first name
count(DISTINCT b.pageviews) Represents several values larger than this value
a.team_id= b.team_id Autocorrelation condition , It's about equal to grouping
therefore Top two Equivalent to count(DISTINCT e2.Salary) < 2 , therefore a.pageviews It can be taken as 3、4, Before the assembly 2 high
The second step : Put the watch again team And table info Connect
SELECT a.id, t.NAME, a.team_id, a.pageviews
FROM info a
LEFT JOIN team t ON a.team_id = t.id
WHERE (
SELECT count(DISTINCT b.pageviews)
FROM info b
WHERE a.pageviews < b.pageviews AND a.team_id= b.team_id) < 2
ORDER BY a.team_id, a.pageviews desc
The results are as follows :
There is also a way to understand :
grouping GROUP BY + HAVING, This method can be used to debug the results step by step
SELECT a.id, t.NAME, a.team_id, a.pageviews, COUNT( DISTINCT b.pageviews )
FROM info a
LEFT JOIN info b ON ( a.pageviews < b.pageviews AND a.team_id = b.team_id )
LEFT JOIN team t ON a.team_id = t.id
GROUP BY a.id, t.NAME, a.team_id, a.pageviews
HAVING COUNT( DISTINCT b.pageviews ) < 2
ORDER BY a.team_id, a.pageviews DESC
problem : If the number of readings is the same , It just cracked .
Illustrate with examples :
When the amount of reading pageviews a = b = [1,2,2,4]
a.pageviews = 1,b.pageviews It can take [2,2,4],count(DISTINCT b.pageviews) = 3
a.pageviews = 2,b.pageviews It can take [4],count(DISTINCT b.pageviews) = 1 # Yes 1 strip , That is to say, they are tied for the second place
a.pageviews = 2,b.pageviews It can take [4],count(DISTINCT b.pageviews) = 1 # Yes 1 strip , That's the second place
a.pageviews = 4,b.pageviews It can take [],count(DISTINCT b.pageviews) = 0 # Yes 0 strip , That is, the biggest The first name
count(DISTINCT e2.Salary) < 2 , therefore a.pageviews It can be taken as 2、2、4, Before the assembly 2 high , But there are three pieces of data
3、 ... and 、 summary
Demand transformation : We will find the first few in groups , It's self related , There are several numbers larger than this one
In fact, this is similar to LeetCode The difficulty is hard A database title of
185. All the employees with the top three salaries in the Department
Reference resources :
版权声明
本文为[osc A 3gbrdm]所创,转载请带上原文链接,感谢
边栏推荐
- SQL第二章第三章
- What details does C + + improve on the basis of C
- GitHub 上适合新手的开源项目(Python 篇)
- Analysis of the source code of ThinkPHP facade
- 共创爆款休闲游戏 “2020 Ohayoo游戏开发者沙龙”北京站报名开启
- Several rolling captions based on LabVIEW
- ubuntu 上使用微信的新方案——手机投屏
- From the practice, this paper discusses the problems caused by the inconsistent design of ruby syntax.
- 抢球鞋?预测股市走势?淘宝秒杀?Python表示要啥有啥
- Graph node classification and message passing
猜你喜欢
2. Introduction to computer hardware
[Python from zero to one] 5. Detailed explanation of beautiful soup basic syntax of web crawler
从实践谈 Ruby 语法上的几个设计不一致带来的问题。
使用rem,做到屏幕缩放时,字体大小随之改变
This program cannot be started because msvcp120.dll is missing from your computer. Try to install the program to fix the problem
使用CopyMemory API出现 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
LTM understanding and configuration notes
Android权限大全
彩虹排序 | 荷兰旗问题
Recommended tools for Mac
随机推荐
Git delete IML file
A solution to the problem that color picker (palette) cannot use shortcut keys in sublime Text3 plug-in
结合阿里云 FC 谈谈我对 FaaS 的理解
2020,Android开发者打破寒冬的利器是什么?
程序员的十年之痒
SQL第二章第三章
ThinkPHP门面源码解析
The whole process of building a fully distributed cluster
For the first time open CSDN, this article is for the past self and what is happening to you
商品管理系统——SPU检索功能
libssl对CentOS登录的影响
Recommendation system, in-depth paper analysis gbdt + LR
基于LabVIEW实现的几种滚动字幕
Oschina plays disorderly on Monday
Mac 必备优质工具推荐
基于synchronized锁的深度解析
Stack & queue (go) of data structure and algorithm series
nodejs学习笔记(慕课网nodejs从零开发web Server博客项目)
手写数字图片识别-卷积神经网络
ThinkPHP框架执行流程源码解析