gpt4 book ai didi

sqlite - 类型转换问题

转载 作者:IT王子 更新时间:2023-10-29 06:22:08 25 4
gpt4 key购买 nike

在下面的查询中,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;

谢谢。

最佳答案

Cast两个除数之一作为 real :

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

示例交互式 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,)

关于sqlite - 类型转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6085424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com