Quantcast
Viewing all articles
Browse latest Browse all 3

Answer by Rodrigo Oliveira for How to fix SELECT statement to return duplicates

What you need is a subquery to first find the duplicated track titles in track table, then join this to the other tables. This subquery would look like:

  SELECT rcdid, COUNT(*) AS number  FROM track  GROUP BY rcdid  HAVING COUNT(*) > 1

Now, depending on what database engine you're using, you may be able to use a CTE which would make it more readable. If that is your case, you could try:

WITH CTE_DuplicatedTracks AS (  SELECT rcdid, COUNT(*) AS number  FROM track  GROUP BY rcdid  HAVING COUNT(*) > 1)SELECT  c.comptitle,  t.trknum,  cd.cdtitleFROM  CTE_DuplicatedTracks dt  JOIN track t ON    dt.rcdid = t.rcdid  JOIN recording r ON    t.rcdid = r.rcdid  JOIN composition c ON    r.compid = c.compid  JOIN cd    ON cd.cdid = t.cdid;

If your engine does not support CTEs:

SELECT  c.comptitle,  t.trknum,  cd.cdtitleFROM  (    SELECT rcdid, COUNT(*) as number    FROM track    GROUP BY rcdid    HAVING COUNT(*) > 1  ) dt  JOIN track t ON    dt.rcdid = t.rcdid  JOIN recording r ON    t.rcdid = r.rcdid  JOIN composition c ON    r.compid = c.compid  JOIN cd    ON cd.cdid = t.cdid;

Viewing all articles
Browse latest Browse all 3

Trending Articles