SQL Server Complete quarter flag

3htmauhk  于 2023-03-11  发布在  其他
关注(0)|答案(1)|浏览(151)

I would like to create flag for my data based at complete quarter. Something like this logic:

If passed months in quarter less than 3 then flag N else Y

Month - quarter- full quarter flag

10-2022, q4, y
11-2022, q4, y
12-2022, q4, y
01-2023, q1, n
02-2023, q1, n

Any ideas how to do this?

Thanks

Any ideas with window function etc

zujrkrfu

zujrkrfu1#

You can use SQL window function and partition by year of the month and quarter number to get the result you want.

Here is the query:

select
    month,
    quarter,
    case when count(1) over (partition by year(month), quarter) >= 3 then 'Y' else 'n' end as full_quarter_flag
from
    data_table
monthquarterfull_quarter_flag
2022-10-01q4Y
2022-11-01q4Y
2022-12-01q4Y
2023-01-01q1n
2023-02-01q1n

相关问题