简体   繁体   English

SQL DatePart星期,以反映会计年度的年份

[英]SQL DatePart Week, year to reflect fiscal year

I currently have a report that uses DATEPART to return the week a 'car' was returned. 我目前有一个使用DATEPART返回“汽车”返回一周的报告。 However, the business I work for fiscal year starts first Sunday of the year (this instance commencing 03/01/2016 would be week 1). 但是,我为会计年度工作的业务从该年度的第一个星期日开始(此实例从03/01/2016开始将是第1周)。 However, using SQL 'DATEPART wk' would return this date is week 2: 但是,使用SQL'DATEPART wk'返回的日期是第2周:

Current outcome using DATEPART (wk, year etc): 使用DATEPART的当前结果(周,年等):

CarTitle      ReturnDate      Year       Week       
Car 1         30/12/2015      2015        53
Car 2         02/01/2016      2016        1
Car 3         03/01/2016      2016        2
Car 4         05/01/2016      2016        2
Car 5         10/01/2016      2016        3
Car 6         17/01/2016      2016        4

Example of expected outcome: 预期结果示例:

CarTitle      ReturnDate      Year       Week       
Car 1         30/12/2015      2015        53
Car 2         02/01/2016      2015        53
Car 3         03/01/2016      2016        1
Car 4         05/01/2016      2016        1
Car 5         10/01/2016      2016        2
Car 6         17/01/2016      2016        3

I can calculate this with the following SQL: 我可以使用以下SQL进行计算:

SELECT
    CarTitle,
    DATEPART(ISO_WEEK, DATEADD(day, 1, ReturnDate))
FROM
    dbo.My_Table

However , this depends on your localization settings (which are probably different from mine given how you express dates) and if you use this code throughout your system and then the business decides to change it's method for calculating weeks then you could end up with a big refactorization headache. 但是 ,这取决于您的本地化设置(鉴于您表达日期的方式,这可能与我的本地化设置不同),如果您在整个系统中使用此代码,然后企业决定更改其计算周数的方法,那么最终可能会花费很多重构头痛。

I prefer to use a Calendar table for this sort of thing: 我更喜欢将Calendar表用于这种事情:

CREATE TABLE dbo.Calendar (
    calendar_date    DATE        NOT NULL,
    week_number      SMALLINT    NOT NULL,
    is_holiday       BIT         NOT NULL,
    name             VARCHAR(30) NOT NULL,  -- i.e. 'January 6th, 2016'
    CONSTRAINT PK_Calendar PRIMARY KEY CLUSTERED (calendar_date)
)

You can expand the table as needed, for example if accounting uses a different definition for week of the year than another department then that's easy to accommodate by adding another column. 您可以根据需要展开表,例如,如果会计年度在一年中的某个星期使用的定义与其他部门不同,则可以通过添加另一列来轻松容纳。 This then makes your query a simple join to get the week number: 然后,这使您的查询成为获得周数的简单联接:

SELECT
    MT.CarTitle,
    MT.ReturnDate,
    CAL.week_number
FROM
    My_Table MT
INNER JOIN dbo.Calendar CAL ON CAL.calendar_date = MT.ReturnDate

You'll need to populate the table, but that should be a one time effort (populate it for 50 years in advance and it's still a pretty small table) and you can use code similar to my first statement to generate the values - don't enter each date by hand ;) After that you just need to maintain the table if the business logic changes (which is easier than changing all of your queries that deal with dates) or if you get a new requirement that needs a new column. 您需要填充该表,但这应该是一次性的工作(提前50年填充它,但它仍然是一个很小的表),并且可以使用类似于我的第一条语句的代码来生成值-不要t手动输入每个日期;)之后,如果业务逻辑发生变化(比更改所有处理日期的查询要容易)或您收到需要新列的新要求,则只需维护该表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM