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
 General SQL Server Forums
 New to SQL Server Programming
 Multiple Join With Comma Delimited List Column

Author  Topic 

boybles
Starting Member

23 Posts

Posted - 2009-06-25 : 23:09:44
I've been racking my brain for hours trying to figure out the SQL on how to join all these columns together and create a comma delimited list of areas associated with each store. The following lays out the tables/columns and desired result. Please, please help!
Thanks
Tony


Store
----------
StoreID(int)
StoreName(varchar)
StoreType(int)

StoreType
----------
TypeID(int)
TypeName(varchar)

Area
-----------
AreaID(int)
AreaName(varchar)


AreaStoreMap
--------------
MapID(int)
StoreID(int)
AreaID(int)

Store
---------------------------
StoreID|StoreName|StoreType
---------------------------
1|StoreA|1
2|StoreB|1
3|StoreC|2
4|StoreD|1

StoreType
-----------------
TypeID|TypeName
------------------
1|Pharmacy
2|Market


Area
-------------------
AreaID|AreaName
-------------------
1|Nevada
2|Washington
3|Oregon
4|Kansas

AreaStoreMap
-------------------
MapID|StoreID|AreaID
---------------------
1|2|1
2|2|3
3|2|2
4|1|4
5|1|3
6|4|2


Desired Result (where Areas is a delimited list of areaIDs)
--------------------------------
StoreID|StoreName|TypeName|Areas
---------------------------------
1|StoreA|Pharmacy|4,3
2|StoreB|Pharmacy|1,3,2
3|StoreC|Market|
4|StoreD|Pharmacy|2

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-25 : 23:10:45
if you are using SQL 2005 you can use this method concatenate records without UDF


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

boybles
Starting Member

23 Posts

Posted - 2009-06-25 : 23:18:44
Thanks. That does shed some light, but the joins are what confuse me combined with the list. Especially when you see that one of the stores (StoreC) doesn't have any areas mapped but still needs to be a part of the result set. What would the SQL look like? Thanks
Tony
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-25 : 23:34:29
[code]
select s.StoreID, s.StoreName, t.TypeName,
Areas = STUFF((SELECT ',' + convert(varchar(10), x.AreaID)
FROM AreaStoreMap x
WHERE x.StoreID = s.StoreID
FOR XML PATH('')), 1, 1, '')
from Store s
inner join StoreType t on s.StoreType = t.TypeID
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

boybles
Starting Member

23 Posts

Posted - 2009-06-26 : 01:08:42
Khatan,
That works perfectly. Thank you very, very much for your guidance. BTW, was there a way to solve this kind of situation (without UDF) before SQL Server 2005? Just curious.
Thanks again for your invaluable help.
Tony
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-26 : 02:40:14
before 2005, read Rowset string concatenation: Which method is best ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -