简体   繁体   English

更改选择查询中的计算字段的类型(sqlite)

[英]Change type of calculated field in select query (sqlite)

im sure i am not the first one to ask this but i can't find the answer to this: I haver a select query on a datatable in a sqlite database. 我确定我不是第一个问这个问题的人,但是我找不到这个问题的答案:我在sqlite数据库中的数据表上有一个选择查询。

select *, ((int_EndTime)-(int_StartTime))/60 as dou_usage_min FROM tbl_unautho_usage;

when i run this i get all the fields from the datatable including a new column calculated from to integer columns with unix time stamp values. 当我运行此命令时,我会从数据表中获取所有字段,包括从新列计算到具有unix时间戳值的整数列。 However, i want my calculated column to be of the type double. 但是,我希望计算列的类型为double。 With the query above i get a type integer. 通过上面的查询,我得到一个整数类型。

select *, ((int_EndTime as float)-(int_StartTime as float))/60 as dou_usage_min FROM tbl_unautho_usage;

Afterwards I tried to change the column type of my integer-columns to float, but this gies me the following error: 之后,我尝试将整数列的列类型更改为float,但这会给我带来以下错误:

near "as": syntax error: 

i got the idea for that from the following post: How to cast computed column with correct decimal/$ result 我从以下帖子中得到了这个主意: 如何使用正确的十进制/ $结果强制转换计算列

Try multiplying a value used within the arithmetic operation by 1.0 . 尝试将算术运算中使用的值乘以1.0

select 
  *, 
  ((int_EndTime*1.0)-(int_StartTime*1.0))/60 as dou_usage_min 
FROM tbl_unautho_usage;

Probably only one value multiplied will be sufficient. 可能只有一个值乘以就足够了。

The correct syntax of a CAST expression is "CAST( something AS type )". CAST表达式的正确语法是“ CAST( 某类 AS 类型 )”。

But in this case, for the division to be done with floating-point numbers, it is sufficient for at least one of the operands to be a floating-point number: 但是在这种情况下,对于要用浮点数进行除法,至少一个操作数是浮点数就足够了:

SELECT *, (int_EndTime - int_StartTime) / 60.0 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM