postgresql 刪除重復(fù)數(shù)據(jù)案例詳解
1.建表
/* Navicat Premium Data Transfer Source Server : localhost Source Server Type : PostgreSQL Source Server Version : 110012 Source Host : localhost:5432 Source Catalog : postgres Source Schema : public Target Server Type : PostgreSQL Target Server Version : 110012 File Encoding : 65001 Date: 30/07/2021 10:10:04 */ -- ---------------------------- -- Table structure for test -- ---------------------------- DROP TABLE IF EXISTS "public"."test"; CREATE TABLE "public"."test" ( "id" int4 NOT NULL DEFAULT NULL, "name" varchar(255) COLLATE "pg_catalog"."default" DEFAULT NULL, "age" int4 DEFAULT NULL ) ; -- ---------------------------- -- Records of test -- ---------------------------- INSERT INTO "public"."test" VALUES (1, 'da', 1); INSERT INTO "public"."test" VALUES (2, 'da', 12); INSERT INTO "public"."test" VALUES (3, 'dd', 80); INSERT INTO "public"."test" VALUES (4, 'dd', 80); INSERT INTO "public"."test" VALUES (5, 'd1', 13); -- ---------------------------- -- Primary Key structure for table test -- ---------------------------- ALTER TABLE "public"."test" ADD CONSTRAINT "test_pkey" PRIMARY KEY ("id");
2.根據(jù)名稱獲取重復(fù)
先看看哪些數(shù)據(jù)重復(fù)了
select name ,count(1) from test group by name having count(1)>1
輸出.
name count
da 2
dd 2
3.刪除所有重復(fù)數(shù)據(jù)
注意把要更新的幾列數(shù)據(jù)查詢出來(lái)做為一個(gè)第三方表,然后篩選更新。
delete from test where name in (select t.name from (select name ,count(1) from test group by name having count(1)>1) t)
4.保留一行數(shù)據(jù)
這里展示我們需要保留的數(shù)據(jù):重復(fù)數(shù)據(jù),保留ID最大那一條
SELECT 1. FROM test WHERE id NOT IN ( ( SELECT min( id ) AS id FROM test GROUP BY name ) )
5.刪除數(shù)據(jù)
DELETE FROM test WHERE id NOT IN ( SELECT t.id FROM ( SELECT max( id ) AS id FROM test GROUP BY name ) t )
到此這篇關(guān)于postgresql 刪除重復(fù)數(shù)據(jù)案例詳解的文章就介紹到這了,更多相關(guān)postgresql 刪除重復(fù)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Postgres 創(chuàng)建Role并賦予權(quán)限的操作
這篇文章主要介紹了 Postgres 創(chuàng)建Role并賦予權(quán)限的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01PostgreSQL教程(二十):PL/pgSQL過(guò)程語(yǔ)言
這篇文章主要介紹了PostgreSQL教程(二十):PL/pgSQL過(guò)程語(yǔ)言,本文講解了、PL/pgSQL概述、PL/pgSQL的結(jié)構(gòu)、聲明、基本語(yǔ)句、控制結(jié)構(gòu)等內(nèi)容,需要的朋友可以參考下2015-05-05PostgreSQL使用MySQL外表的步驟詳解(mysql_fdw)
這篇文章主要介紹了PostgreSQL使用MySQL外表的步驟(mysql_fdw),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01postgresql rank() over, dense_rank(), row_number()用法區(qū)別
這篇文章主要介紹了postgresql rank() over, dense_rank(), row_number()的用法區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12PostgreSQL數(shù)據(jù)庫(kù)中窗口函數(shù)的語(yǔ)法與使用
這PostgreSQL中提供了窗口函數(shù),一個(gè)窗口函數(shù)在一系列與當(dāng)前行有某種關(guān)聯(lián)的表行上進(jìn)行一種計(jì)算。下面這篇文章主要給大家介紹了關(guān)于PostgreSQL數(shù)據(jù)庫(kù)中窗口函數(shù)的語(yǔ)法與使用的相關(guān)資料,需要的朋友可以參考下2019-03-03PostgreSQL配置遠(yuǎn)程連接簡(jiǎn)單圖文教程
這篇文章主要給大家介紹了關(guān)于PostgreSQL配置遠(yuǎn)程連接的相關(guān)資料,PostgreSQL是一個(gè)功能非常強(qiáng)大的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)(RDBMS),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12