我正在尝试更改一周的开始日期,但无法实现
当前是周一至周日,应定为周日至周六
我尝试在以下数据集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
答案 0 :(得分:0)
我认为不要将日期减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
答案 1 :(得分:0)
找到了可行的解决方案。给定一个日期,如果您要星期天-周六,而不是默认情况下的星期一-周日,则这是一周开始和一周结束的日期。
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 |
+-------------+------------+