问题描述
我有一个要求,我需要查询从当前日期到本月前 10 日的数据集。 示例 - 如果日期是今天的 1 月 27 日,那么查询将考虑从 1 月 10 日到 1 月 27 日的数据。
如果当前日期小于本月 10 日,则查询应考虑从上个月 10 日到当前日期的数据。 示例 - 如果日期是今天的 1 月 3 日,则查询应考虑从 12 月 10 日到 1 月 3 日的数据。
select * from my_table
where partition_col >= date_add(current_date,1 - day(current_date)+9)
and partition_col <= current_date;
解决方法
像这样使用 CASE 表达式:
where partition_col >= case when day(current_date)>=10
then date(concat(substr(current_date,1,7),'-10'))
else date(concat(substr(add_months(current_date,-1),'-10'))
end
and partition_col <= current_date;