类型转换问题

发布于 2024-11-09 02:43:31 字数 369 浏览 0 评论 0原文

在下面的查询中,1/2 的答案始终为零,而它应该是 0.5。你能告诉我如何将两个 int 列转换为 float/double 吗?

SELECT 1/2, total, sb_ondate, likes
  FROM (
      SELECT sb_ondate, COUNT(*) AS total,
             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;

谢谢。

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 total,
             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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月如刀 2024-11-16 02:43:31

将两个除数之一转换为实数:

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

交互式 Python 会话示例:

>>> 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,)

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,)
心凉 2024-11-16 02:43:31

.0 添加到常量之一:

sqlite> select 1/2;
1/2
----------
0
sqlite> select 1.0/2;
1.0/2
----------
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文