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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Concatenating data of a field
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Srinika
Flowing Fount of Yak Knowledge

Sri Lanka
1378 Posts

Posted - 01/31/2007 :  13:06:56  Show Profile  Reply with Quote
I have 3 tables t1(F1,F2,F3), t2(F2,F4) & t3(F1,F5)

create table #t1(F1 varchar(10),F2 varchar(10),F3 int) 
create table #t2(F1  varchar(10),F4 varchar(10)) 
create table #t3(F2 varchar(10),F5 varchar(10))

Insert into #t1 ('aa','bb',3)
Insert into #t1 ('dd','bb',17)
Insert into #t1 ('gg','cc',36)
Insert into #t1 ('jj','dd',24)

Insert into #t2 ('bb','BBBB')
Insert into #t2 ('cc','CCCC')
Insert into #t2 ('dd','DDDD')

Insert into #t3 ('aa','ZZZZZ')
Insert into #t3 ('aa','YYYYY')
Insert into #t3 ('gg','XXXXX')
Insert into #t3 ('jj','WWWWW')
Insert into #t3 ('jj','VVVVV')

What I want as the results:

aa	bb	BBBB	ZZZZ,YYYY	 	 3
dd	bb	BBBB	NULL			17
gg	cc	CCCC	XXXXX	 		24
jj	dd	DDDD	WWWWW,VVVVV	 	 3

My Query So far

Select #t1.F1, #t1.F2, #t2.F4, ........... , #t1.F3 
From #t1 
inner join #t2 on #t1.F2 = #t2.F2
inner join #t3 on #t1.F1 = #t3.F1


Please help me to build the query


Srinika

SwePeso
Patron Saint of Lost Yaks

Sweden
30102 Posts

Posted - 01/31/2007 :  13:26:13  Show Profile  Visit SwePeso's Homepage  Reply with Quote
SELECT DISTINCT	F1,
		F2,
		F4,
		dbo.fnConcatString(F5) AS F5,
		F3
FROM		(
			SELECT		t1.F1,
					t1.F2,
					t2.F4,
					t3.F5,
					t1.F3
			FROM		#Table1 AS t1
			LEFT JOIN	#Table2 AS t2 ON t2.F1 = t1.F3
			LEFT JOIN	#Table3 AS t3 ON t3.F2 = t1.F1
		) AS d
The function dbo.fnConcatString can be found here at SQLTeam!
Do a search...


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Srinika
Flowing Fount of Yak Knowledge

Sri Lanka
1378 Posts

Posted - 01/31/2007 :  14:47:11  Show Profile  Reply with Quote
Thanks Peso

Found something similar to fnConcatString and done.


Srinika
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.06 seconds. Powered By: Snitz Forums 2000