[英]Python Count number of records within a given date range
We have a backend table that stores details of transaction including seconds since epoch. 我们有一个后端表,用于存储交易详细信息,包括自纪元以来的秒数。 I am creating a UI where I collect from-to dates to display counts of transaction occurred in-between the dates. 我正在创建一个UI,在其中收集从开始日期到日期之间的交易次数。
Assuming that the date range is from 07/01/2012 - 07/30/2012, I am unable to establish a logic that will increment a counter for records that happened within the time period. 假设日期范围是从07/01/2012-07/30/2012,我将无法建立一种逻辑来增加该时间段内发生的记录的计数器。 I should hit the DB only once as hitting for each day will give poor performance. 我应该只打一次数据库,因为每天打一次会导致性能下降。
I am stuck at a logic: 我陷入了逻辑:
Convert 07/01/2012 & 07/30/2012 to seconds since epoch.
Get the records for start date - end date [as converted to seconds since epoch]
For each record get the month / date
-- now how will we add counters for each date in between 07/01/2012 - 07/30/2012
MySQL has the function FROM_UNIXTIME
which will convert your seconds since epoch into datetime and you can then extract the DATE
part of it ( YYYY-MM-DD
format) and group according to it. MySQL具有FROM_UNIXTIME
函数,它将把从纪元以来的秒数转换为日期时间,然后您可以提取它的DATE
部分( YYYY-MM-DD
格式)并根据它进行分组。
SELECT DATE(FROM_UNIXTIME(timestamp_column)), COUNT(*)
FROM table_name
GROUP BY DATE(FROM_UNIXTIME(timestamp_column))
This will return something like 这将返回类似
2012-07-01 2
2012-07-03 4
…
(no entries for days without transactions) (几天内没有交易的无条目)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.