Home Database Mysql Tutorial PostgreSQL删除重复数据

PostgreSQL删除重复数据

Jun 07, 2016 pm 02:58 PM
postgresql delete data repeat

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=# 

 

说明:在表数据量较大的情况下,这种删除方法效率很高。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is it true that you can be blocked and deleted on WeChat and permanently unable to be added? Is it true that you can be blocked and deleted on WeChat and permanently unable to be added? Apr 08, 2024 am 11:41 AM

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.

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Apr 03, 2024 pm 12:04 PM

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

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks Apr 29, 2024 pm 06:55 PM

I cry to death. The world is madly building big models. The data on the Internet is not enough. It is not enough at all. The training model looks like "The Hunger Games", and AI researchers around the world are worrying about how to feed these data voracious eaters. This problem is particularly prominent in multi-modal tasks. At a time when nothing could be done, a start-up team from the Department of Renmin University of China used its own new model to become the first in China to make "model-generated data feed itself" a reality. Moreover, it is a two-pronged approach on the understanding side and the generation side. Both sides can generate high-quality, multi-modal new data and provide data feedback to the model itself. What is a model? Awaker 1.0, a large multi-modal model that just appeared on the Zhongguancun Forum. Who is the team? Sophon engine. Founded by Gao Yizhao, a doctoral student at Renmin University’s Hillhouse School of Artificial Intelligence.

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Apr 01, 2024 pm 07:46 PM

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

How to send files to others on TikTok? How to delete files sent to others? How to send files to others on TikTok? How to delete files sent to others? Mar 22, 2024 am 08:30 AM

On Douyin, users can not only share their life details and talents, but also interact with other users. In this process, sometimes we need to send files to other users, such as pictures, videos, etc. So, how to send files to others on Douyin? 1. How to send files to others on Douyin? 1. Open Douyin and enter the chat interface where you want to send files. 2. Click the "+" sign in the chat interface and select "File". 3. In the file options, you can choose to send pictures, videos, audio and other files. After selecting the file you want to send, click "Send". 4. Wait for the other party to accept your file. Once the other party accepts it, the file will be transferred successfully. 2. How to delete files sent to others on Douyin? 1. Open Douyin and enter the text you sent.

How to completely delete TikTok chat history How to completely delete TikTok chat history May 07, 2024 am 11:14 AM

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.

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

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

How can I retrieve someone else's deleted comment on Xiaohongshu? Will it be displayed if someone else's comment is deleted? How can I retrieve someone else's deleted comment on Xiaohongshu? Will it be displayed if someone else's comment is deleted? Mar 21, 2024 pm 10:46 PM

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

See all articles