SQL-julianday函数的正确日期格式是什么?

问题描述 投票:1回答:2

因此,我正在尝试查询数据库,并指出每个用户要计算的总小时数,以便确定工资。我应该提供该日期,该日期结束的那一周以及前一个月结束的总时间在那个日期。

我有三个查询,但它们未返回任何内容:

SELECT username,SUM((julianday(end_time)-julianday(start_time))*24) as duration from Session where start_time<=DATE('2019-07-20','+1 day') AND start_time>=DATE('2019-07-20','0 day') GROUP BY username

SELECT username,SUM((julianday(end_time)-julianday(start_time))*24) as duration from Session where start_time<=DATE('2019-07-20','+1 day') AND start_time>=DATE('2019-07-20','-7 day') GROUP BY username

SELECT username,SUM((julianday(end_time)-julianday(start_time))*24) as duration from Session where start_time<=DATE('2019-07-20','+1 day') AND start_time>=DATE('2019-07-20','-30 day') GROUP BY username

日期有效,并且已经存在于数据库中。有人知道发生了什么吗?

数据库中的数据:Dataset

+----------+----------------------+--------------------+--------------------+
| Username | Session_ID           | Start_time         | End_time           |
+----------+----------------------+--------------------+--------------------+
| test9    | X7kP0ARulnaWaiXG2WlH | 2019-7-20 08:54:00 | 2019-7-20 11:57:00 |
+----------+----------------------+--------------------+--------------------+
| test4    | piS6GWIzYUxsv8ibIJWP | 2019-7-20 08:51:00 | 2019-7-20 10:51:00 |
+----------+----------------------+--------------------+--------------------+
| test1    | MVYKrORQseOzhrYfXl3V | 2019-7-20 08:50:00 | 2019-7-20 11:42:00 |
+----------+----------------------+--------------------+--------------------+
| test5    | 6FjiFHK6PHD8_5gKqNPg | 2019-7-20 08:48:00 | 2019-7-20 11:53:00 |
+----------+----------------------+--------------------+--------------------+
| test3    | mdKL06e_5Zt9ISMyPQ3h | 2019-7-20 08:43:00 | 2019-7-20 10:55:00 |
+----------+----------------------+--------------------+--------------------+
| test6    | zIlMPQ8kHw094HJHy6zS | 2019-7-20 08:39:00 | 2019-7-20 12:31:00 |
+----------+----------------------+--------------------+--------------------+
| test2    | rouH_1jFFwdW0RSUP9Nf | 2019-7-20 08:35:00 | 2019-7-20 11:15:00 |
+----------+----------------------+--------------------+--------------------+
| test8    | v8EFrHNchNr156XaOmRk | 2019-7-20 08:23:00 | 2019-7-20 12:06:00 |
+----------+----------------------+--------------------+--------------------+
| test7    | 3rkN1fEx9S6yhx8HaIsX | 2019-7-20 08:19:00 | 2019-7-20 11:57:00 |
+----------+----------------------+--------------------+--------------------+
| test10   | yjslD9UydwMAr0OL-gJ- | 2019-7-20 08:05:00 | 2019-7-20 10:47:00 |
+----------+----------------------+--------------------+--------------------+

预期输出:

enter image description here

结果:enter image description here

在将更多数据集添加到数据库后,它已停止工作。在它有大约26行之前,它一直在工作:

When it was working

这可能是原因?:在我的日期和时间格式不同之前:例如-2019-12-02 19:26:18但现在它是这样的:例如-2019-6-1 08:44:00(所有秒00)

[<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9Hbm43YS5wbmcifQ==” alt =“之前和之后的数据库”> 5

更新:已修复,更改了日期格式并重新填充了日期库,再次运行了查询,它现在开始运行。

sqlite
2个回答
1
投票

我相信您的问题是日期不是公认的格式,因此在使用julianday函数时,它们将导致空值。也就是说,当月值小于10时,它是个位数,而不是用0填充,因此该数据应为2019-07-20,以供julianday函数使用,而不是2019-7-20。


0
投票

假设您的结束时间和开始时间在一个时区,以及开始时间和结束时间的差异(以秒为单位),您可以这样做。

© www.soinside.com 2019 - 2024. All rights reserved.