在HIVE中选择每月的第一天 [英] Selecting the first day of the month in HIVE
本文介绍了在HIVE中选择每月的第一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Hive(与SQL相似,但是对于SQL用户,语法可能没有什么不同).我看了其他的stackoverflow,但是它们似乎在SQL中具有不同的语法.
I am using Hive (which is similar to SQL, but the syntax can be little different for the SQL users). I have looked at the other stackoverflow, but they seems to be in the SQL with different syntax.
我正在尝试通过此查询获取每月的第一天.这给了我今天的一天.例如,如果今天是2015-04-30,则结果将是2015-04-01.谢谢!
I am trying to the get the first day of the month through this query. This one gives me today's day. For example, if today is 2015-04-30, then result would be 2015-04-01. Thanks!
select
cust_id,
FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd') as first_day_of_month_transaction
--DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) as first_day_of_month_transaction --SQL format. Not compatible in Hive.
from
customers;
推荐答案
要获取每月的第一天,可以使用:
To get the first day of the month, you can use:
date_add(<date>,
1 - day(<date>) )
应用于您的表情:
date_add(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'),
1 - day(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'))
)
但这将适用于任何格式正确的列.
But this will work for any column in the right format.
这篇关于在HIVE中选择每月的第一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文