周、月、季度和年的 Impala 日期

Posted

技术标签:

【中文标题】周、月、季度和年的 Impala 日期【英文标题】:Impala Dates for Weeks, Months, Quarters, & Years 【发布时间】:2019-01-08 01:14:26 【问题描述】:

我想在 Impala 查询中获取以下日期:

a. Sunday to Saturday Week (SSW):
1. First and Last day of Current week (SSW) 
2. First and Last day of Previous week(SSW)

b. Monday to Sunday Week (MSW):
1. First and Last day of Current week (MSW)
2. First and Last day of Previous week(MSW)

c. Month:
1. First and Last Day of Current Month 
2. First and Last Day of Previous Month 

d. Quarter:
1. First and Last Day of Current Quarter 
2. First and Last Day of Previous Quarter

e. Year:
1. First and Last Day of Current Year 
2. First and Last Day of Previous Year 

这是我目前所拥有的:

SELECT
--Month
date_add(last_day(add_months(current_timestamp(),-1)),1) as Frist_Day_of_Current_Month,

last_day(now()) as Last_Day_of_Current_Month,

date_add(last_day(add_months(current_timestamp(),-2)),1) as Frist_Day_of_Previous_Month,

last_day(add_months(current_timestamp(),-1)) as Last_Day_of_Previous_Month,

--Year
trunc(now(), 'Y') as Frist_Day_of_Current_Year,

date_sub(trunc(now(), 'YEAR'), 1) AS Last_Day_of_Previous_Year

【问题讨论】:

这个问题太多了。 【参考方案1】:

以下是日期:

选择 --单日

TRUNC(NOW(),'DD') 就像今天一样,

ADDDATE(TRUNC(NOW(),'DD'), -1) 昨天,

ADDDATE(TRUNC(NOW(),'DD'), -2) AS TWO_DAYS_AGO,

ADDDATE(TRUNC(NOW(),'DD'), -3) AS THREE_DAYS_AGO,

ADDDATE(TRUNC(NOW(),'DD'), -4) AS FOUR_DAYS_AGO,

ADDDATE(TRUNC(NOW(),'DD'), -5) AS FIVE_DAYS_AGO,

ADDDATE(TRUNC(NOW(),'DD'), -6) AS SIX_DAYS_AGO,

ADDDATE(TRUNC(NOW(),'DD'), -7) AS WEEK_AGO,

--周

--SUNDAY TO SATURDAY (NEED TO SCHEDULE THE REPORT TO RUN ON ONLY ON MONDAYS)

TRUNC(NOW(), 'D') - 间隔 1 天为 FIRST_DAY_OF_CURRENT_WEEK_SS,

TRUNC(NOW(), 'D') + INTERVAL 5 DAY AS LAST_DAY_OF_CURRENT_WEEK_SS,

TRUNC(NOW(), 'D') - 间隔 8 天为 FIRST_DAY_OF_PREVIOUS_WEEK_SS,

TRUNC(NOW(), 'D') - INTERVAL 2 DAY AS LAST_DAY_OF_PREVIOUS_WEEK_SS,

--MONDAY TO SUNDAY (NEED TO SCHEDULE THE REPORT TO RUN ONLY ON MONDAYS)

TRUNC(NOW(),'DY') AS FRIST_DAY_OF_CURRENT_WEEK_MS,

TRUNC(NOW(),'DY') + INTERVAL 6 DAY AS LAST_DAY_OF_CURRENT_WEEK_MS,

TRUNC(NOW(),'DY') - 间隔 7 天为 FIRST_DAY_OF_PREVIOUS_WEEK_MS,

TRUNC(NOW(),'DY') - 间隔 1 天为 LAST_DAY_OF_PREVIOUS_WEEK_MS,

--月

DATE_ADD(LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP(),-1)),1) AS FRIST_DAY_OF_CURRENT_MONTH,

LAST_DAY(NOW()) 作为 LAST_DAY_OF_CURRENT_MONTH,

DATE_ADD(LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP(),-2)),1) AS FRIST_DAY_OF_PREVIOUS_MONTH,

LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP(),-1)) 作为 LAST_DAY_OF_PREVIOUS_MONTH,

--季度

TRUNC(NOW(), 'Q') AS FIRST_DAY_OF_CURRENT_QUARTER,

TRUNC(NOW(), 'Q')+ INTERVAL 3 MONTHS - INTERVAL 1 DAY AS LAST_DAY_OF_CURRENT_QUARTER,

TRUNC(NOW(), 'Q') - 间隔 3 个月为 FIRST_DAY_OF_PREVIOUS_QUARTER,

TRUNC(NOW(), 'Q') - 间隔 1 天为 LAST_DAY_OF_PREVIOUS_QUARTER,

--年份

TRUNC(NOW(), 'Y') AS FRIST_DAY_OF_CURRENT_YEAR,

TRUNC(NOW(), 'YEAR') + INTERVAL 1 YEAR - INTERVAL 1 DAY AS LAST_DAY_OF_CURRENT_YEAR,

TRUNC(NOW(), 'YEAR') - 间隔 1 年为 FRIST_DAY_OF_PREVIOUS_YEAR,

TRUNC(NOW(), 'Y') - 间隔 1 天为 LAST_DAY_OF_PREVIOUS_YEAR

谢谢,

问候, 艾哈迈德

【讨论】:

【参考方案2】:

在这里我分享我正在寻找的查询:)

TRUNC(add_months(NOW() ,-3), 'Q') - INTERVAL 1 DAY AS LAST_DAY_OF_PREVIOUS_QUARTER_-2

【讨论】:

这是在回答什么问题?

以上是关于周、月、季度和年的 Impala 日期的主要内容,如果未能解决你的问题,请参考以下文章

获取周和年的第一个和最后一个日期

更改 impala trunc() 返回的一周的开始日期

从给定日期提取日、月和年的最快方法是啥?

java把一段时间分成周,月,季度,年的时间段

Lifestyle-1962年的 Chevy Impala.

pandas 日期数据处理大全,按照年、季度、月、周、日筛选数据