flink sql求本周一到周日日期函数
时间: 2023-11-07 11:13:09 浏览: 239
Flink SQL中可以使用`DATE_FORMAT`和`DATE_SUB`函数来计算本周一到周日的日期。具体实现如下:
```sql
SELECT
DATE_FORMAT(DATE_SUB(CURRENT_DATE(), DAYOFWEEK(CURRENT_DATE()) - 2), 'yyyy-MM-dd') AS monday,
DATE_FORMAT(DATE_SUB(CURRENT_DATE(), DAYOFWEEK(CURRENT_DATE()) - 3), 'yyyy-MM-dd') AS tuesday,
DATE_FORMAT(DATE_SUB(CURRENT_DATE(), DAYOFWEEK(CURRENT_DATE()) - 4), 'yyyy-MM-dd') AS wednesday,
DATE_FORMAT(DATE_SUB(CURRENT_DATE(), DAYOFWEEK(CURRENT_DATE()) - 5), 'yyyy-MM-dd') AS thursday,
DATE_FORMAT(DATE_SUB(CURRENT_DATE(), DAYOFWEEK(CURRENT_DATE()) - 6), 'yyyy-MM-dd') AS friday,
DATE_FORMAT(DATE_SUB(CURRENT_DATE(), DAYOFWEEK(CURRENT_DATE()) - 7), 'yyyy-MM-dd') AS saturday,
DATE_FORMAT(CURRENT_DATE(), 'yyyy-MM-dd') AS sunday
FROM
(
SELECT 1 AS id
) t
```
其中,`DAYOFWEEK`函数可以获取当前日期是星期几,从而计算出本周一到周日的日期。`DATE_SUB`函数可以在当前日期的基础上减去相应的天数,从而得到所求日期。`DATE_FORMAT`函数可以将日期转换为指定格式的字符串。
阅读全文