Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have Two table Named tbl_Products and tbl_Keywords listed below
SQL
tbl_Products 
ID 	KeywordIds
1 	1,2
2 	1,3
3 	1,4
4 	2,4

SQL
tbl_Keywords 
Key_ID	Keyword
1	Keyword1
2	Keyword2
3	Keyword3
4	Keyword4


I want to join this tbl_Products with tbl_keywords with keywords as csv as show below
SQL
Result
ID 	Keywords	keywordCSV
1 	1,2		Keyword1,Keyword2
2 	1,3		Keyword1,Keyword3
3 	1,4		Keyword1,Keyword4
4 	2,4		Keyword2,Keyword4


Thanks All
Posted
Updated 4-Nov-11 0:14am
v2

Your table design looks incorrect (if you are using relational databases).
The first table tbl_Products is not in first normal form.

The table should like
VB
ID  KeywordIds
1   1
1   2
2   1
2   3
3   1
3   4
4   2
4   4


If it did, a simple join would give you the results (or similar results to what) you are looking for.
 
Share this answer
 
v2
Comments
RaisKazi 5-Nov-11 4:31am    
Absolutely correct. 5ed
Abhinav S 8-Nov-11 2:08am    
Thank you Rais.
As mentioned in the solution 1, your database design is incorrect and I assume that it is an exisiting database and you are unable to change the database structure

This is the query

SQL
with testdata (ProductID, ItemID, Keywords) as (

select tbl_Products.ID,b.items,tbl_Keywords.Keywords
from tbl_Products
cross apply dbo.Split(tbl_Products.KeywordIDs ,',') b
left join tbl_Keywords on b.items=tbl_Keywords.ID
) select distinct ProductID ,substring( (
            select  ','+ Keywords
            from testdata inner1
            where inner1.ProductID =outer1.ProductID  for XML path('')
                  ),2,500)keywordCSV
from testdata outer1


and you needs to have the following function created in your database prior to the query execution


Create FUNCTION [dbo].[Split] (@String varchar(8000), @Delimiter char(1))
   returns @temptable TABLE (items varchar(8000))
   as
   begin
       declare @idx int
        declare @slice varchar(8000)

        select @idx = 1
            if len(@String)<1 or @String is null  return

       while @idx!= 0
       begin
           set @idx = charindex(@Delimiter,@String)
           if @idx!=0
               set @slice = left(@String,@idx - 1)
           else
              set @slice = @String

           if(len(@slice)>0)
               insert into @temptable(Items) values(@slice)

           set @String = right(@String,len(@String) - @idx)
           if len(@String) = 0 break
       end
   return
   end

GO



I have tested the solution in SQL server 2008 and it works, if you have any issues let me know the error message along with your SQL server version.
 
Share this answer
 
v3
I got a solution

SQL
declare @tbl_Products table(ID int, Keywords varchar(10))

insert into @tbl_Products values
(1,   '1,2'),
(2,   '1,3'),
(3,   '1,4'),
(4,   '2,4')

declare @tbl_Keywords table(Key_ID int, Keyword varchar(10))

insert into @tbl_Keywords values
(1,        'Keyword1'),
(2,        'Keyword2'),
(3,        'Keyword3'),
(4,        'Keyword4')

select P.ID,
       P.Keywords,
       stuff((select ', '+K.keyword
              from @tbl_Keywords as K
                inner join P.XKeywords.nodes('/k') as KX(N)            
                  on KX.N.value('.', 'int') = K.Key_ID
              for xml path(''), type).value('.', 'varchar(max)'), 1, 2, '') as keywordCSV
from (
      select ID,
             Keywords,
             cast('<k>'+replace(Keywords, ',', '</k><k>')+'</k>' as xml) as XKeywords
      from @tbl_Products
     ) as P​


Thanks All for Reply.
 
Share this answer
 
Comments
Bala Selvanayagam 5-Nov-11 5:14am    
This does not work for me when i tried.

Am i missing something ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900