简体   繁体   中英

SQL Date Dimension for how to write first Wednesday after second Tuesday

After searching for the best suitable way to create a date dimension, i finally found: https://www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server/

Credit to author: Aaron Bertrand (@AaronBertrand)

I was able to use his query to create a table but now my issue is, i want to be able to use that table to create a column that will house my dates for first Wednesday after second Tuesday. I don't know how to write this. Please help!

Example:

Date        DayName     First Wednesday after Second Tuesday
1/1/2018    Monday          1/10/2018
1/2/2018    Tuesday         1/10/2018
1/3/2018    Wednesday       1/10/2018
1/4/2018    Thursday        1/10/2018
1/5/2018    Friday          1/10/2018
1/6/2018    Saturday        1/10/2018
1/7/2018    Sunday          1/10/2018
1/8/2018    Monday          1/10/2018
1/9/2018    Tuesday         1/10/2018
1/10/2018   Wednesday       1/10/2018
1/11/2018   Thursday        1/10/2018
1/12/2018   Friday          1/10/2018
1/13/2018   Saturday        1/10/2018
1/14/2018   Sunday          1/10/2018
1/15/2018   Monday          1/10/2018
1/16/2018   Tuesday         1/10/2018
1/17/2018   Wednesday       1/10/2018
1/18/2018   Thursday        1/10/2018
1/19/2018   Friday          1/10/2018
1/20/2018   Saturday        1/10/2018
1/21/2018   Sunday          1/10/2018
1/22/2018   Monday          1/10/2018
1/23/2018   Tuesday         1/10/2018
1/24/2018   Wednesday       1/10/2018
1/25/2018   Thursday        1/10/2018
1/26/2018   Friday          1/10/2018
1/27/2018   Saturday        1/10/2018
1/28/2018   Sunday          1/10/2018
1/29/2018   Monday          1/10/2018
1/30/2018   Tuesday         1/10/2018
1/31/2018   Wednesday       1/10/2018
2/1/2018    Thursday        2/14/2018
2/2/2018    Friday          2/14/2018
2/3/2018    Saturday        2/14/2018
2/4/2018    Sunday          2/14/2018
2/5/2018    Monday          2/14/2018

I would create a temporary table to hold the WeekNumber and CustomSortOrder and join your calendar table, the one from Aaron Bertrand's article, against it. You can use a INSERT INTO with the query as the SELECT bit. The Year and Week values are required to align your custom sort order in chronological order.

DECLARE  @StartDate  DATETIME = GETDATE()
DECLARE @EndDate DATETIME = DATEADD(DAY,50,@StartDate)

DECLARE @CustomOrderTable TABLE (DayOfWeek INT, OrderInTable INT)
INSERT @CustomOrderTable VALUES (1,1),(2,2),(3,3),(4,5),(5,4),(6,6),(7,7)

;WITH DateRange AS  
(  
    SELECT  DATEADD(DAY,1,@StartDate) AS D, DayOfWeek = DATEPART(DW,@StartDate),Week = DATEPART(WEEK,@StartDate), Year =DATEPART(YEAR,@StartDate), DayName = DATENAME(WEEKDAY, @StartDate)
    UNION ALL  
    SELECT DATEADD(DAY,1, D), DayOfWeek = DATEPART(DW,D),Week = DATEPART(WEEK,D), Year =DATEPART(YEAR,D), DayName = DATENAME(WEEKDAY,D) FROM DateRange 
    WHERE D < @EndDate  
)

--INSERT INTO YourTable
SELECT 
    * 
FROM 
    DateRange DR
    INNER JOIN @CustomOrderTable CO ON CO.DayOfWeek=DR.DayOfWeek
ORDER BY
    DR.Year,
    DR.Week,
    CO.OrderInTable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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