Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SQL
Message
De
08/02/2000 17:41:22
Dragan Nedeljkovich (En ligne)
Now officially retired
Zrenjanin, Serbia
 
 
À
08/02/2000 13:13:35
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Titre:
Re: SQL
Divers
Thread ID:
00328952
Message ID:
00329204
Vues:
29
>Sherry,
>
>I see several problems with this SQL. As a matter of fact I'm surprised it didn't error out.
>
>1) You have a field from Brokprod in your group by clause, yet no fields from Brokprod are in your select
>
>2) Any fields not involved in a sum, must be included in the group by clause.
>
>A simple example:
>
>Select name, address, city, zip, sum(bill) as bill from mytable ;
> group by name, address, city, zip ;
> into cursor mycursor
>
>Name,address, city, zip are all in the group by because they are not being summed.

Not necessarily so. We may simply group by zip if we wanted a 'per city' total. Of course, that would make the name and address fields meaningless. Depends on what we want.

My suggestion for this code is something like this:
 SELECT OUT.*, rpt_year, 
           SUM(PL_TOT) AS P_sum,;
	   SUM(CL_TOT) AS C_sum,;
	   SUM(TOTAL) AS T_sum;
       FROM BROKPROD,OUT;
       WHERE BROKPROD.AG_ID = OUT.AG_ID;
             GROUP BY BROKPROD.AG_ID, rpt_year ;
       into curs tmp

select a.ag_id, a.p_sum as p97, a.c_sum as c97, a.t_sum as t97, ;
   b.p_sum as p98, b.c_sum as c98, b.t_sum as t98,  ;
   c.p_sum as p99, c.c_sum as c99, c.t_sum as t99 ;
   from tmp a;
   full join tmp b on a.ag_id=b.ag_id and b.rpt_year='1998';
   full join tmp c on a.ag_id=c.ag_id and c.rpt_year='1999';
   where a.rpt_year='1997' ;
   order by 1 ;
       INTO TABLE &OUTPUT
I've used a three-way join later to get the years side by side. I think this way would be at least faster and more readable.
"Order by" defaults to the grouping order, so it's not really necessary. Of course, here we had the grouping in the first one and ordering in the second.

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform