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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to group sums and such...

Status
Not open for further replies.

grande

Programmer
Feb 14, 2005
657
CA
Okay so I have two tables tblExpenses and tblExpenseDetails (1-to-Many). tblExpenses has ProjectID and ExpenseType. There are 3 different types of Expenses (Purchase, Travel, Contract).

I would like to have a query that totals the Expense Details by Project and Expense Type. ie, each project will have 3 records in the query.

Sample Data: Pre-Query
Code:
tblExpenses
ExpenseID ProjectID ExpenseType
1         1         Purchase
2         2         Travel
3         1         Contract
4         1         Purchase

tblExpenseDetails
ID UnitPrice #ofUnits ExpenseID
1  $2        5        1
2  $5        6        1
3  $7        2        2
4  $2        9        2        
5  $3        6        3
6  $9        1        3
7  $1        9        4
8  $5        5        4

Sample Data: Post-Query
Code:
ProjectID ExpenseType Total
1         Purchase    $74
1         Contract    $27
2         Travel      $32

I also have to do taxes and such, but I'm pretty sure that if I get this, I'm pretty sure I can figure out the rest.

-------------------------
Just call me Captain Awesome.
 
Try:
Code:
SELECT tblExpenses.ProjectID, tblExpenses.ExpenseType, Sum(tblExpenseDetails.UnitPrice) AS Total
FROM tblExpenses INNER JOIN tblExpenseDetails ON tblExpenses.ExpenseID = tblExpenseDetails.ExpenseID
GROUP BY tblExpenses.ProjectID, blExpenses.ExpenseType;

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Now I'm getting an error message that says
"You tried to execute a query that does not include the specified expression 'ExpenseType' as part of an aggregate function."

Any ideas?

-------------------------
Just call me Captain Awesome.
 
what's the SQL you used?

Leslie

In times of universal deceit, telling the truth will be a revolutionary act. - George Orwell
 
I have (at least one) typo in my posting
Code:
GROUP BY tblExpenses.ProjectID, [COLOR=red]t[/color]blExpenses.ExpenseType;
 
I just copy & pasted exactly what traingamer typed. All the field names are correct.

I have the field ExpenseType set up as a ComboBox (In table design mode, under the "Lookup" tab). I use the SQL
Code:
SELECT [tblBudgetType].[BudgetTypeID], [tblBudgetType].[BudgetTypeText] FROM tblBudgetType;
to get the ExpenseType.

-------------------------
Just call me Captain Awesome.
 
Crap, it was a spelling mistake that I had missed, thanks guys!

-------------------------
Just call me Captain Awesome.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top