开发者

Type-casting issue

In the following query, the answer of 1/2 is always zero while it should be 0.5. Can you please tell me how to convert the two int columns into float/double?

SELECT 1/2, total, sb_ondate, likes
  FROM (
      SELECT sb_ondate, COUNT(*) AS t开发者_开发问答otal,
             STRFTIME("%w",sb_ondate) AS weekDay,
             COUNT(CASE WHEN sb_reaction = 'like' THEN sb_id END) AS likes
        FROM diet
    GROUP BY weekDay) AS f;

Thanks.


Cast one of the two divisors as a real:

SELECT 1 / CAST(2 AS REAL),...

Sample interactive Python session:

>>> import sqlite3
>>> c = sqlite3.connect(':memory:')
>>> c.execute("create table t (a int,b int);")
<sqlite3.Cursor object at 0x7f9b0e9539d0>
>>> c.execute("insert into t values (1,2)")
<sqlite3.Cursor object at 0x7f9b0e953ad8>
>>> conn.execute("select a / cast(b as real) from t").fetchone()
(0.5,)


Add .0 to one of the constants:

sqlite> select 1/2;
1/2
----------
0
sqlite> select 1.0/2;
1.0/2
----------
0.5
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜