aspose file tools*
The moose likes JDBC 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

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
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: 18659

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: 19720

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
I agree. Here's the link:
subject: sql problem - sum is Incorrect