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
 Display based on reference table

Author  Topic 

razeena
Yak Posting Veteran

54 Posts

Posted - 2013-02-06 : 22:55:19
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

52326 Posts

Posted - 2013-02-06 : 23:02:40
[code]
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

[/code]

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

Go to Top of Page
   

- Advertisement -