gpt4 book ai didi

sql - 删除sql中的重复行

转载 作者:行者123 更新时间:2023-11-29 13:03:46 25 4
gpt4 key购买 nike

我想删除 sql 中的重复行。我的 table 看起来像这样:

CREATE TABLE test_table 
(
id Serial,
Date Date,
Time Time,
Open double precision,
High double precision,
Low double precision
);


DELETE FROM test_table
WHERE ctid IN (SELECT min(ctid)
FROM test_table
GROUP BY id
HAVING count(*) > 1);

使用下面的 delete 语句,我在 secret 列 ctid 中搜索重复的条目并删除它们。但是,这不能正常工作。查询得到正确执行,但不会删除任何内容。

非常感谢您的回答!

更新

这是一些示例数据(没有生成的 id):

2013.11.07,12:43,1.35162,1.35162,1.35143,1.35144
2013.11.07,12:43,1.35162,1.35162,1.35143,1.35144
2013.11.07,12:44,1.35144,1.35144,1.35141,1.35142
2013.11.07,12:45,1.35143,1.35152,1.35143,1.35151
2013.11.07,12:46,1.35151,1.35152,1.35149,1.35152

最佳答案

改掉使用 ctidxid 等的习惯 - 它们没有被宣传是有原因的。

一次处理重复行的一种方法,具体取决于您的 postgres 版本的最新程度:

with unique_rows
as
(
select distinct on (id) *
from test_table
),
delete_rows
as
(
delete
from test_table
)
insert into test_table
select *
from unique_rows
;

或者将所有内容分解为三个步骤并使用临时表:

create temp table unique_rows
as
select distinct on (id) *
from test_table
;

create temp table delete_rows
as
delete
from test_table
;

insert into test_table
select *
from unique_rows
;

关于sql - 删除sql中的重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21194180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com