如何在配置单元sql中获取季度结束日期?

brqmpdu1  于 2021-06-26  发布在  Hive
关注(0)|答案(2)|浏览(272)

我很难从配置单元sql中获取季度最新值。如何在配置单元sql中获取当前季度的第一天?
表名:订单
字段:日期、订单号、销售号
请告知。

4si2a6ki

4si2a6ki1#

如果不能使用dudu建议的各种date函数,则可以将其转换为字符串,解析出月份,并使用case语句。根据您的配置单元版本,您可能需要使用简单的案例而不是搜索。
(假设yyyy-mm-dd)

case cast (substring (cast(<date field> as varchar(10)),6,2) as integer)
when between 1 and 3 then 1
when between 4 and 6 then 2
when between 7 and 9 then 3
else 4
end

很难看,但应该管用。

q0qdq0h2

q0qdq0h22#

这似乎是最干净的方法

with t as (select date '2016-08-27' as dt) 
select add_months(trunc(dt,'MM'),-(month(dt)-1)%3) from t
;

2016-07-01
这里还有两个选择

with t as (select date '2016-08-27' as dt) 
select  trunc(add_months(dt,-(month(dt)-1)%3),'MM') 
from    t
;

2016-07-01

with t as (select date '2016-08-27' as dt) 
select  add_months(trunc(dt,'YY'),cast((month(dt)-1) div 3 * 3 as INT)) 
from    t
;

2016-07-01
对于早期版本

with t as (select '2016-08-27' as dt)
select  printf('%04d-%02d-%02d',year(dt),(((month(dt)-1) div 3) * 3) + 1,1)
from    t

2016-07-01
同样,但今天

with t as (select from_unixtime(unix_timestamp(),'yyyy-MM-dd') as today)
select  printf('%04d-%02d-%02d',year(today),(((month(today)-1) div 3) * 3) + 1,1) as 
from    t

2017-04-01

相关问题