aspose file tools
The moose likes JDBC and the fly likes SQL statment - issues with SUM Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "SQL statment - issues with SUM" Watch "SQL statment - issues with SUM" New topic
Author

SQL statment - issues with SUM

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
ok, here is the deal:

I have 2 tables: invoice & payments.

the invoice include all information the customer needs to pay (hotel, flights...) the payments shows all payments done by him.

So.... I am trying to get the SUM of all the invoice AND the SUM of all payments.
it works...but not correctly. It if there are 5 arguments in the deposit and one payment it multiply the deposit 5 times (the amount of argumetns in the invoice).

My statment is this:

SELECT invoice.Customer_ID,
SUM( (perCustomer)* (timesCustomer)) as TotalInvoice,
SUM( paymentAmount) as TotalPayments,
( SUM(perCustomer)* SUM(timesCustomer)-SUM(paymentAmount) ) as Balance
FROM invoice, payment
WHERE invoice.Customer_ID = payment.Customer_ID
GROUP BY Customer_ID


any idea???
thanks
Peter

[Bear edit: improved title]
[ April 07, 2005: Message edited by: Bear Bibeault ]
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Clarification:

how can i get the output below?

Table invoice

CustomerID invoiceAmount
105 30
105 20
105 5
94 8
94 22




Table payment

CustomerID paymentAmount
105 50
105 5
94 10



How can I get this info:


ID totalInvoiceAmount totalPaymentAmount
105 55 55
94 30 10
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26141
    
  66

Peter,
You can do something like this:


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
excellent. Thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SQL statment - issues with SUM
 
Similar Threads
Whats Wrong in my Thread program
Contract money
sql problem - sum is Incorrect
Group & Sum Using XSLT
XML file NOT well-formed in Tomcat 6 same file IS well-formed in Tomcat5.5