forked from lyhabc/SQLServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DayoftheYear.sql
144 lines (100 loc) · 2.43 KB
/
DayoftheYear.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Get the day number, day name and day id (meaning 1 - 7) of each day for the week.
*/
CREATE TABLE ##year(
WeekDayID TINYINT,
DayNumber SMALLINT IDENTITY(1,1),
MonthDay SMALLINT,
MonthBusinessDay SMALLINT,
MonthID TINYINT,
MonthName VARCHAR(10),
DayName VARCHAR(10),
ActualDate DATE
)
DECLARE @start DATE = '2013-01-01'
DECLARE @begin SMALLINT = 1
WHILE @begin <= 365
BEGIN
INSERT INTO ##year (ActualDate)
SELECT @start
SET @start = DATEADD(DD,1,@start)
SET @begin = @begin + 1
END
UPDATE ##year
SET MonthID = MONTH(ActualDate),
WeekDayID = DATEPART(DW,ActualDate),
MonthName = DATENAME(MONTH, ActualDate),
DayName = DATENAME(DW,ActualDate),
MonthDay = DAY(ActualDate)
DECLARE @mbegin TINYINT = 1
WHILE @mbegin <= 12
BEGIN
CREATE TABLE #month(
DayID SMALLINT IDENTITY(1,1),
DayNumber SMALLINT
)
INSERT INTO #month (DayNumber)
SELECT DayNumber
FROM ##year
WHERE MonthID = @mbegin
AND WeekDayID <> 7
AND WeekDayID <> 1
UPDATE ##year
SET MonthBusinessDay = DayID
FROM #month m
WHERE ##year.DayNumber = m.DayNumber
AND ##year.MonthBusinessDay IS NULL
DROP TABLE #month
SET @mbegin = @mbegin + 1
END
SELECT *
FROM ##year
/*
-- Playing with trade dates (on business days only): returns the last four trade dates of the month
DECLARE @store TABLE(
MonthID TINYINT,
MonthBusinessDay TINYINT
)
DECLARE @month TINYINT = 1
WHILE @month <= 12
BEGIN
INSERT INTO @store
SELECT @month
, MAX(MonthBusinessDay)
FROM ##year
WHERE MonthID = @month
AND MonthBusinessDay IS NOT NULL
SET @month = @month + 1
END
SELECT DATEADD(DD,-3,y.ActualDate) AS ThirdBeforeFinal
, DATEADD(DD,-2,y.ActualDate) AS SecondBeforeFinal
, DATEADD(DD,-1,y.ActualDate) AS FirstBeforeFinal
, y.ActualDate AS FinalTradeDate
FROM ##year y
INNER JOIN @store s ON y.MonthID = s.MonthID AND y.MonthBusinessDay = s.MonthBusinessDay
SELECT MIN(ActualDate) AS "FirstTradeDay"
, DATEADD(DD,1,(MIN(ActualDate))) AS "SecondTradeDay"
FROM ##year
WHERE ActualDate IN (SELECT ActualDate FROM ##year WHERE MonthBusinessDay IN (1,2))
GROUP BY MONTH(ActualDate)
-- S
-- WeekDayID C
-- ActualDate NC
SELECT ActualDate
FROM TTCYear
WHERE ActualDate BETWEEN '2013-03-01' AND '2013-04-30'
SELECT ActualDate
FROM TTCYear
WHERE MONTH(ActualDate) = '03'
OR MONTH(ActualDate) = '04'
SELECT *
FROM TTCYear
WHERE WeekDayID <> 1
AND WeekDayID <> 7
SELECT *
FROM TTCYear
WHERE WeekDayID NOT IN (1,7)
SELECT *
FROM TTCYear
WHERE WeekDayID BETWEEN 2 AND 6
*/