0

I am trying to identify duplicate records between two tables. The duplicate records will have the same playlist name and track id, but will have different playlist ids. I just need to identify the second of the duplicate record. So there is a playlist named Music with a track ID of 1. This is part of playlist ID 1. There is another playlist named Music that also has a track ID of 1, but this attached playlist ID 8. I just want to identify the Music, 1, 8 record.

I am able to identify the duplicate records, but am unsure how to pull in the playlist name. I think I might need to use the EXISTS clause and a MAX aggregate function, but unsure and don't know where to start. My current code is below.

SELECT P.Name AS [Playlist Name] ,PT.TrackId AS [Track ID] FROM PlaylistTrack AS [PT] FULL JOIN Playlist AS [P] ON PT.PlaylistId = P.PlaylistId GROUP BY P.Name, PT.TrackId HAVING COUNT(CONCAT(P.Name,PT.TrackId)) > 1 ORDER BY P.Name 

I am trying to get something that looks like the results below. So for example, Audiobooks with a track ID of NULL appears previously but has a different Playlist ID. The results should just capture the next playlist name/track ID (i.e. Audiobooks/NULL) match that has a different Playlist ID (i.e. 8).

Playlist Name Playlist ID Track ID Audiobooks 6 NULL Movies 7 NULL Music 8 1 Music 8 2 Music 8 3 Music 8 4 

1 Answer 1

1

You have to use an aggregate function to choose which of the values of the duplicated rows you wish to show

SELECT P.Name AS [Playlist Name] ,MAX(P.PlaylistId) ,PT.TrackId AS [Track ID] FROM PlaylistTrack AS [PT] FULL JOIN Playlist AS [P] ON PT.PlaylistId = P.PlaylistId GROUP BY P.Name, PT.TrackId HAVING COUNT(CONCAT(P.Name,PT.TrackId)) > 1 ORDER BY P.Name 

Note MAX(P.PlaylistId) instead of P.PlaylistId

For the NULL values, try to switch to an INNER JOIN

Here you can find a list of the available functions

Sign up to request clarification or add additional context in comments.

1 Comment

Great, thanks! I was able to capture the information I needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.