日期范围之间的时间戳

timestamp between date range

我正在尝试使用以下查询查找每天的所有记录数:

select cast(Timestamp_field as date), count(*) as cnt
from table1 
group by 1 
having cast(Timestamp_field as date) between date and date -10;

Timestamp_field 是一个时间戳,我将其投射到最新。这个;尽管 Timestamp_field 的最大值显示 2016-09-20 12:31:38.000000,但 return 没有任何记录。知道为什么吗?

我的猜测是问题出在 between。也许这对你有用:

select cast(Timestamp_field as date), count(*)
from table1
group by 1
having cast(Timestamp_field as date) between date - 10 and date;

对于比较对象,较小的值应该排在第一位。

注意:您应该在 group by 之前进行过滤,而不是在

之后进行过滤
select cast(Timestamp_field as date), count(*)
from table1
where cast(Timestamp_field as date) between date - 10 and date;
group by 1