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

Build query summing a column by another column 1

Status
Not open for further replies.

LeeAnnF

Programmer
Aug 13, 2004
5
US
I have 2 tables, money_sheet and cash_receiving. The linking column is document_num. The cash_receiving table has one amount per document_num. the money_sheet table has multiple amounts per document_num. The cash_receiving table is the money as it comes into the agency. The money_sheet table is how that money is spent. I need to get a sum of the money_sheet.amount per money_sheet.document_num to see if the sum of the money_sheet.amount = the cash_receiving.amount.

select m.document_num, m.amount, c.amount
from money_sheet m, cash_receiving c
where m.document_num = c.document_num
group by m.document_num, m.amount, c.amount

I tried making the m.amount, sum(m.amount) but that just sums each entry for m.amount, not the sum of all the m.amounts for the m.document_num.

Any suggestions are greatly appreciated!!
 
You need to include c.amount (but not m.amount) in the group by to avoid duplicating it:

select m.document_num, sum(m.amount), c.amount
from money_sheet m, cash_receiving c
where m.document_num = c.document_num
group by m.document_num, c.amount

 
I could have sworn I tried that combo! This worked perfectly. Thank you so much!
LeeAnn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top