You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Redshift查询含当日的近30天数据问题:BETWEEN语句未返回今日数据

Redshift查询包含当日的近30天数据解决方案

你的查询未包含当日数据的原因是:current_date返回的是当日的日期值(例如2024-05-20),当与timestamp without timezone类型的date_column比较时,会自动转换为当日00:00:00的时间戳。因此BETWEEN ... AND current_date的范围只覆盖到前一天的23:59:59,当日产生的时间戳数据会被排除在外。

可以通过以下两种方式调整查询:

方法一:扩展结束时间范围

将结束条件改为current_date + INTERVAL '1 day',让范围覆盖到次日00:00:00,这样就能包含当日所有时间的数据:

select * 
from mytable  
WHERE date_column BETWEEN current_date - INTERVAL '30 day' AND current_date + INTERVAL '1 day'
order by date_column desc;

方法二:截断时间戳到日期维度

使用DATE_TRUNC函数将date_column截断为日期类型,忽略时间部分,只要日期在近30天(含当日)范围内就会被筛选:

select * 
from mytable  
WHERE DATE_TRUNC('day', date_column) BETWEEN current_date - INTERVAL '30 day' AND current_date
order by date_column desc;

内容的提问来源于stack exchange,提问作者Alagusundaram R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 01:20:21