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
 Please Advice

Author  Topic 

pushp82
Yak Posting Veteran

83 Posts

Posted - 2012-09-25 : 02:34:33
Table1
ID Code
1 A
2 B
3 C
4 D
5 E
6 F
12 L
13 M
14 N
15 O
16 P
17 Q
18 R

Table2
VALUE
1,2,4
2,5,6
3,4
4,6,1
12,16,1
18,12,1

I have two tables as above and want result as under:
VALUE_Code
A,B,D
B,E,F
C,D
D,F,A
L,P,A
R,L,A

Please advice!
Pushp

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-09-25 : 02:50:09
using fnParseList from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033

with cte as
(
select t2.VALUE, csv.RowID, csv.Data, t1.Code
from Table2 t2
cross apply dbo.fnParseList(',', VALUE) csv
inner join Table1 t1 on csv.Data = t1.ID
)
select distinct stuff(CodeCSV, 1, 1, '')
from cte c
cross apply
(
select ',' + Code
from cte x
where x.VALUE = c.VALUE
order by RowID
for xml path ('')
) csv (CodeCSV)



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

Go to Top of Page
   

- Advertisement -