tmartin314
tmartin314

Reputation: 4171

Building a query with two tables using SUM

I have two tables that I need to gather data from, then add up the sum of one field and return it if the sum is over a certain amount.

Affiliates Table - Table1: [id]
Commissions Table - Table1: [affiliate_id][amount]

I need to grab only affiates where the total commissions 'amount' is over 30.

I will also need to return all of the data from both tables into a single array.

Upvotes: 1

Views: 118

Answers (1)

Burak Guzel
Burak Guzel

Reputation: 1285

Try using the HAVING clause.

SELECT affiliate_id, SUM(amount) total_amount 
FROM commissions GROUP BY affiliate_id 
HAVING total_amount > 30

Upvotes: 1

Related Questions