更改一周的开始日期impala

x9ybnkn6  于 2021-06-26  发布在  Impala
关注(0)|答案(2)|浏览(462)

我正在尝试更改一周的开始日期,但无法实现当前的星期一到星期天,它应该是星期天-星期六我尝试在下面的数据集上使用此查询 invoice date 以及 invoice_week ```
SELECT invoice_date,
weekofyear(invoice_date) as invoice_week,
datesub(invoice_date,1),
weekofyear(datesub(invoice_date,1)) as invoice_week1
from table

![](https://i.stack.imgur.com/UaPnB.png)
3mpgtkmj

3mpgtkmj1#

我认为你不应该把日期减1,而应该把日期加1:

select 
        invoice_date,
        weekofyear(invoice_date) as invoice_week,
        adddate(invoice_date,1),
        weekofyear(adddate(invoice_date,1)) as invoice_week1 
from(
        select cast('2018-01-07 16:00:00' as timestamp) invoice_date
)stg
ybzsozfc

ybzsozfc2#

找到了一个有效的解决方案。给定一个日期,这里是一周的开始和结束的日期,如果您想要一个周日-周六的星期,而不是默认的周一-周日的星期。

SET var:date=2019-01-06;
select  to_date(IF( dayofweek(cast('${var:date}' as timestamp)) = 1, cast('${var:date}' as timestamp), trunc(cast('${var:date}' as timestamp), 'd') - interval 1 day)) as startOfWeek, to_date(IF( dayofweek(cast('${var:date}' as timestamp)) = 1, (trunc(cast('${var:date}' as timestamp) + interval 1 day, 'd') + interval 5 day), (trunc(cast('${var:date}' as timestamp), 'd') + interval 5 day) )) as endOfWeek;
+-------------+------------+
| startofweek | endofweek  |
+-------------+------------+
| 2019-01-06  | 2019-01-12 |
+-------------+------------+

相关问题