hi
its easier to write a cursor and loop through ... !!
there are also other ways to do this !!
i tried to do this ONE WAY .. please take a look ..
i hard coded the commas part .. that needs to be fixed ..
please click arrow to the left for create data script
drop table #employee
create table #employee
(Id int,name varchar(20),mon int,tue int,wed int,thu int,fri int,sat int,sun int)
insert into #employee select 2,'john',1,1,1,1,0,0,1
insert into #employee select 3,'Jane',0,0,0,1,0,1,1
select * from #employee
drop table #lookup
create table #lookup
(
weekday varchar(20),
value varchar(10)
)
insert into #lookup select 'mon','80'
insert into #lookup select 'tue','90'
insert into #lookup select 'wed','98'
insert into #lookup select 'thu','65'
insert into #lookup select 'fri','66'
insert into #lookup select 'sat','43'
insert into #lookup select 'sun','56'

; with cte as
(
select Id,name
, case when mon =1 then 'mon' else '' end
+','+ case when tue =1 then 'tue' else '' end
+','+ case when wed =1 then 'wed' else '' end
+','+ case when thu =1 then 'thu' else '' end
+','+ case when fri =1 then 'fri' else '' end
+','+ case when sat =1 then 'sat' else '' end
+','+ case when sun =1 then 'sun' else '' end as ok
from #employee
) ,cte_lookup as
(
select
'mon' as mon,max(case when weekday = 'mon' then value end ) as okmon
, 'tue' as tue,max(case when weekday = 'tue' then value end ) as oktue
, 'wed' as wed,max(case when weekday = 'wed' then value end ) as okwed
, 'thu' as thu,max(case when weekday = 'thu' then value end ) as okthu
, 'fri' as fri,max(case when weekday = 'fri' then value end ) as okfri
, 'sat' as sat,max(case when weekday = 'sat' then value end ) as oksat
, 'sun' as sun,max(case when weekday = 'sun' then value end ) as oksun
from #lookup
)
select a.Id,a.name,
replace(replace(replace(replace(replace(replace(replace(replace(replace(a.ok,b.mon,b.okmon),b.tue,b.oktue),b.wed,b.okwed),b.thu,b.okthu),b.fri,b.okfri),b.sat,b.oksat),b.sun,b.oksun),',,',','),',,',',')
from cte a ,cte_lookup b
