Add all timestamp data according to date

非 Y 不嫁゛ 提交于 2019-12-24 21:11:48

问题


I have this access_log table

 create_time  | time_spent
--------------+------------
NOV 02, 2017  | 00:00:00
NOV 02, 2017  | 01:45:04
NOV 02, 2017  | 00:00:00
NOV 02, 2017  | 00:00:00
NOV 02, 2017  | 00:39:29
NOV 02, 2017  | 03:05:49
NOV 03, 2017  | 03:58:57
NOV 03, 2017  | 00:52:29
NOV 03, 2017  | 02:53:25

using this table, make looks like this using PostgreSQL. it should add all time_spent column data of respective date

 create_time  | time_spent
--------------+------------
NOV 02, 2017  | 05:30:22
NOV 03, 2017  | 07:44:51

Thank you in advance.


回答1:


a simple group by here:

t=# create table access_log (create_time  date,time_spent time);
CREATE TABLE
t=# copy access_log from stdin delimiter '|';
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>> NOV 02, 2017  | 00:00:00
NOV 02, 2017  | 01:45:04
NOV 02, 2017  | 00:00:00
NOV 02, 2017  | 00:00:00
NOV 02, 2017  | 00:39:29
NOV 02, 2017  | 03:05:49
NOV 03, 2017  | 03:58:57
NOV 03, 2017  | 00:52:29
NOV 03, 2017  | 02:53:25>> >> >> >> >> >> >> >>
>> \.
t=# select sum(time_spent), create_time from access_log group by create_time;
   sum    | create_time
----------+-------------
 05:30:22 | 2017-11-02
 07:44:51 | 2017-11-03
(2 rows)


来源:https://stackoverflow.com/questions/47091785/add-all-timestamp-data-according-to-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!