Skip to content
Advertisement

Creating FISCAL_WEEK column in SQL Server Calendar table

We have a requirement. We need to create a fiscal_week column in a SQL Server table.

Table will have data as Normal_date column as fiscal_date and fiscal_year column which will have year part of the date.

Logic for FISCAL_WEEK is like this:

  • FIRST FISCAL WEEK WILL START FROM 1 JAN OF EVERY YEAR AND IT WILL BE TILL FIRST FRIDAY.
  • SECOND WEEK STARTS FROM SATURDAY AND IT WILL RUN TILL NEXT FRIDAY.
  • THIS WILL GO ON TILL THE END OF YEAR (31 JAN)

We will have data something as below table.

Table With Fields as per Requirement

How would I create query for this ? We will have data from 2010 till 2035 years in the table.

Thanks, Mahesh

Advertisement

Answer

You can use an ad-hoc tally table to create the dates and then the window function sum() over() to calculate the Fiscal_Week

Results

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement