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 > Data Access, Manipulation & Batch Languages > ANSI SQL > trouble counting SUM GROUPed BY

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-13-05, 09:21
stasik_du stasik_du is offline
Registered User
 
Join Date: Oct 2004
Posts: 12
trouble counting SUM GROUPed BY

I've got payments table for estate agency and I want display Properties and total ammount was payed for each property
SELECT PROPERTYID, PROPERTYNAME, TENANT, SUM(PAYMENTAMOUNT) FROM PAYMENTS GROUP BY PROPERTYID;
and it doesn't work.
How can I do it?
Reply With Quote
  #2 (permalink)  
Old 01-13-05, 10:23
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Cool

You need to add ALL aggregated columns to the GROUP BY:
Code:
SELECT PROPERTYID, PROPERTYNAME, TENANT, SUM(PAYMENTAMOUNT)
  FROM PAYMENTS
 GROUP BY PROPERTYID, PROPERTYNAME, TENANT;
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 01-13-05, 10:26
ndu35 ndu35 is offline
Registered User
 
Join Date: May 2003
Location: France
Posts: 112
SELECT PROPERTYID, PROPERTYNAME, TENANT, SUM(PAYMENTAMOUNT) FROM PAYMENTS GROUP BY PROPERTYID,PROPERTYNAME,TENANT;

All colums must be in group by clause
Or
select a.propertyid, propertyname, tenant from payments b, (a.totalselect propertyid, sum(paymentamount) total from payments a group by propertyid)
where a.propertyid=b.propertyid

Hope this help you
Reply With Quote
  #4 (permalink)  
Old 01-13-05, 12:59
stasik_du stasik_du is offline
Registered User
 
Join Date: Oct 2004
Posts: 12
what does a.totalselect means?
Reply With Quote
  #5 (permalink)  
Old 01-13-05, 13:10
stasik_du stasik_du is offline
Registered User
 
Join Date: Oct 2004
Posts: 12
Quote:
Originally Posted by LKBrwn_DBA
You need to add ALL aggregated columns to the GROUP BY:
Code:
SELECT PROPERTYID, PROPERTYNAME, TENANT, SUM(PAYMENTAMOUNT)
  FROM PAYMENTS
 GROUP BY PROPERTYID, PROPERTYNAME, TENANT;
But I want it to be grouped by propertyID only and in the same time see other fields.
Reply With Quote
  #6 (permalink)  
Old 01-13-05, 14:09
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Cool

Try this:
Code:
SELECT P.PROPERTYID, P.PROPERTYNAME, P.TENANT, T.TOTAMT
  FROM PAYMENTS P
     , (SELECT PROPERTYID, SUM(PAYMENTAMOUNT) TOTAMT
          FROM PAYMENTS
         GROUP BY PROPERTYID) T
 WHERE P.PROPERTYID = T.PROPERTYID;
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
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