我有一列包含日期,并想介绍另一列,它是关于日期是否是其各自季度的最后一天的标志 (1/0)。
数据:
| date |
|------------|
| 2021-04-10 |
| 2021-05-19 |
| 2021-09-30 |
预期输出:
| date | is_last_day_of_quarter |
|------------|------------------------|
| 2021-04-10 | 0 |
| 2021-05-19 | 0 |
| 2021-09-30 | 1 |
我能够提出以下建议,这似乎可行:
select
date,
case
when date = last_day_of_q then 1 else 0
end as is_last_day_of_quarter
from(
select
date,
case
when month(date) < 4 then (concat(year(date), '03-31'))
when month(date) between 4 and 6 then concat((year(date)), '-06-30')
when month(date) between 7 and 9 then concat((year(date)), '-09-30')
when month(date) between 10 and 12 then concat((year(date)), '-12-31')
end as last_day_of_q
from
some_table
) t
我想知道是否有更好的方法可以做到这一点,或者是否可以在不必使用子查询的情况下实现?