如何在 Hive SQL 中获取季度至今?

How can I get Quarter to Date in Hive SQL?

我很难从 Hive SQL 获取季度至今的值。 如何在 Hive sql 中获取当前季度的第一天?

Table 姓名:订单

字段:日期,order_num,sales_num

请指教。

这似乎是最干净的方式

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


还有2个选项

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

如果你不能使用 Dudu 建议的各种日期函数,你总是可以将其转换为字符串,解析出月份,然后使用 case 语句。根据您的 Hive 版本,您可能必须使用简单的案例而不是搜索。

(假设 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

丑陋,但应该可以。