--select [1] / ([0] + [1]) from
--(
--select ex_pwsr.r as v, sum(ex_pwsr.p * ex_pr.p * ex_ps.p)as p
--from ex_pwsr 
--	inner join ex_pr on ex_pwsr.r = ex_pr.r 
--	inner join ex_ps on ex_pwsr.s = ex_ps.s
--where
--	w = 1 
--	and ex_pwsr.s = 1
--group by ex_pwsr.r
--) as s
--pivot (sum(p) for v in ([0],[1])) as pt

declare @smoking bit;
declare @asia bit; 
declare @xray bit;
declare @disponea bit;

set @asia = 0;
set @smoking = 0;
set @xray = 1;
set @disponea = 0;

-- tb
select '1.P(Tuber)', [1] / ([0] + [1]) from
(
select pta.t as t, sum(pta.p * pa.p * pelt.p * pxe.p * pls.p * ps.p * pbs.p * pdeb.p) as p
from pa, pta, pelt, pls, pxe, ps, pbs, pdeb
where
	pa.a = pta.a and
	pta.t = pelt.t and
	pelt.e = pxe.e and
	pelt.e = pdeb.e and
	ps.s = pls.s and
	pls.l = pelt.l and
	ps.s = pbs.s and
	pbs.b = pdeb.b
	
	--and pta.a = @asia
	and ps.s = @smoking
	and pxe.x = @xray
	--and pdeb.d = @disponea
group by pta.t
) as s
pivot (sum(p) for t in ([0],[1])) as pt

union

-- lc
select '2.P(LungCancer)', [1] / ([0] + [1]) from
(
select pls.l as v, sum(pta.p * pa.p * pelt.p * pxe.p * pls.p * ps.p * pbs.p * pdeb.p) as p
from pa, pta, pelt, pls, pxe, ps, pbs, pdeb
where
	pa.a = pta.a and
	pta.t = pelt.t and
	pelt.e = pxe.e and
	pelt.e = pdeb.e and
	ps.s = pls.s and
	pls.l = pelt.l and
	ps.s = pbs.s and
	pbs.b = pdeb.b
	
	--and pta.a = @asia
	and ps.s = @smoking
	and pxe.x = @xray
	--and pdeb.d = @disponea
group by pls.l
) as s
pivot (sum(p) for v in ([0],[1])) as pt

union

-- lc
select '3.P(Bronchitis)', [1] / ([0] + [1]) from
(
select pbs.b as v, sum(pta.p * pa.p * pelt.p * pxe.p * pls.p * ps.p * pbs.p * pdeb.p) as p
from pa, pta, pelt, pls, pxe, ps, pbs, pdeb
where
	pa.a = pta.a and
	pta.t = pelt.t and
	pelt.e = pxe.e and
	pelt.e = pdeb.e and
	ps.s = pls.s and
	pls.l = pelt.l and
	ps.s = pbs.s and
	pbs.b = pdeb.b
	
	--and pta.a = @asia
	and ps.s = @smoking
	and pxe.x = @xray
	--and pdeb.d = @disponea
group by pbs.b
) as s
pivot (sum(p) for v in ([0],[1])) as pt