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
 Display based on reference table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

razeena
Yak Posting Veteran

53 Posts

Posted - 02/06/2013 :  22:55:19  Show Profile  Reply with Quote
Hi,
Please see two master table and a link table below.
----------
declare @tblClient table(cName varchar(30),cid int)
insert into @tblClient
select 'anu',100 union all
select 'binu',101

select * from @tblClient

declare @tblPayor table(pName varchar(30),pid int)
insert into @tblPayor
select 'Jacob',200 union all
select 'Mary',201 union all
select 'Lissy',202

select * from @tblPayor

declare @tblLink table(lid int,cid int,pid int)
insert into @tblLink
select 1,100,200 union all
select 2,100,201 union all
select 3,100,202 union all
select 4,101,200

select * from @tblLink
---------------
A cname can be assigned to multiple Pname.Based on the link table values, comma separated output requried like this.
==========
/*
CName PName
Anu Jacob,Mary,Lissy
Binu Jacob
*/
===========
Any ideas?

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 02/06/2013 :  23:02:40  Show Profile  Reply with Quote

SELECT c.cName,
STUFF((SELECT ',' + pName FROM @tblPayor p 
       INNER JOIN @tblLink l
       ON l.pid = p.pid
       WHERE l.cid = c.cid
       ORDER BY l.lid
       FOR XML PATH('')),1,1,'') AS PName
FROM @tblClient c


output
------------------------------------------------
cName	PName
------------------------------------------------
anu	Jacob,Mary,Lissy
binu	Jacob



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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