SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Please Advice
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pushp82
Yak Posting Veteran

83 Posts

Posted - 09/25/2012 :  02:34:33  Show Profile  Reply with Quote
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)

Singapore
17595 Posts

Posted - 09/25/2012 :  02:50:09  Show Profile  Reply with Quote
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
Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000