PostgreSQL删除重复数据
PostgreSQL删除重复数据 去重的方法一般是找到重复数据中的一条,以某一唯一条件去掉其他重复值。 Oracle 去重的方法很多,常用的是根据 rowid 进行去重。 PostgreSQL 库如何去除单表重复数据呢?可以通过 ctid 进行,下面是实验过程。 一、创建测试表 david
PostgreSQL删除重复数据
去重的方法一般是找到重复数据中的一条,以某一唯一条件去掉其他重复值。
Oracle 去重的方法很多,常用的是根据 rowid 进行去重。
PostgreSQL 库如何去除单表重复数据呢?可以通过 ctid 进行,下面是实验过程。
一、创建测试表
david=# create table emp (
david(# id int,
david(# name varchar);
CREATE TABLE
david=#
二、插入测试数据
david=# insert into emp values (1, 'david');
INSERT 0 1
david=# insert into emp values (1, 'david');
INSERT 0 1
david=# insert into emp values (1, 'david');
INSERT 0 1
david=# insert into emp values (2, 'sandy');
INSERT 0 1
david=# insert into emp values (2, 'sandy');
INSERT 0 1
david=# insert into emp values (3, 'renee');
INSERT 0 1
david=# insert into emp values (4, 'jack');
INSERT 0 1
david=# insert into emp values (5, 'rose');
INSERT 0 1
david=#
三、查询初始化数据
david=# select ctid, * from emp;
ctid | id | name
-------+----+-------
(0,1) | 1 | david
(0,2) | 1 | david
(0,3) | 1 | david
(0,4) | 2 | sandy
(0,5) | 2 | sandy
(0,6) | 3 | renee
(0,7) | 4 | jack
(0,8) | 5 | rose
(8 rows)
david=#
查询重复数据数
david=# select distinct id, count(*) from emp group by id having count(*) > 1;
id | count
----+-------
1 | 3
2 | 2
(2 rows)
david=#
查询出 id 为1的记录有3条,id 为2的记录有2条。
四、查询要保留的数据
以 min(ctid) 或 max(ctid) 为准。
david=# select ctid, * from emp where ctid in (select min(ctid) from emp group by id);
ctid | id | name
-------+----+-------
(0,1) | 1 | david
(0,4) | 2 | sandy
(0,6) | 3 | renee
(0,7) | 4 | jack
(0,8) | 5 | rose
(5 rows)
david=#
五、删除重复数据
david=# delete from emp where ctid not in (select min(ctid) from emp group by id);
DELETE 3
david=#
六、查看最后结果
david=# select ctid, * from emp;
ctid | id | name
-------+----+-------
(0,1) | 1 | david
(0,4) | 2 | sandy
(0,6) | 3 | renee
(0,7) | 4 | jack
(0,8) | 5 | rose
(5 rows)
david=#
说明:如果表中已经有标明唯一的序列主键值,可以把该值替换上述的ctid直接删除。
七、其他方法
也可以使用以下SQL删除重复数据。
david=# delete from emp a
david-# where a.ctid
david-# (
david(# select min(b.ctid) from emp b
david(# where a.id = b.id
david(# );
DELETE 3
david=#
说明:在表数据量较大的情况下,这种删除方法效率很高。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











0.What does this article do? We propose DepthFM: a versatile and fast state-of-the-art generative monocular depth estimation model. In addition to traditional depth estimation tasks, DepthFM also demonstrates state-of-the-art capabilities in downstream tasks such as depth inpainting. DepthFM is efficient and can synthesize depth maps within a few inference steps. Let’s read about this work together ~ 1. Paper information title: DepthFM: FastMonocularDepthEstimationwithFlowMatching Author: MingGui, JohannesS.Fischer, UlrichPrestel, PingchuanMa, Dmytr

1. First of all, it is false to block and delete someone permanently and not add them permanently. If you want to add the other party after you have blocked them and deleted them, you only need the other party's consent. 2. If a user blocks someone, the other party will not be able to send messages to the user, view the user's circle of friends, or make calls with the user. 3. Blocking does not mean deleting the other party from the user's WeChat contact list. 4. If the user deletes the other party from the user's WeChat contact list after blocking them, there is no way to recover after deletion. 5. If the user wants to add the other party as a friend again, the other party needs to agree and add the user again.

The performance of JAX, promoted by Google, has surpassed that of Pytorch and TensorFlow in recent benchmark tests, ranking first in 7 indicators. And the test was not done on the TPU with the best JAX performance. Although among developers, Pytorch is still more popular than Tensorflow. But in the future, perhaps more large models will be trained and run based on the JAX platform. Models Recently, the Keras team benchmarked three backends (TensorFlow, JAX, PyTorch) with the native PyTorch implementation and Keras2 with TensorFlow. First, they select a set of mainstream

Xiaohongshu is a popular social e-commerce platform, and interactive comments between users are an indispensable method of communication on the platform. Occasionally, we may find that our comments have been deleted by others, which can be confusing. 1. How can I retrieve someone else’s deleted comments on Xiaohongshu? When you find that your comments have been deleted, you can first try to directly search for relevant posts or products on the platform to see if you can still find the comment. If the comment is still displayed after being deleted, it may have been deleted by the original post owner. At this time, you can try to contact the original post owner to ask the reason for deleting the comment and request to restore the comment. If a comment has been completely deleted and cannot be found on the original post, the chances of it being reinstated on the platform are relatively slim. You can try other ways

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

The latest video of Tesla's robot Optimus is released, and it can already work in the factory. At normal speed, it sorts batteries (Tesla's 4680 batteries) like this: The official also released what it looks like at 20x speed - on a small "workstation", picking and picking and picking: This time it is released One of the highlights of the video is that Optimus completes this work in the factory, completely autonomously, without human intervention throughout the process. And from the perspective of Optimus, it can also pick up and place the crooked battery, focusing on automatic error correction: Regarding Optimus's hand, NVIDIA scientist Jim Fan gave a high evaluation: Optimus's hand is the world's five-fingered robot. One of the most dexterous. Its hands are not only tactile

1. Open the Douyin app, click [Message] at the bottom of the interface, and click the chat conversation entry that needs to be deleted. 2. Long press any chat record, click [Multiple Select], and check the chat records you want to delete. 3. Click the [Delete] button in the lower right corner and select [Confirm deletion] in the pop-up window to permanently delete these records.

New SOTA for multimodal document understanding capabilities! Alibaba's mPLUG team released the latest open source work mPLUG-DocOwl1.5, which proposed a series of solutions to address the four major challenges of high-resolution image text recognition, general document structure understanding, instruction following, and introduction of external knowledge. Without further ado, let’s look at the effects first. One-click recognition and conversion of charts with complex structures into Markdown format: Charts of different styles are available: More detailed text recognition and positioning can also be easily handled: Detailed explanations of document understanding can also be given: You know, "Document Understanding" is currently An important scenario for the implementation of large language models. There are many products on the market to assist document reading. Some of them mainly use OCR systems for text recognition and cooperate with LLM for text processing.
