问题
I'm using a Firebird database and am trying the following sql but each time it returns 0, instead of 0.61538 (etc).
SELECT (COUNT(myfield)/26) totalcount
FROM mytable
Now when I remove the /26, the totalcount returns 16 as it should. But when I add the divided by 26 back in, the result shows as 0, but it should show as the full decimal value of 0.615384... Does anyone know why it's not returning the full value? I've even tried wrapping it in a CAST((count(myfield)/26) as double) totalcount but it still returns 0.
Thank you in advance for any suggestions!!!!
回答1:
Try:
SELECT COUNT(myfield/26.0) totalcount
FROM mytable
Or:
SELECT COUNT(CAST(myfield as double)/26) totalcount
FROM mytable
Not familiar with Firebird, but in other implementations you have to cast/convert either the numerator or denominator as a decimal before division, as integer division returns an integer value.
来源:https://stackoverflow.com/questions/18987484/sql-force-show-decimal-values