Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sum from fields in two tables
Message
From
16/11/2008 14:09:25
 
 
To
16/11/2008 13:57:38
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9 SP2
Miscellaneous
Thread ID:
01362166
Message ID:
01362173
Views:
11
Exactly! thank you.

>>
>select CodeField, ;
>         SUM( SumForFieldTotal ) as SumForFieldTotal, ;
>        SUM( SumForFieldQuant) as SumForFieldQuant, ;   
>       Field3 ;
> from ( ;
>    ;
>>SELECT  Table1.cod as CodeField,  ;
>>	SUM( Table1.total) as SumForFieldTotal, ;
>>	SUM( Table1.quant) as SumForFieldQuant, ;
>>	Field3 ;
>>FROM Table1 ;
>>JOIN table3  ;
>>ON Table1.cod = table3.code ;
>>where table3.field4 = 1  ;
>>GROUP BY Field3, CodeField ;
>>UNION ALL select  ;
>>	table2.cod as CodeField, ;
>>	sum( table2.total) as SumForFieldTotal, ;
>>	sum( table2.quant) as SumForFieldQuant,  ;
>>	Field3  ;
>>	FROM table2 ;
>>	JOIN table3  ;
>>	ON table2.cod = table3.code  ;
>>	WHERE table3.field4 = 1 ;	
>>	GROUP BY Field3, CodeField ;
>>ORDER BY CodeField ;
>  ;
>  ) as xxx ;
>GROUP BY Field3, CodeField ;
>ORDER BY CodeField
>
>>
>
>
>
>>
>>SELECT  Table1.cod as CodeField,  ;
>>	SUM( Table1.total) as SumForFieldTotal, ;
>>	SUM( Table1.quant) as SumForFieldQuant, ;
>>	Field3 ;
>>FROM Table1 ;
>>JOIN table3  ;
>>ON Table1.cod = table3.code ;
>>where table3.field4 = 1  ;
>>GROUP BY Field3, CodeField ;
>>UNION ALL select  ;
>>	table2.cod as CodeField, ;
>>	sum( table2.total) as SumForFieldTotal, ;
>>	sum( table2.quant) as SumForFieldQuant,  ;
>>	Field3  ;
>>	FROM table2 ;
>>	JOIN table3  ;
>>	ON table2.cod = table3.code  ;
>>	WHERE table3.field4 = 1 ;	
>>	GROUP BY Field3, CodeField ;
>>ORDER BY CodeField
>>
>>
>>
>>>>I have to sum two fields. Same field name but in two different tables.
>>>>
>>>>Right now I sum and join but I end-up with two sums for the same Id.
>>>>
>>>>How do I sum everything without intermediate sums (because of the joins) ?
>>>>
>>>>I guess that "where in (select...) would be involved but I'm sot sure how to build this.
>>>
>>>Can you post your current code so we would know the problem?
*******************************************************
Save a tree, eat a beaver.
Denis Chassé
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform