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;

Recommended Answers

All 3 Replies

Try

SELECT SUM(i.amount) - SUM(p.amount) FROM income i, payments p

Try

SELECT SUM(i.amount) - SUM(p.amount) FROM income i, payments p

Thanx but still doesn't work out

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.