Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Is this another pivot question?

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, Surname
1, John, Murphy
2, Mary, Jones
3, Julian, Duff

AKA Table contains
ID, NamesID, AKA FirstName, AKA Surname
1, 2, May,
2, 2, Beth,
3, 2, Jane
4, 3, Jules, Duffey
5, 3, JJ,

The results I want are

NOTE: below I use * to separate fields because a comma delimits entries in the AKA field.

FirstName, Surname
John * Murphy
Mary (May, Beth, Jane) * Jones
Julian (Jules, JJ) * Duff (Duffey)

OR
FirstName, AKAFirstName, Surname, AKASurname
John * Murphy
Mary (May, Beth, Jane) * Jones
Julian * Jules, JJ * Duff * Duffey


In 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 on
use northwind

--------------------------------------------------------------------
--DDL/DML
create table Names (ID int, [First Name] varchar(20), Surname varchar(20))
create table AKA (ID int, NamesID int, AKAFirstName varchar(20), AKASurname varchar(20))
go
insert Names
select 1, 'John', 'Murphy' union all
select 2, 'Mary', 'Jones' union all
select 3, 'Julian', 'Duff'

insert AKA
select 1, 2, 'May', null union all
select 2, 2, 'Beth', null union all
select 3, 2, 'Jane', null union all
select 4, 3, 'Jules', 'Duffey' union all
select 5, 3, 'JJ', null
--------------------------------------------------------------------
go
--Function to return a comma seperated list of AKAs for a given NameID
create function dbo.fn_AKA(@namesid int, @FS char(1) )
returns varchar(200)
as
begin
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 @aka
end
go
--------------------------------------------------------------------
--select statement using function
select FName = [First Name] + coalesce(' (' + dbo.fn_AKA(ID,'F') + ')', '')
,Surname = Surname + coalesce(' (' + dbo.fn_AKA(ID,'S') + ')', '')
from Names

go

--------------------------------------------------------------------
--cleanup
drop function dbo.fn_AKA
drop table AKA
drop table Names


Be One with the Optimizer
TG
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-03 : 23:54:06
Here is why Function is to be used
http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx?Pending=true

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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
Go to Top of Page
   

- Advertisement -