| Author |
Topic |
|
liffey
Yak Posting Veteran
58 Posts |
Posted - 2005-11-03 : 10:03:16
|
| Suggestions please!I have two tables one for names and a second for aliases (AKA – Also Known As)Names Table contains;ID, First Name, Surname1, John, Murphy2, Mary, Jones3, Julian, DuffAKA Table containsID, NamesID, AKA FirstName, AKA Surname1, 2, May,2, 2, Beth,3, 2, Jane4, 3, Jules, Duffey5, 3, JJ,The results I want areNOTE: below I use * to separate fields because a comma delimits entries in the AKA field.FirstName, SurnameJohn * MurphyMary (May, Beth, Jane) * JonesJulian (Jules, JJ) * Duff (Duffey)ORFirstName, AKAFirstName, Surname, AKASurnameJohn * MurphyMary (May, Beth, Jane) * JonesJulian * Jules, JJ * Duff * DuffeyIn other words what I want is 1 row per row in the Names Table (3 in this example) with the values from all matching entries in the AKA table either appended to the first name field in the result OR in a new field for each first and surname.Is this possible using SLQ or would I be better returning 2 related tables and iterating through them in code?Declan -dw |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-11-03 : 13:34:48
|
dw,Here's one way. This solution creates a function that returns an AKA CSV for a given NameID. set nocount onuse northwind----------------------------------------------------------------------DDL/DMLcreate table Names (ID int, [First Name] varchar(20), Surname varchar(20))create table AKA (ID int, NamesID int, AKAFirstName varchar(20), AKASurname varchar(20))goinsert Names select 1, 'John', 'Murphy' union allselect 2, 'Mary', 'Jones' union allselect 3, 'Julian', 'Duff'insert AKAselect 1, 2, 'May', null union allselect 2, 2, 'Beth', null union allselect 3, 2, 'Jane', null union allselect 4, 3, 'Jules', 'Duffey' union allselect 5, 3, 'JJ', null--------------------------------------------------------------------go--Function to return a comma seperated list of AKAs for a given NameIDcreate function dbo.fn_AKA(@namesid int, @FS char(1) )returns varchar(200)asbegin declare @aka varchar(200) if @FS = 'F' begin select @aka = coalesce(@aka + ', ' + AKAFirstName, AKAFirstName) from AKA where NamesID = @namesid and AKAFirstName is NOT NULL end else begin select @aka = coalesce(@aka + ', ' + AKAsurname, AKAsurname) from AKA where NamesID = @namesid and AKASurname is NOT NULL end return @akaendgo----------------------------------------------------------------------select statement using functionselect FName = [First Name] + coalesce(' (' + dbo.fn_AKA(ID,'F') + ')', '') ,Surname = Surname + coalesce(' (' + dbo.fn_AKA(ID,'S') + ')', '')from Namesgo----------------------------------------------------------------------cleanupdrop function dbo.fn_AKAdrop table AKAdrop table NamesBe One with the OptimizerTG |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
liffey
Yak Posting Veteran
58 Posts |
Posted - 2005-11-04 : 05:22:03
|
| TG,Brilliant, thanks! madhivanan,Thanks for the link, I will study this.All,I never ceases to amase me how generous people like you all can be. While waiting/hoping for a response I had coded my requirements within my application. However yours (TG) is a much more elegant solution which I will use.Thanks again,Declan-dw |
 |
|
|
|
|
|