I want to make something like recursion. I have two queries. In first query by passing variable I get multiple values. Now again for each I will get again multiple values . and so on.. Each I want to pass in another query and want to make some calculation based in that. How to do same with sql server ?
[EDIT]
This r my queries.
from this m getting order and factor.
with captive as
(select b.t_pdno as Production_Order, d.t_orno as Cpt_Cons_frm, (a.t_nuni/e.t_quam) as Cpt_Cons_Fctr from
ttisfc001650 b
right join tticst300650 a on b.t_pdno = a.t_orno
left join tticst001650 c on b.t_pdno = c.t_pdno and a.t_nuni = c. t_qucs
left join twhltc100650 d on a.t_item = d.t_item and c.t_clot = d.t_clot
join (select t_orno, t_item, sum(t_quam)as t_quam from twhltc100650 where t_quam !=0 group by t_orno, t_item) e on d.t_orno = e.t_orno
where a.t_cpcp_f = '002')
select * from captive where Production_Order = @PRDN;
Now I want to pass this factor and Production order into following query
with costs as
(select t_orno Production_Order, "001" * @FACT as Material, "009" * @FACT as Subcontract, "010" * @FACT as Variable, "011" * @FACT as Fixed, "012" * @FACT as Depreciation from
(select t_cprj, t_orno, t_cpcp_f, t_amnt_f_1 as t_amnt
from tticst300650 where t_fitr in ('52','61')) y
pivot (sum(t_amnt) for t_cpcp_f in ("001", "009", "010", "011", "012" )) as p)
select * from costs where Production_Order = @PRDN;
This may give multiple results. From all I want to pass production order value in first query and so on. And in last I want sum of all costs.
Note: 'EDIT' part comes from comment to the solution 3 - Maciej Los
[/EDIT]