Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the an issue with SQL giving wrong sums with the query,
SQL
select GL, sum(TotalFuel) as "tf" from fueltable1
where [TransDate] > '3/1/2015 12:00:00 AM'
and [TransDate] < '4/1/2015 12:00:00 AM'
group by GL

If I sum the table fueltable1 I get 10405.63, but this results in a total of 9934.36.
Any ideas?
Posted
Comments
PIEBALDconsult 26-Aug-15 14:44pm    
Floating point limitations? What datatype is TotalFuel ?
jhoga 26-Aug-15 14:47pm    
Decimal(18,2)

1 solution

The likeliest reason is that the conditions are eliminating rows from aggregation. For example should either one of the TransDate conditions include equality, for example
SQL
where [TransDate] >= '3/1/2015 12:00:00 AM'
and [TransDate] < '4/1/2015 12:00:00 AM'

Another thing is that you're relying on the implicit date conversion. In worst case dates and months switch places so I'd suggest using CONVERT(date, '3/1/2015', 101) (or whatever is the correct format for you)

If neither of these reveal the reason, then checking the individual rows would be in place. Instead of grouping the data, select the source data, save it to excel and check if there is a difference.
 
Share this answer
 
Comments
jhoga 26-Aug-15 15:04pm    
That did it >= so obvious now. Thanks, you rock Mika
Wendelius 26-Aug-15 15:55pm    
Glad it helped :)
Maciej Los 26-Aug-15 16:10pm    
Hawk eye! +5!
Wendelius 27-Aug-15 1:47am    
Thanks Maciej!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900