Toggle navigation
PostgreSQL中文社区
首页
(current)
社区新闻
中文文档
加入ACE
相关资料
了解PostgreSQL
PostgreSQL相关文档
PostgreSQL软件下载
PostgreSQL中文图书
社区年会PPT资料
关于中文社区
注册
登录
全部
Bug
使用技巧
内容问题
建议
系统安装
集群复制
其他
首页
有问有答(FAQ)
【%E5%85%B6%E4%BB%96】
类主题列表
qsding
多条记录中的结束时间减去开始时间求平均,结果小时部分大于24小时 ...
... 2019-06-05 10:52:40+08...1楼
### 多条记录中的结束时间减去开始时间求平均,得出的结果在小时部分大于24小时?不知道是不是pg的bug,还是其他什么问题,望社区知晓的小伙伴解答。 #### 1.情况:有多条记录,记录中包含了开始时间和结束时间。需求规定,结束时间 - 开始时间 = 处理时长。 现有一需求,想求出多条记录的平均处理时长,根据处理时长的定义,sql如下: ``` select avg(end_time - start_time) from t_time; ``` 查询出来的结果 ``` 13 days 28:17:58.612433 ``` #### 2.部分记录如下: ``` -- 创建表 CREATE TABLE public.t_time ( id serial NOT NULL, start_time timestamp NULL, end_time timestamp NULL ); ``` ``` INSERT INTO public.t_time (start_time,end_time) VALUES ('2019-04-28 12:24:45.242','2019-05-09 14:31:38.957') ,('2018-12-05 10:27:03.271','2018-12-29 16:52:12.426') ,('2018-11-05 09:25:17.749','2018-12-05 11:27:39.147') ,('2019-03-15 10:37:43.136','2019-03-18 16:54:03.725') ,('2018-12-27 13:23:12.029','2018-12-28 10:17:02.454') ,('2019-03-30 15:27:18.241','2019-04-01 16:52:06.081') ,('2019-03-07 11:20:27.551','2019-03-18 09:40:46.139') ,('2019-02-15 11:06:56.692','2019-02-18 09:09:27.865') ,('2019-03-14 10:38:17.774','2019-03-26 16:39:32.044') ,('2019-03-15 15:20:33.018','2019-03-18 13:14:52.815') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2018-11-30 11:16:53.703','2018-12-10 10:17:01.862') ,('2019-02-20 14:16:56.618','2019-02-28 09:32:26.009') ,('2018-11-05 10:48:04.424','2019-05-21 14:05:13.164') ,('2019-04-22 16:06:48.269','2019-04-25 14:44:45.786') ,('2019-04-22 09:20:29.057','2019-04-30 09:10:23.013') ,('2018-12-13 09:44:16.813','2019-01-23 13:53:15.876') ,('2019-01-30 15:41:30.471','2019-05-06 13:14:55.734') ,('2019-01-23 16:44:22.062','2019-01-30 15:39:54.144') ,('2019-01-02 16:17:12.719','2019-01-04 09:49:21.672') ,('2018-12-29 10:23:21.235','2019-01-02 15:05:12.914') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2018-12-29 15:51:00.177','2019-01-04 09:55:33.510') ,('2019-04-01 10:21:06.984','2019-04-08 15:10:02.864') ,('2019-03-07 13:18:03.339','2019-03-08 09:57:46.142') ,('2019-01-08 17:00:07.192','2019-01-15 17:23:22.873') ,('2018-10-10 09:04:38.811','2019-01-11 13:57:21.578') ,('2019-04-03 14:23:35.889','2019-05-28 13:22:44.564') ,('2019-01-10 13:55:33.883','2019-01-15 17:22:43.741') ,('2019-04-03 10:05:53.078','2019-04-03 10:51:28.727') ,('2019-04-04 13:40:56.332','2019-04-10 09:50:09.692') ,('2019-04-28 14:21:54.742','2019-04-28 14:31:39.969') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2019-04-28 08:44:46.311','2019-04-28 10:40:45.619') ,('2019-03-12 11:27:23.085','2019-03-18 13:15:09.550') ,('2018-12-18 09:45:10.209','2018-12-25 16:49:13.994') ,('2019-01-22 17:21:47.208','2019-02-21 10:32:33.717') ,('2019-03-18 15:00:12.080','2019-03-21 09:10:29.552') ,('2019-04-08 14:16:25.131','2019-04-08 15:10:48.196') ,('2018-09-04 15:56:20.099','2018-12-05 17:10:44.443') ,('2019-03-07 13:38:53.582','2019-03-07 16:42:21.292') ,('2018-12-24 09:42:20.241','2018-12-29 11:20:22.956') ,('2019-02-15 15:47:10.626','2019-05-06 13:20:11.539') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2019-02-11 10:45:11.184','2019-02-11 16:15:08.936') ,('2019-01-31 12:08:08.543','2019-02-03 11:25:26.929') ,('2019-01-22 16:22:41.952','2019-01-31 12:04:45.265') ,('2018-10-31 14:55:32.908','2018-12-03 10:06:10.671') ,('2019-03-19 11:18:53.935','2019-03-21 16:58:39.957') ,('2019-05-06 09:54:48.549','2019-05-20 09:53:17.532') ,('2018-10-31 12:11:39.510','2019-01-15 09:54:07.693') ,('2019-01-02 16:17:56.673','2019-01-04 09:54:31.466') ,('2019-04-17 11:13:17.360','2019-04-26 08:49:26.518') ,('2018-10-16 17:13:09.093','2018-11-22 11:14:57.800') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2019-03-23 12:52:28.928','2019-03-28 10:27:13.307') ,('2019-03-29 13:59:49.174','2019-04-02 10:31:21.549') ,('2019-02-11 15:50:47.408','2019-02-18 11:21:49.708') ,('2019-05-06 09:46:33.074','2019-05-10 15:51:27.030') ,('2019-03-13 15:02:11.468','2019-03-26 16:39:41.604') ,('2019-05-13 11:29:52.379','2019-05-20 14:57:44.867') ,('2019-01-25 11:06:20.523','2019-01-28 13:06:28.683') ,('2018-12-03 09:53:18.368','2019-01-10 15:19:57.291') ,('2019-04-03 14:02:39.390','2019-04-08 15:08:18.371') ,('2018-11-21 10:21:41.330','2019-01-21 16:23:27.779') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2019-02-11 10:44:37.698','2019-02-14 09:43:48.886') ,('2019-01-21 14:08:12.481','2019-01-28 09:33:03.769') ,('2019-01-28 15:31:10.466','2019-01-28 15:45:50.290') ,('2018-11-06 15:52:25.064','2019-01-03 13:56:03.338') ,('2019-05-17 16:18:00.532','2019-05-27 10:40:55.572') ,('2018-12-29 15:37:59.230','2019-01-09 12:07:09.134') ,('2019-04-18 16:39:27.697','2019-04-24 09:11:12.952') ,('2019-04-25 11:24:24.994','2019-05-20 09:59:12.825') ,('2019-02-14 10:27:04.542','2019-02-28 09:32:16.772') ,('2019-04-30 16:56:41.511','2019-05-06 11:37:15.165') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2019-04-28 15:26:59.051','2019-04-28 15:51:00.038') ,('2019-05-13 09:47:34.497','2019-05-13 10:02:41.675') ,('2019-05-14 10:13:41.270','2019-05-20 15:00:25.760') ,('2018-12-28 16:53:00.906','2019-01-02 15:06:57.240') ,('2018-11-08 17:05:39.934','2019-01-09 14:00:37.045') ,('2019-05-16 13:51:36.859','2019-05-28 09:02:21.600') ,('2019-01-02 16:57:12.528','2019-01-07 14:18:40.871') ,('2019-01-02 16:17:37.475','2019-01-04 09:58:30.339') ,('2018-12-13 14:14:15.812','2018-12-17 10:47:54.232') ,('2019-01-21 14:06:42.111','2019-01-28 09:32:43.442') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2018-12-04 14:43:16.187','2018-12-12 10:56:28.200') ,('2018-11-30 11:11:00.178','2018-12-04 09:55:09.930') ,('2019-04-08 10:06:42.505','2019-04-08 15:11:05.437') ,('2019-04-03 13:19:11.091','2019-04-04 14:26:21.309') ,('2019-04-02 16:09:30.910','2019-04-02 16:34:48.102') ,('2019-01-21 15:59:14.519','2019-01-28 10:26:19.801') ,('2019-04-17 15:58:11.197','2019-04-23 09:14:44.130') ,('2019-04-08 14:18:06.624','2019-04-10 09:48:52.722') ,('2019-05-07 10:57:17.172','2019-05-10 15:52:09.696') ,('2019-05-15 17:01:06.043','2019-05-20 14:59:49.968') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2019-05-10 10:06:05.502','2019-05-15 16:45:22.483') ,('2019-04-28 09:35:39.495','2019-05-10 15:52:51.878') ,('2018-12-06 11:37:32.646','2018-12-12 10:55:59.221') ,('2018-10-11 09:53:04.361','2018-10-22 11:00:25.920') ,('2018-11-23 14:26:11.463','2019-01-11 13:50:09.824') ,('2019-03-13 14:35:42.225','2019-03-19 10:06:30.510') ,('2019-01-11 14:41:29.726','2019-01-21 09:14:23.108') ,('2019-03-07 13:15:50.267','2019-03-08 09:56:07.233') ,('2019-03-13 14:39:47.052','2019-03-26 10:10:56.915') ,('2018-11-13 09:25:28.184','2018-11-13 16:12:05.044') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2018-11-20 10:41:27.587','2018-11-20 11:20:48.069') ,('2018-11-15 16:19:46.201','2018-11-16 13:35:34.645') ,('2019-01-02 14:44:25.264','2019-01-24 14:33:53.560') ,('2019-02-20 14:26:20.492','2019-02-28 09:31:24.054') ,('2019-04-01 16:50:06.235','2019-04-03 15:53:01.249') ,('2019-01-31 16:49:44.250','2019-02-01 09:03:56.006') ,('2019-01-23 13:21:17.185','2019-01-31 13:22:04.103') ,('2019-04-18 11:32:40.105','2019-04-23 09:08:56.672') ,('2019-04-03 14:07:34.396','2019-04-08 15:10:18.123') ,('2018-12-24 09:39:32.179','2019-03-13 13:13:29.183') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2019-04-18 11:32:57.213','2019-04-22 10:29:58.972') ,('2019-02-27 10:50:22.613','2019-02-27 11:07:40.787') ,('2019-02-27 17:12:20.527','2019-02-28 09:07:22.976') ,('2019-03-27 14:52:35.404','2019-04-08 14:37:14.708') ,('2018-11-19 11:10:04.113','2019-01-11 13:55:39.854') ,('2018-11-23 14:25:15.913','2018-12-05 11:25:42.214') ,('2018-12-07 11:15:37.545','2018-12-12 09:39:50.625') ,('2019-01-02 16:47:50.690','2019-01-02 17:00:05.285') ,('2018-12-25 14:14:07.254','2019-01-08 11:10:22.643') ,('2019-03-01 10:34:10.816','2019-03-18 14:50:31.668') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2019-03-01 15:26:25.537','2019-03-04 09:12:43.149') ,('2019-03-12 11:01:12.166','2019-03-15 16:14:18.023') ,('2019-05-13 13:12:06.291','2019-05-13 14:08:59.300') ,('2019-02-26 10:15:24.899','2019-03-07 09:20:18.888') ,('2019-05-06 14:25:44.141','2019-05-20 09:47:05.056') ,('2019-03-25 11:22:50.533','2019-04-25 14:26:32.319') ,('2019-04-26 11:59:55.586','2019-05-08 09:53:03.623') ,('2018-10-11 16:32:26.049','2019-03-01 14:08:50.641') ,('2019-04-04 13:45:29.119','2019-04-08 15:09:28.020') ,('2019-04-26 17:30:14.544','2019-05-06 16:10:33.995') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2019-05-16 15:14:59.977','2019-05-24 11:06:18.047') ,('2018-12-03 16:30:44.560','2018-12-10 10:17:38.009') ,('2019-02-14 13:25:30.956','2019-02-21 10:32:13.369') ,('2019-03-13 14:06:16.585','2019-03-18 16:54:32.069') ,('2019-01-11 15:57:00.447','2019-02-01 09:41:26.844') ,('2019-01-02 16:16:54.628','2019-01-04 09:48:39.755') ,('2019-05-09 14:34:52.227','2019-05-15 16:46:03.031') ,('2019-01-09 15:55:50.055','2019-01-14 16:28:21.730') ,('2019-04-19 09:16:00.684','2019-05-20 09:58:55.784') ,('2018-12-04 14:24:20.131','2018-12-10 12:06:49.369') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2019-04-01 09:55:34.102','2019-04-03 15:13:33.934') ,('2019-05-23 17:54:19.765','2019-05-28 08:59:05.284') ,('2018-12-25 11:28:27.382','2019-01-15 17:36:04.015') ,('2019-01-02 16:38:20.521','2019-01-03 11:06:29.091') ,('2019-05-06 15:38:28.484','2019-05-10 15:51:50.617') ,('2019-02-27 16:30:18.896','2019-03-04 09:19:34.410') ,('2019-05-14 19:17:52.719','2019-05-20 14:57:20.063') ,('2018-09-03 14:23:26.278','2018-09-04 16:08:21.387') ,('2019-05-09 14:36:43.730','2019-05-15 16:48:13.464') ,('2019-03-13 14:38:15.852','2019-03-19 10:06:37.597') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2018-12-28 16:53:24.606','2019-01-02 16:17:19.193') ,('2019-01-07 10:51:24.643','2019-01-07 11:17:21.051') ,('2019-05-08 10:42:18.397','2019-05-13 17:25:13.140') ,('2019-01-24 09:26:12.892','2019-01-25 13:33:16.357') ,('2019-01-04 16:07:51.112','2019-01-09 12:06:44.307') ,('2019-04-22 10:56:04.211','2019-05-20 09:59:26.145') ,('2019-03-03 10:20:13.691','2019-03-04 15:23:05.369') ,('2019-03-12 10:13:01.361','2019-03-13 11:25:24.909') ,('2019-03-11 10:24:03.573','2019-03-11 13:34:17.333') ,('2019-04-26 13:31:13.561','2019-05-09 09:57:09.308') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2019-03-01 10:31:30.142','2019-03-01 13:58:09.079') ,('2018-11-23 14:26:53.648','2018-12-05 11:22:42.227') ,('2019-01-10 13:57:02.108','2019-01-15 17:23:00.322') ,('2018-09-14 15:13:47.926','2018-10-12 16:40:03.963') ,('2019-05-20 13:51:27.664','2019-05-22 10:03:49.660') ,('2019-03-07 13:21:58.659','2019-03-07 16:42:13.671') ,('2019-01-21 14:20:04.803','2019-01-28 10:26:36.961') ,('2019-03-23 12:51:36.298','2019-05-20 10:38:08.198') ,('2019-02-28 16:37:12.744','2019-03-19 16:29:52.103') ,('2019-02-15 16:19:51.481','2019-03-07 09:20:03.993') ; INSERT INTO public.t_time (start_time,end_time) VALUES ('2019-01-02 16:57:57.400','2019-01-03 11:05:54.484') ,('2019-01-14 16:04:59.395','2019-01-17 15:59:15.504') ,('2018-12-18 09:58:04.857','2018-12-28 10:11:25.543') ; ```
我的签名:
qsding
回复: ...
... 2019-06-05 11:11:23+08...2楼
猜测,下面sql查询出来每条记录的处理时长 ``` select (end_time - start_time) as interval_time from t_time; ``` ``` -- 每条记录的处理时长 11 days 02:06:53.715 24 days 06:25:09.155 30 days 02:02:21.398 3 days 06:16:20.589 20:53:50.425 2 days 01:24:47.84 10 days 22:20:18.588 2 days 22:02:31.173 12 days 06:01:14.27 2 days 21:54:19.797 ... ``` 求总的处理时长 ``` select sum(end_time - start_time) as interval_time from t_time; -- 结果是:2360 days 2231:49:59.951 ``` 从总的处理时长来看,结果中的小时部分2231已经大于24小时。而通过只相加每条记录的处理时长中的时间部分,得出的结果刚好是 2231:49:59.95。 因此大胆猜测,postgresql在处理时间的时候天数是直接相加的,而时间部分仅对小时以下的单位进行了处理,即分钟、秒、毫秒等满足进制的时候会向上添加,而小时则不会因大于24小时就对天数+1. 下面是具体去了两条数据做验证。 取记录中的172和169做验证。 ``` -- 执行插入语句后,自动生成的id取172和169 select (end_time - start_time) as interval_time from t_time where id in( 172,169); -- 169的处理时长 -- 18 days 23:52:39.359 -- 172的处理时长 -- 2 days 23:54:16.109 -- 172和169的处理时长和 select sum(end_time - start_time) as interval_time from t_time where id in( 172,169); -- 20 days 47:46:55.468 = 18 days 23:52:39.359 + 2 days 23:54:16.109 -- 172和169的平均处理时长 select avg(end_time - start_time) as interval_time from t_time where id in( 172,169); -- 10 days 23:53:27.734 = 20 days 47:46:55.468 / 2 ```
我的签名:
您还没有登录,请您登录后再发表回复
© 2010 PostgreSQL中文社区