MIDRANGE dot COM Mailing List Archive



Home » MIDRANGE-L » September 2003

RE: SQL dividing the sum of two results on a select



fixed

I thought the same thing until I tried

SELECT NBRA, NBRB, NBRC, NBRD, nbra/nbrb, nbrc/nbrd
from rob/davesmith

....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8....+....9....+.
NBRA   NBRB   NBRC   NBRD                        NBRA / NBRB          NBRC 
/ NBRD 
   1      2   3.00   4.00      .5000000000000000000000000000 
.7500000000000000000000000000 

But this gives me:

SELECT NBRA, (sum(nbra)/sum(nbrb))
from rob/davesmith 
group by nbra 
order by nbra 

....+....1....+....2....+....3....+....4....+....
NBRA                          Numeric Expression 
   1                                           0 

However your casting was a good idea:
SELECT NBRA, (float(sum(nbra))/float(sum(nbrb)))
from rob/davesmith 
group by nbra 
order by nbra 
....+....1....+....2....+....3.
NBRA         Numeric Expression
   1    5.0000000000000000E-001

Rob Berendt
-- 
"They that can give up essential liberty to obtain a little temporary 
safety deserve neither liberty nor safety." 
Benjamin Franklin 





"Elvis Budimlic" <ebudimlic@xxxxxxxxxxxxxxxxxxxxxxxxx>
Sent by: midrange-l-bounces@xxxxxxxxxxxx
09/18/2003 12:44 PM
Please respond to Midrange Systems Technical Discussion
 
        To:     <midrange-l@xxxxxxxxxxxx>
        cc: 
        Fax to: 
        Subject:        RE: SQL  dividing the sum of two results on a 
select


Dave,

AFAIK, SQL performs integer division, meaning it rounds to the nearest
integer value.
If your first sum is smaller than the second one, you'll always get a 
result
of zero.

So, check your sum values independently and make sure that's not the case.
Another thing you can try is casting the values within the sum function to
floats, i.e. SUM(FLOAT(field1)).  Division should then return the real
division (with decimal points and such).

Elvis



_______________________________________________
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing 
list
To post a message email: MIDRANGE-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/midrange-l.







Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2014 by MIDRANGE dot COM and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available here. If you have questions about this, please contact