Assuming that I understood what you said - does this resemble what you are looking for? DECLARE @Tracks TABLE(TrackID int NOT NULL, TrackName varchar(50) NOT NULL,GenreID int NOT NULL,ArtistId int NOT NULL);DECLARE @GenreTracks TABLE(ID int NOT NULL IDENTITY(1,1),TrackId int NOT NULL,GenreId int NOT NULL);INSERT @Tracks(TrackID,TrackName,GenreID,ArtistId)SELECT 100,'Track-100',100,100UNION ALLSELECT 200,'Track-200',100,100UNION ALLSELECT 300,'Track-300',100,200UNION ALLSELECT 400,'Track-400',400,200UNION ALLSELECT 500,'Track-500',500,300UNION ALLSELECT 100,'Track-100',400,100UNION ALLSELECT 100,'Track-100',500,100INSERT @GenreTracks(GenreId,TrackId)SELECT DISTINCT GenreID,TrackID FROM @Tracks;;WITH CTE AS ( SELECT ROW_NUMBER() OVER (PARTITION BY TrackId,ArtistId ORDER BY ArtistID) RowCtr, TrackID, TrackName, ArtistId, GenreID FROM @Tracks)-- the data to be retainedSELECT 'FinalList',TrackID,TrackName,ArtistId FROM CTE WHERE RowCtr = 1;-- run this delete to delete data and retain what is needed--DELETE CTE WHERE RowCtr > 1-- drop the GenreId column after deleting the rowsSELECT 'FullList',* FROM @Tracks;SELECT * FROM @GenreTracks;
If this isn't what you wanted - could you use the sample code above to explain what you need?