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