SoFunction
Updated on 2025-04-08

How to query postgresql for duplicate count and deduplication query

Postgresql query duplicate count and deduplication query

1. Repeat counting

SELECT id, name, email,
count(*) over (partition by email) cnt
FROM people

2. Counting and sorting of fields with the same value

SELECT id, name, email,
row_number() over (PARTITION BY email ORDER BY id DESC) AS row_num
FROM people

3. Re-duplicate query

3.1、

select distinct on (a) a,b,c from t_distinct;

Returns a unique arbitrary row

3.2、

select distinct a,b,c from t_distinct;

Returns a,b,c unique values.

Postgresql counts duplicate values ​​in fields

1. Repeat counting

SELECT Number,property,count(*) over (PARTITION BY Number) AS “row_num”
from (select Number,‘Need to change the terminal user' as attribute from "Market Department 0330 needs to be replaced with terminal user details" union
 SELECT number, ‘Card slot needs to be replaced’ as property FROM “Marketing Department0330Need to upgrade the terminal version to openVoLTEReplace the switch slot”) as ss

2. Counting and sorting of fields with the same value

SELECT Number,property,row_number() over (PARTITION BY Number ORDER BY Number DESC) AS “row_num”
from (select Number,‘Need to change the terminal user' as attribute from "Market Department 0330 needs to be replaced with terminal user details" union
 SELECT number, ‘Card slot needs to be replaced’ as property FROM “Marketing Department0330Need to upgrade the terminal version to openVoLTEReplace the switch slot”) as ss

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.