If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Where clause from sum() value?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-10-10, 11:06
RandyRiegel RandyRiegel is offline
Registered User
 
Join Date: Sep 2003
Posts: 22
Where clause from sum() value?

I've been away from SQL for quite a while and am working on a DB project now. I have the following code I am trying to only get Tenants who have a negative balance.

Code:
select sum(finance.trans_amount) as balance, tenants.last, tenants.first from 
finance join tenants on (finance.trans_tenant = tenants.tenant_id)
group by finance.trans_tenant 
order by balance;
I have tried to put a "where balance < 0" in the statement but get an error about balance not being a column. Without the where clause like above everything works fine. What do I need to do to only retrieve tenants with negative balances.
Reply With Quote
  #2 (permalink)  
Old 09-10-10, 12:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT tenants.last
     , tenants.first 
     , SUM(finance.trans_amount) AS balance
  FROM finance 
INNER
  JOIN tenants 
    ON tenants.tenant_id = finance.trans_tenant
GROUP 
    BY tenants.last
     , tenants.first 
HAVING balance < 0
ORDER 
    BY balance;
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On