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 2005 Forums
 Transact-SQL (2005)
 Select multi value field

Author  Topic 

MiaF
Starting Member

13 Posts

Posted - 2007-03-09 : 14:13:00
I don't know how to title my question. What I want to do is the following:

I have two tables

Customer:
ID Name
1 John
2 Mary

Purchase:
ID Customer Item
1 1 book
2 2 shoes
3 1 pencil

Is there a query I can write to get a result like this:
Customer Purchase
John book pencil
Mary shoes

Thanks ahead for any idea.

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2007-03-09 : 16:36:24
There is a T-SQL solution here
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=53293
and if you want to get into CLR programming (2005 only) then there is a CLR user-defined concatenate aggregate function here
http://msdn2.microsoft.com/en-us/library/ms254508.aspx
Go to Top of Page

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2007-03-10 : 05:23:19
try this

Declare @strValue VarChar(1000)
Select Id, @StrValue = Case When @strValue Is Null Then Item Else @strValue + ', ' + Item End From <Ur Table>
Group By Id
Select Id, @strValue As Item

U can use COALESCE function also to do so.

let me know,

Mahesh
Go to Top of Page
   

- Advertisement -