Converting Integer Division to Real Float in SQLite
In SQLite, division operations performed on integers result in integer values. This behavior can be problematic when the desired outcome is a floating-point value. To overcome this limitation, a simple solution exists involving typecasting.
Suppose we have the following query that computes the division of two integer counts, totalUsers and totalBids:
sqlite> select totalUsers/totalBids from (select (select count(*) from Bids) as totalBids , (select count(*) from Users) as totalUsers) A; 1
As evident, the result is 1, which is an integer value. To obtain a real number division, we can multiply one of the operands by 1.0, converting it to a floating-point number. This operation modifies the division to floating-point division, yielding the desired result:
SELECT something*1.0/total FROM somewhere
By following this simple technique, you can ensure that division operations in SQLite return real floating-point values, offering more flexibility and accuracy in your data analysis.
The above is the detailed content of How Can I Get Floating-Point Results from Integer Division in SQLite?. For more information, please follow other related articles on the PHP Chinese website!