日期范围之间的时间戳

时间:2016-09-20 19:41:09

标签: sql teradata

我试图使用查询找到每天的所有记录数:

 select cast(Timestamp_field as date), count(*) 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,但不会返回任何记录。知道为什么吗?

1 个答案:

答案 0 :(得分:2)

我的猜测是问题是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