如何删除符合 SQL 中某些条件的一条记录? (内特扎)

发布于 2024-12-12 08:07:14 字数 483 浏览 0 评论 0原文

我的表中有一些重复的记录,因为事实证明 Netezza 不支持对主键进行约束检查。话虽这么说,我有一些信息完全相同的记录,我只想删除其中一条。我已经尝试过

delete  from table_name where test_id=2025 limit 1

这样做,但是

delete  from table_name where test_id=2025 rowsetlimit 1

这两种选择都不起作用。我收到一条错误消息“

found 'limit'. Expecting a keyword

是否有任何方法可以限制此查询删除的记录?”我知道我可以删除记录并重新插入它,但这有点乏味,因为我必须多次执行此操作。

请注意,这不是 SQL Server 或 MySQL。这是用于 Netezza

I've got some duplicate records in a table because as it turns out Netezza does not support constraint checks on primary keys. That being said, I have some records where the information is the exact same and I want to delete just ONE of them. I've tried doing

delete  from table_name where test_id=2025 limit 1

and also

delete  from table_name where test_id=2025 rowsetlimit 1

However neither option works. I get an error saying

found 'limit'. Expecting a keyword

Is there any way to limit the records deleted by this query? I know I could just delete the record and reinsert it but that is a little tedious since I will have to do this multiple times.

Please note that this is not SQL Server or MySQL.This is for Netezza

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

天邊彩虹 2024-12-19 08:07:14

如果它不支持“DELETE TOP 1”或“LIMIT”关键字,您最终可能必须执行以下操作之一:

1)添加某种自动递增列(如 ID),使每一行独特的。不过,我不知道创建表后是否可以在 Netezza 中执行此操作。

2)使用某种编程语言以编程方式读取整个表,以编程方式消除重复项,然后删除所有行并再次插入它们。如果它们被其他表引用,则这可能是不可能的,在这种情况下,您可能必须暂时删除约束。

我希望这有帮助。请告诉我们。

并供将来参考;这就是为什么我个人总是创建一个自动递增的 ID 字段,即使我认为我永远不会使用它。 :)

If it doesn't support either "DELETE TOP 1" or the "LIMIT" keyword, you may end up having to do one of the following:

1) add some sort of an auto-incrementing column (like IDs), making each row unique. I don't know if you can do that in Netezza after the table has been created, though.

2) Programmatically read the entire table with some programming language, eliminate duplicates programmatically, then deleting all the rows and inserting them again. This might not be possible if they are references by other tables, in which case, you might have to temporarily remove the constraint.

I hope that helps. Please let us know.

And for future reference; this is why I personally always create an auto-incrementing ID field, even if I don't think I'll ever use it. :)

玩物 2024-12-19 08:07:14

以下查询适用于从表中删除重复项。

DELETE FROM YOURTABLE 
WHERE COLNAME1='XYZ' AND 
(
COLNAME1,
ROWID
)   
NOT IN
(
SELECT COLNAME1,
       MAX(ROWID)
FROM YOURTABLENAME
WHERE COLNAME = 'XYZ'
GROUP BY COLNAME1
)

The below query works for deleting duplicates from a table.

DELETE FROM YOURTABLE 
WHERE COLNAME1='XYZ' AND 
(
COLNAME1,
ROWID
)   
NOT IN
(
SELECT COLNAME1,
       MAX(ROWID)
FROM YOURTABLENAME
WHERE COLNAME = 'XYZ'
GROUP BY COLNAME1
)
魄砕の薆 2024-12-19 08:07:14

如果记录相同,那么您可以执行类似的操作

CREATE TABLE DUPES as
SELECT col11,col2,col3,col....... coln from source_table where test_id = 2025
group by 
1,2,3..... n

DELETE FROM source_table where test_id = 2025

INSERT INTO Source_table select * from duoes

DROP TABLE DUPES

您甚至可以创建一个子查询来选择所有具有 COUNT(*) > 的 test_ids 1 自动查找第 1 步和第 3 步中的受骗者

If the records are identical then you could do something like

CREATE TABLE DUPES as
SELECT col11,col2,col3,col....... coln from source_table where test_id = 2025
group by 
1,2,3..... n

DELETE FROM source_table where test_id = 2025

INSERT INTO Source_table select * from duoes

DROP TABLE DUPES

You could even create a sub-query to select all the test_ids HAVING COUNT(*) > 1 to automatically find the dupes in steps 1 and 3

埋情葬爱 2024-12-19 08:07:14
  -- remove duplicates from the <<TableName>> table
  delete from <<TableName>> 
  where rowid not in 
  (
    select min(rowid) from <<TableName>>
    group by (col1,col2,col3) 
  );
  -- remove duplicates from the <<TableName>> table
  delete from <<TableName>> 
  where rowid not in 
  (
    select min(rowid) from <<TableName>>
    group by (col1,col2,col3) 
  );
情话墙 2024-12-19 08:07:14

GROUP BY 1,2,3,....,n 将消除插入临时表时的重复

The GROUP BY 1,2,3,....,n will eliminate the dupes on the insert to the temp table

思慕 2024-12-19 08:07:14

Netezza 中是否允许使用 rowid...据我所知,我认为该查询不会在 Netezza 中执行...

Does the use rowid is allowed in Netezza...As far as my knowledge is concern i don't think this query will executed in Netezza...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文