Sqlite将时间转换为秒

时间:2016-10-13 06:48:51

标签: sqlite

我有一个生成列Username,TotalPages,TotalTime,AveragePage

的查询
SELECT UserName,TotalPages,TotalTime,cast(strftime('%H',TotalTime)*3600 as integer) as AveragePerPage FROM
(
SELECT UID FROM DPS WHERE UID >0
)a
LEFT JOIN
(
SELECT UID,UserName,COUNT(UserName) as TotalPages,cast((sum(strftime('%s', endtime) - strftime('%s', starttime))/3600)as varchar) || ":" || cast(((sum(strftime('%s', endtime) - strftime('%s', starttime))/60)%60)as varchar) || ":" || cast((sum(strftime('%s', endtime) - strftime('%s', starttime))%60)as varchar) as TotalTime FROM abbottTimer GROUP BY UID
)b ON a.UID = b.UID
GROUP BY a.UID

enter image description here

具有单个H的行会生成空值 但是这个两位数会产生数据。我怎样才能做到这一点。我正在使用sqlite。 编辑: 另外如何计算HH:MM:SS

3 个答案:

答案 0 :(得分:1)

支持的time strings在所有字段中都有两位数字。

内置时间函数无法处理数据库中的值。你必须改变它们。

答案 1 :(得分:0)

SELECT UserName,TotalPages,TotalTime,CAST(sum((TotalTime1/TotalPages)/3600) as varchar) || ":" || CAST(sum(((TotalTime1/TotalPages)/60)%60) as varchar) || ":" || CAST(sum((TotalTime1/TotalPages)%60) as varchar) as Averageperpage
                                  FROM
(
SELECT UID FROM DPS WHERE UID >0
)a
LEFT JOIN
(
SELECT UID,UserName,COUNT(UserName) as TotalPages,cast((sum(strftime('%s', endtime) - strftime('%s', starttime))/3600)as varchar) || ":" || cast(((sum(strftime('%s', endtime) - strftime('%s', starttime))/60)%60)as varchar) || ":" || cast((sum(strftime('%s', endtime) - strftime('%s', starttime))%60)as varchar) as TotalTime,sum(STRFTIME('%s',endtime) - STRFTIME('%s',starttime)) as TotalTime1 FROM abbottTimer GROUP BY UID
)b ON a.UID = b.UID
GROUP BY a.UID

我刚解决了。将整数除以3600小时等等

答案 2 :(得分:-1)

您应该向我们显示数据库中存储的原始TotalTime值。 另外,您是否阅读了sqlite doc about strftime,您可以使用%s修饰符将您的时间作为自19700101以来的秒数返回。

当我处理日期和时间时,我个人使用datetime(<myTime>, 'unixepoch')