conversión de int a real en sqlite


La división en sqlite devuelve un valor entero

sqlite> select totalUsers/totalBids from 
(select (select count(*) from Bids) as totalBids , 
(select count(*) from Users) as totalUsers) A;
1

¿Podemos encasillar el resultado para obtener el valor real del resultado de división?

Author: Ken White, 2011-11-29

4 answers

Simplemente multiplique uno de los números por 1.0:

SELECT something*1.0/total FROM somewhere

Eso le dará división en coma flotante en lugar de división entera.

 100
Author: NullUserException,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-29 03:53:20

En Sqlite la división de un entero por otro entero siempre redondee al entero más cercano.

Por lo tanto, si lanzas tu enumerador a un flotador:

SELECT CAST(field1 AS FLOAT) / field2
 39
Author: Adam Garner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-02 10:08:38
select cast ( ( select 1 ) as real );

Https://www.sqlite.org/lang_expr.html#castexpr

 7
Author: mpb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-31 17:18:36

O si desea actualizar la columna basada en la columna de texto:

UPDATE table_with_fields SET real_field=cast(field_with_txt AS real)
 1
Author: Greg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-13 10:24:59