当前位置:网站首页>SQL statement. After grouping, the group is sorted according to the datetime field, the latest record in the group is retained, and other items are deleted. (used to eliminate duplicate data within th
SQL statement. After grouping, the group is sorted according to the datetime field, the latest record in the group is retained, and other items are deleted. (used to eliminate duplicate data within th
2022-07-22 13:10:00 【King in the world TJM】
One 、 background :
surface pipeline_data, There is a self increasing primary key line_id, Due to multiple imports .mdb file , Lead to exp_no Duplicate data exists , Business needs a exp_no Keep only one piece of the latest data , namely updatetime The field is up to date . If the same exp_no There are three records , And updatetime All the same , Keep only one record , Delete the other two extra items .
Two 、 The general idea :
First use exp_no grouping , Only updatetime The latest one , Delete other items ; If the latest in the Group updatetime There are multiple , Just keep one item .
3、 ... and 、 Solution :
1. What is used locally is mysql5.6 or mysql5.7.
2. Online mysql8.0 The corresponding method of ,sql The coding difference is quite large , Mainly introduced row_number() Method , Convenient grouping and sorting within groups 、 Filtration, etc. . The reference codes are as follows :( I haven't tried this )
SELECT
a.Classid,
a.English
FROM
(
SELECT
Classid,
English,
row_number () over (
PARTITION BY Classid
ORDER BY
English DESC
) AS n
FROM
CJ
) a
WHERE
n <= 2
Official recommendations ,
row_number() from 1 Start , Return a number for each grouped record ,
row_number() OVER (PARTITION BY COL1 ORDER BY COL2) Express basis COL1 grouping , Within the group according to COL2 Sort , The value calculated by this function represents the sequence number of each group after internal sorting ( The only continuous ).
3. The following is the method practice process of this paper , Including the core sql sentence .
It is known that :line_id It's an auto primary key , Data may be inserted many times , The same exp_no It may correspond to multiple records ;
requirement : One exp_no Keep only one record , And is updatetime The latest record , The rest should be deleted .
The core SQL The statement is as follows ,
DELETE
FROM
pipeline_data
WHERE
line_id IN (
SELECT
y.line_id AS y_id
FROM
(
SELECT
w.line_id AS line_id
FROM
pipeline_data w
WHERE
w.exp_no IN (
SELECT
p.exp_no AS exp_no
FROM
(
SELECT
z.exp_no AS exp_no
FROM
(
SELECT
k.exp_no AS exp_no
FROM
pipeline_data k
GROUP BY
k.exp_no
HAVING
COUNT(1) > 1
) z
) p
)
) y
)
AND line_id NOT IN (
SELECT
j.line_id AS j_id
FROM
(
SELECT
e.line_id AS line_id
FROM
(
SELECT
u.line_id AS line_id
FROM
(
SELECT
g.line_id AS line_id,
g.exp_no AS g_exp_no,
MAX(g.updatetime) AS max_g_updatetime
FROM
pipeline_data g
GROUP BY
g_exp_no
) u
) e
) j
)
Four 、 Error scenario analysis :
1. The nested query of the same table reports an error :[Err] 1248 - Every derived table must have its own alias
Cause analysis : The results of the sub query should be queried for the second time , The subquery must be aliased , You can query , That is, put the result set of the sub query into the temporary table .
2. Add, delete, modify and report errors after querying the same table :[Err] 1093 - You can't specify target table 'pipeline_data' for update in FROM clause
Cause analysis :
[Err] 1093 - Can't be in FROM Clause specifies the target table "pipeline_data" updated .
Cause analysis : If in the addition, deletion and modification statement , Nested the same table in the form of sub query to add, delete and modify , Then the result set of this sub query should be aliased first , That is, it is saved in the temporary table , Then add, delete and modify the outer layer , To execute . The outer operation is actually the first select Add, delete and modify in the temporary table of the result set of .( otherwise , There is a sense of internal and external dead circulation , Son select Is the condition of the parent operation , Execute child first select, The table has been locked pipeline_data, Cannot perform parent operation ;java Inside for There are similar scenarios in the loop , The same list It must be traversed , You have to modify yourself in the loop body , You're going to report a mistake )
边栏推荐
- 每日一题-LeetCode814-二叉树剪枝-递归
- Differential privacy budget optimization method based on deep learning in Internet of things environment
- Merge two linked lists
- How to pre train multimodal models using multi type data?
- 安科瑞智能小型断路器改如何选型?
- Matlab digital image processing homework: facial expression recognition
- Eventlet collaboration
- How does am5se-is anti islanding protection device solve the impact in the process of distributed photovoltaic power generation?
- 华为无线设备配置WIDS和WIPS
- MD5 & MD5 salt value encryption
猜你喜欢
「运维有小邓」通过审计用户活动和AD域内部变化来确保GDPR合规
每日一题-LeetCode814-二叉树剪枝-递归
el-pinut number取消数字操作符号
Wechat hotel reservation applet graduation project (4) opening report
AM5SE-IS防孤岛保护装置如何解决分布式光伏发电过程中的影响?
《倍增商业成功宝典》全新升级上线!炙夏新品,久等终至!
数据类型与变量
4种 Redis 集群方案及优缺点对比
Configure SSL certificate for Baidu cloud virtual machine BCH
SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?
随机推荐
SeekTiger的Okaleido有大动作,生态通证STI会借此爆发?
Selenium starts chrome, and the page shows and hides events
ECCV 2022 | 形状、纹理和颜色在视觉识别中的作用
: style usage in projects
Several ways of employee motivation
The four trends and three key points of the data platform construction mode are summarized
三种JS截取字符串方法
Research on Wi Fi indoor location technology based on Knowledge Map
Four redis cluster schemes and their advantages and disadvantages
China Economic Net: "Yuan universe" is hot
How to pre train multimodal models using multi type data?
百度飞桨EasyDL X 韦士肯:看轴承质检如何装上“AI之眼”
Ioinputstream type
IOInputStream类型
《六》BFC
el-pinut number取消数字操作符号
力扣解法汇总814-二叉树剪枝
为什么RISC-V架构前进势头铆足
Concept, architecture and key technologies of industrial Internet
New energy_ Photovoltaic inverter