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

Greenplum日期与周函数迁移至HiveSQL的语法转换咨询

Greenplum日期SQL转Hive SQL适配方案

以下适配逻辑和Greenplum原生逻辑完全对齐,默认周起始为周一,支持yyyy-MM-dd格式的日期参数idate:


适配写法(兼容Hive 2.3及以上版本)

直接复用Hive原生date_trunc函数实现,逻辑和Greenplum一致:

  • 上一周起始日期(周一)
date_sub(date_trunc('week', idate), 7) AS date_from
  • 上一周结束日期(周日)
date_add(date_sub(date_trunc('week', idate),7), 6)
  • 当周起始日期(周一)
date_trunc('week', idate)

低版本Hive兼容写法(无date_trunc函数时使用)

基于next_day函数实现,兼容性更强:

  • 上一周起始日期(周一)
date_sub(next_day(idate, 'MO'), 14) AS date_from
  • 上一周结束日期(周日)
date_sub(next_day(idate, 'MO'), 8)
  • 当周起始日期(周一)
date_sub(next_day(idate, 'MO'), 7)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 18:42:01