Click here to Skip to main content
15,914,780 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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.
SQL
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 --and b.t_mitm = a.t_item           
                left join tticst001650 c on b.t_pdno = c.t_pdno and a.t_nuni = c. t_qucs --and a.t_item = c.t_sitm 
                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
SQL
(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]
Posted
Updated 10-Apr-14 19:37pm
v3

For that you can use CTE(common table expression)
a simple demostration is avail on this link

CTE[^]
 
Share this answer
 
Comments
[no name] 10-Apr-14 8:13am    
This is not what I am looking for.
Maciej Los 10-Apr-14 12:51pm    
It's very interesting statement...
This is exactly what you need.
Schatak 10-Apr-14 8:15am    
then can you please improve your question and let us know
Maciej Los 10-Apr-14 12:51pm    
+5!
 
Share this answer
 
Solution1 by Schatak is very good. As i mentioned, CTE is exactly waht you need!
Alternativelly, you can use: CURSOR[^] or/and WHILE[^] loop.
 
Share this answer
 
Comments
[no name] 10-Apr-14 23:21pm    
--content has been moved to the question---
Maciej Los 11-Apr-14 1:39am    
Please, improve your question and based on sample data provide more details about your issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900