[英]SQL query : aggregating records having a timestamped field, where fields timestamp falls in same time unit (sec, min, hour..)
I have a database table customers
that records among others the arrival time of clients for a one month data.我有一个数据库表
customers
,其中记录了一个月数据的客户到达时间。 So, the customers
table contains among other a column named arrival_time
.因此,
customers
表包含一个名为arrival_time
的列。 (eg, arrival_time = 12/1/2020 12:01:39 AM, arrival_time = 12/1/2020 12:01:34 AM etc…) Is it possible to design/write an SQL query that returns the numbers of customers that arrived each second (or say, min, hour...) in this this one month data. (例如,arrival_time = 12/1/2020 12:01:39 AM,arrival_time = 12/1/2020 12:01:34 AM 等等……)是否可以设计/编写一个 SQL 查询来返回客户数量在这一个月的数据中,每一秒(或者说,分钟,小时......)到达。
Thank you.谢谢你。
If you are using PostgreSQL you can use DATE_TRUNC()
.如果您使用的是 PostgreSQL,则可以使用
DATE_TRUNC()
。 For example:例如:
select
date_trunc('minute', arrival_time) as at,
count(distinct customer_id) as cnt
from t
group by date_trunc('minute', arrival_time)
You can change 'minute'
for 'day'
, 'week'
, 'second'
, etc. See https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC您可以将
'minute'
更改为'day'
、 'week'
、 'second'
等。请参阅https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.