Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL SUM Function

Status
Not open for further replies.

Paris2000

Programmer
Joined
May 1, 2002
Messages
1
Location
GB
Hi There, as a new developer I was hoping someone out there could answer this for me.

Is it possible to use the SUM function on a field that has been already SUMmed ?

eg, How would I manage to get the total of the Payments Column in this example.

SELECT DISTINCT
e.description,
SUM(a.hectares) Area,
b.rate,
SUM(a.HECTARES * b.RATE) Payment,
c.occ_id
FROM G_FIELD_AREA_ZONATION a,
G_PAYMENT_RATE b,
G_OCCUPIER c,
G_FIELD d,
G_ZONATION_TYPE e,
G_HOLDING f,
G_SEASON g
WHERE a.znt_id IN (SELECT znt_id FROM G_ZONATION_TYPE)
AND c.occ_id = &Occupier_Id
AND c.occ_id = f.occ_id
AND f.HLD_ID = d.HLD_ID
AND b.sea_id = g.sea_id
AND a.ZNT_ID = e.ZNT_ID
AND d.FLD_ID = a.fld_id
AND e.ZNT_ID = b.ZNT_ID
GROUP BY e.description,b.rate, c.occ_id

Many thanks to any of you who can help.
 
Try:

Code:
SELECT SUM( Payment )
FROM (
     SELECT DISTINCT        
            e.description, 
            SUM(a.hectares) Area,
            b.rate,
            SUM(a.HECTARES * b.RATE) Payment,
            c.occ_id
     FROM   G_FIELD_AREA_ZONATION a,
            G_PAYMENT_RATE b,
            G_OCCUPIER c,
            G_FIELD d,
            G_ZONATION_TYPE e,
            G_HOLDING f,
            G_SEASON g
     WHERE  a.znt_id IN (SELECT znt_id FROM G_ZONATION_TYPE)
     AND       c.occ_id = &Occupier_Id
     AND       c.occ_id = f.occ_id
     AND       f.HLD_ID = d.HLD_ID
     AND       b.sea_id = g.sea_id
     AND       a.ZNT_ID = e.ZNT_ID
     AND       d.FLD_ID = a.fld_id
     AND    e.ZNT_ID = b.ZNT_ID
     GROUP BY e.description,b.rate, c.occ_id
);

Does this help?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top