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

如何在SQL中设置基于字段的下一年首日筛选条件?

调整日期筛选条件至截止下一年第一天

直接用日期函数构造下一年的第一天,再把筛选条件改成split_start_date小于等于这个日期就能实现需求,以下是不同SQL引擎的具体写法:

  • 通用SQL写法(适配PostgreSQL、Spark SQL等多数引擎):
select * from df
where split_start_date <= date_trunc('year', current_date) + interval '1 year'
  • MySQL专属写法:
select * from df
where split_start_date <= date_add(date_format(current_date(), '%Y-01-01'), interval 1 year)
  • Hive专属写法:
select * from df
where split_start_date <= to_date(concat(year(current_date()) + 1, '-01-01'))

原理很直白:date_trunc('year', current_date)会提取当年的第一天,加1年就得到下一年的第一天;也可以直接拼接下一年份与01-01构造目标日期。用<=该日期作为筛选条件,会自动包含当前及过往所有日期,直到下一年第一天为止,完全匹配你的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 23:25:20