Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Sum from fields in two tables
Message
De
16/11/2008 13:57:38
 
 
À
16/11/2008 13:43:43
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Versions des environnements
Visual FoxPro:
VFP 9 SP2
Divers
Thread ID:
01362166
Message ID:
01362172
Vues:
15
This message has been marked as the solution to the initial question of the thread.
>
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?
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform