Skip to content
Advertisement

Hi, I create a table with only business day, I need to calculate every 4th business day from my date table. Can you please help me?

My table is like:

2021-03-01 
2021-03-02 (for exmp. 3rd March is holiday and not including to table) 
2021-03-04
2021-03-05
... 
2021-05-03
2021-05-04
2021-05-05
2021-05-06
2021-05-07
...

And I should get result as 2021-03-05 for march, and 2021-05-06 for may.

So every month I should get the 4th row as date result.

Advertisement

Answer

You can use row_number(). But the real trick are the date functions. Let me assume that your database supports year() and month():

select t.*
from (select t.*,
             row_number() over (partition by year(col), month(col) order by col) as seqnum
      from t
     ) t
where seqnum = 4;
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement