简体   繁体   English

SQL查询:聚合具有时间戳字段的记录,其中字段时间戳属于相同的时间单位(秒,分钟,小时..)

[英]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.

相关问题 选择 Unix 时间戳落在指定小时或天的位置 - Select where unix timestamp falls on specified hour or day 如何在MYSQL中查询具有相同时间戳的记录? - How can I query records having the same timestamp in MYSQL? 如何获取SQL查询HAVING count显示具有INNER JOIN存在的相同值的BOTH记录 - How to get SQL query HAVING count display BOTH records with the same value where INNER JOIN exists 使用 Scala Slick 从 SQL 数据库中检索所有记录,其中时间戳列的日期在上个月或上周 - Using Scala Slick retrieve all records from SQL db where date from timestamp column falls in the last month or last week SQL时间戳时间 - SQL where time in timestamp SQL:按名称选择记录字段,并选择所有具有相同地址的记录 - SQL: Select a record by name field and all records having the same address 在一个查询中为同一字段选择一个日期和一段时间的时间戳 - Select timestamp for one date and for a period time on the same field in one query 用于查找每个组中成员相同的记录的 SQL 查询 - SQL query for finding records where members within each group are the same SQL查询提取具有相同WHERE条件的硬编码记录和金额 - SQL query pulling hardcoded records and amounts all with the same WHERE conditions SQL查询以基于同一字段和条件中的2个不同值选择记录 - SQL Query to select records based on 2 different values in the same field and a condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM