Skip to content
Advertisement

Return distinct results that appear more than once

I have the following data:

Result should be:

Note that the result find unique combinations of ID and Site that repeat more than once for a given ID.

I did the following query but does not return the result:

Advertisement

Answer

See: DBFIDDLE

  • The SELECT ID, site FROM table1 GROUP BY ID, site will select the distinct values.
  • Then, using HAVING count(*) > 1, only the IDs that appear more than once are filtered.

P.S. You should try to avoid using DISTINCT and GROUP BY in one query. It makes life so much more complicated when you do that … 😉

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement