SQL to SUM and GROUP BY

JohnPapa

Registered User.
Local time
Today, 04:22
Joined
Aug 15, 2010
Messages
1,096
I have a table with 4 fields, A,B,C and D.
Would like to sum up individually the values of B,C and D and GROUP BY A,

Code:
For example Input is
A      B    C    D
1      3    4    1
1      1    1    3
3      4    7    2
3      8    4    3
5      3    5    1
 
Output should be
1      4    5    4
3     12   11    5 
5      3    5    1
I tried using Sum and Group By, but to no avail.

Is there a single SQL statement which can achieve this?

Thanks,
John
 
Code:
Select A, Sum(B) As SumOfB, Sum(c) As SumOfC, Sum(d) As SumOfD From TableName Group By A;
 
David,

I had the syntax right but was trying to assign the SELECT as RecordSource to a form where the field name was not present.

For SUM(B) AS sumB I did not have a field sumB.

Thanks,
John
 
Have you created the query to start with? If not do so then use the query as your recordsource.
 
Another problem.

The actual is SQL is indicated below:

Me.RecordSource = "SELECT strCC, SUM(sngTRCost) AS sngTRCostSum, SUM(LCCost1) AS LCCost1Sum, SUM(LCCost2) AS LCCost2Sum, SUM(LCCost3) AS LCCost3Sum FROM tbl004 GROUP BY strCC;"

There are 5 fields,
strCC, sngTRCostSum, LCCost1Sum, LCCost2Sum, LCCost3Sum

I need to add to each record one more field, tblCountry.strCountry where tblCountry.strCode = tbl004.strCC

Thanks,
John
 

Users who are viewing this thread

Back
Top Bottom