File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes sql problem - sum is Incorrect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "sql problem - sum is Incorrect" Watch "sql problem - sum is Incorrect" New topic

sql problem - sum is Incorrect

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
can anyone show me the light here.

this statment produce the wrong sum (chgTotal) SUM(unit * charge)
- it duplicates the sum for some reason. why?

Paul Clapham

Joined: Oct 14, 2005
Posts: 19322

Then the query without the GROUP BY clause returns twice as many rows as you think it should. So you need more criteria in your WHERE clause to stop that from happening. Run the query without GROUP BY (and without SUM and MIN and MAX) and maybe you'll see why it is producing the duplicate records.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19899

Probably in your join, you have two records of one table mapping to one record of the other table. The join creates two records in the result set, even though there is only one record in the one table.

How To Ask Questions How To Answer Questions
It is sorta covered in the JavaRanch Style Guide.
subject: sql problem - sum is Incorrect