JT_SIRO
asked on
SQL Selet Join Question
Hello - I have a relational database structure where I have a Songs table and a SongWriters table, that have a one-to-many relationship, joined by Songs.RecID = SongWriters.fkRecID.
I need to export to Excel in a Flat data structure. IE, I need to put all of the SongWriters for each song, into one SongWriters field. Does someone know the SQL to do this?
For example:
This Songs record:
RecID = 10, SongName = 'Let It Be'
need to join with these SongWriters table recs:
fkRecID = 10, WriterName = 'Jon Lenon', MusicOrg = 'ASCAP', OwnershipPercent = '25'
fkRecID = 10, WriterName = 'Paul McCartney', MusicOrg = 'ASCAP', OwnershipPercent = '25'
fkRecID = 10, WriterName = 'Ringo Star', MusicOrg = 'ASCAP', OwnershipPercent = '25'
fkRecID = 10, WriterName = 'George Harrison', MusicOrg = 'ASCAP', OwnershipPercent = '25'
I would want to put all of the above info into one field, delineated by a "/", if that's possible?
So my result set would look like:
RecID SongName Writers
-------- ------------- -------------------------- ---------- ---------- ---------- ---------- ---------- -----
10 Let it Be Jon Lenon 25% ASCAP / Paul McCartney 25% BMI / Ringo Star 25% BMI / George Harrison 25% SESAC
I need to export to Excel in a Flat data structure. IE, I need to put all of the SongWriters for each song, into one SongWriters field. Does someone know the SQL to do this?
For example:
This Songs record:
RecID = 10, SongName = 'Let It Be'
need to join with these SongWriters table recs:
fkRecID = 10, WriterName = 'Jon Lenon', MusicOrg = 'ASCAP', OwnershipPercent = '25'
fkRecID = 10, WriterName = 'Paul McCartney', MusicOrg = 'ASCAP', OwnershipPercent = '25'
fkRecID = 10, WriterName = 'Ringo Star', MusicOrg = 'ASCAP', OwnershipPercent = '25'
fkRecID = 10, WriterName = 'George Harrison', MusicOrg = 'ASCAP', OwnershipPercent = '25'
I would want to put all of the above info into one field, delineated by a "/", if that's possible?
So my result set would look like:
RecID SongName Writers
-------- ------------- --------------------------
10 Let it Be Jon Lenon 25% ASCAP / Paul McCartney 25% BMI / Ringo Star 25% BMI / George Harrison 25% SESAC
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked great! Thanks a lot!
select distinct RecID, SongName, dbo.fn_list_str(RecID) from Songs
Open in new window