Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to list rows with duplicate columns

I have a table with the fields id, name, birthday, clinic:

id | name | birthday   | clinic
 1 | mary | 2020-01-01 | clin 1
 2 | mary | 2020-01-01 | clin 1
 3 | mary | 2020-01-01 | clin 2
 4 | john | 2021-01-01 | clin 1
 5 | pete | 2020-01-05 | clin 1
 6 | pete | 2020-01-05 | clin 2
 7 | pete | 2020-01-05 | clin 3

I want to get all records with name, birthday duplicate like:

id | name | birthday   | clinic
 1 | mary | 2020-01-01 | clin 1
 2 | mary | 2020-01-01 | clin 1
 3 | mary | 2020-01-01 | clin 2
 5 | pete | 2020-01-05 | clin 1
 6 | pete | 2020-01-05 | clin 2
 7 | pete | 2020-01-05 | clin 3

Mary and Pete have more than one record with same name and birthday

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using COUNT() as an analytical function, we can try:

WITH cte AS (
    SELECT *, COUNT(*) OVER (PARTITION BY name, birthday) cnt
    FROM yourTable
)

SELECT id, name, birthday, clinic
FROM cte
WHERE cnt > 1;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading