2

How can I get just the records that have a duplicate combination of columns? For example in the table below I want to get the records that have a duplicate combination of ParentID and Date.

+----+----------+-------------------------+
| ID | ParentID |          Date           |
+----+----------+-------------------------+
|  1 |      225 | 2018-05-10 00:00:00.000 |
|  2 |      225 | 2018-05-10 00:00:00.000 |
|  3 |      300 | 2019-25-11 00:00:00.000 |
+----+----------+-------------------------+ 
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
rohitnair
  • 45
  • 1
  • 6

2 Answers2

2

The inner select gets the parentID and date of the records having duplicates and the outer select gets the cpmplete data including id

SELECT t1.*
FROM your_table t1
INNER JOIN
(
    SELECT parentId, date
    FROM your_table
    GROUP BY parentId, date
    HAVING COUNT(*) > 1
) t2
    ON t1.parentId = t2.parentId AND t1.date = t2.date;
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
juergen d
  • 201,996
  • 37
  • 293
  • 362
1

The MySQL answer to this question was given by @juergen but here is an option for any database supporting analytic functions:

SELECT ID, ParentID, Date
FROM
(
    SELECT ID, ParentID, Date, COUNT(*) OVER (PARTITION BY ParentID, Date) cnt
    FROM yourTable
) t
WHERE t.cnt > 1;

In general, the above approach using analytic functions would probably outperform the traditional join/aggregation approach. But, this assumes that your database supports the above query.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Your answer seems to suggest, that MySQL does not support that. Is that really the case? I thought that MySQL 8.0.2 introduced window functions. – Micha Wiedenmann Jun 04 '18 at 07:23
  • @MichaWiedenmann Yes, it does. But few people are using this version in production (I don't even know if it's available e.g. via RDS). Hence, most MySQL questions sort of assume the version is earlier than 8. – Tim Biegeleisen Jun 04 '18 at 07:25
  • I would be delighted if you could rephrase your answer such that the hasty copy-reader defaults to analytic functions even when using MySQL. IMHO this is the better default case. – Micha Wiedenmann Jun 04 '18 at 07:28