Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Sum and count filtered records in an SQL statement
Message
De
26/11/1998 18:17:01
 
 
À
26/11/1998 04:27:47
Marcelo Codino
Mystic & Faith - Sistemas -
Buenos Aires, Argentine
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00161724
Message ID:
00161871
Vues:
29
Sum() and Count() respect the WHERE clause. Try to rewrite your SELECT-SQL like this:
SELECT Field1, Field2, Field3, Field4, Sum(Field3) AS n_Imp, Count(Field1) AS n_Can;
   FROM MyTable;
   WHERE (Field2 = tcPeriod) AND (Field3 = tcYear);
   INTO CURSOR c_Names;
   ORDER BY Field1
Another reason that your code would not give you the result you expect, is if SET ANSI is OFF and tcPeriod and tcYear are empty. In that case, put SET ANSI to ON or use the "==" instead of the single "=".

HTH
>I have this SQL statement.The instruction works fine but it sums and counts all the records.I want to do this but only with filetered records.Is it possible in the same instruction ?
>
>SELECT a.field1, a.field2, a.field3, a.field4 ;
> FROM mytable a ;
> SUM(a.field3) AS n_imp, ;
> COUNT(a.field1) AS n_can ;
> WHERE a.field2 = tcperiod ;
> AND a.field3 = tcyear ;
> ORDER BY a.field1 ;
> INTO CURSOR c_names
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform