Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SUM not working in report field
Message
De
01/12/2005 08:07:33
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivie
 
 
À
01/12/2005 08:00:17
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01073187
Message ID:
01073650
Vues:
16
>Thank you Hilmar.
>The error in my code above was just the way I typed it here. It was right in my actual code.
>I think we are getting closer. This is my actual SQL statement:
>
>SELECT ALL codigo,IIF(MONTH(fecha)=1,cantidad,$0),;
> IIF(MONTH(fecha)=2,cantidad,$0),;
> IIF(MONTH(fecha)=3,cantidad,$0),;
> IIF(MONTH(fecha)=4,cantidad,$0),;
> IIF(MONTH(fecha)=5,cantidad,$0),;
> IIF(MONTH(fecha)=6,cantidad,$0),;
> IIF(MONTH(fecha)=7,cantidad,$0),;
> IIF(MONTH(fecha)=8,cantidad,$0),;
> IIF(MONTH(fecha)=9,cantidad,$0),;
> IIF(MONTH(fecha)=10,cantidad,$0),;
> IIF(MONTH(fecha)=11,cantidad,$0),;
> IIF(MONTH(fecha)=12,cantidad,$0)FROM ventasxmes GROUP BY codigo INTO CURSOR xvenxmes
>
>REPORT FORM ventasmensual PREVIEW
>
>In the report I have 12 'cantidad' fields, each with a 'Print when month(fecha)=1' (one for each month) condition. It works ok. Only problem is that if a given product had, say, sales in 3 different months, it shows each month in a separate line, instead of putting all the sales under its respective month column in a single line. (Hope this makes sense) :)
>How can we solve this?
>Mil Gracias.

Well, after separating months by columns as above, you can apply SUM() + GROUP BY. You already included the GROUP BY; I would expect the above command to get one single amount from a more or less arbitrary record (instead of the sum) for each product. If codigo is the product code, I would add the sum() around the iif():
SELECT ALL;
     codigo,;
     sum(IIF(MONTH(fecha)=1,cantidad,$0)),;
     sum(IIF(MONTH(fecha)=2,cantidad,$0)),;
     ...
     sum(IIF(MONTH(fecha)=12,cantidad,$0));
  FROM ventasxmes;
  GROUP BY codigo;
  INTO CURSOR xvenxmes
This would first add or not add an amount to a specific month (with the iif()), and then add all the amounts for a product (with the sum() and group by).

By the way, I am assuming that you use currency for the amounts. If you use numeric, it is better to replace $0 with 000000.00 - adjust the number of zeroes to the size of your numeric field.
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform