User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 363,450 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,283 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser:
Views: 669 | Replies: 3
Reply
Join Date: Jan 2006
Posts: 4
Reputation: Asiima is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Asiima Asiima is offline Offline
Newbie Poster

MySQL SUM Statemene

  #1  
Apr 19th, 2008
Hi there,

Anyone to help me on sql code for SUM.
I need a query that gets the SUM of one table column and subtract it from the sum from another table column:
I tried this but it gave me wrong answer;

SELECT SUM(i.amount-p.amount) FROM income i, payments p;
Last edited by peter_budo : Apr 20th, 2008 at 6:36 am. Reason: Keep It Organized - please use [code] tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Posts: 102
Reputation: mwasif is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
mwasif mwasif is offline Offline
Junior Poster

Re: MySQL SUM Statemene

  #2  
Apr 19th, 2008
Try
  1. SELECT SUM(i.amount) - SUM(p.amount) FROM income i, payments p
Last edited by mwasif : Apr 19th, 2008 at 3:15 pm.
Reply With Quote  
Join Date: Jan 2006
Posts: 4
Reputation: Asiima is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Asiima Asiima is offline Offline
Newbie Poster

Re: MySQL SUM Statemene

  #3  
Apr 19th, 2008
Originally Posted by mwasif View Post
Try
  1. SELECT SUM(i.amount) - SUM(p.amount) FROM income i, payments p


Thanx but still doesn't work out
Reply With Quote  
Join Date: Apr 2008
Posts: 248
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 29
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: MySQL SUM Statement

  #4  
Apr 19th, 2008
hi,
without where clause cross product of both tables will be computed, that is each row of income will be combined with each row of payments. To prevent this, you need a where clause like where i.fieldofincome = p.fieldofpayments, If you don't have such common fields you can't join both tables.

krs,
cliff
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MySQL Marketplace
Thread Tools Display Modes

Other Threads in the MySQL Forum

All times are GMT -4. The time now is 2:32 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC