所以我想知道是否有使用递归CTE的方式,我可以一年7个星期,每个月一次。目前,对于每月日历视图,我使用递归CTE,它使用startdate作为锚点并递归到enddate(我已粘贴下面的proc)。我尝试创建的每周视图应该与Microsoft Outlook的“日历”选项卡中的视图类似。提前感谢任何想法!
每月观看过程:
CREATE PROCEDURE [Event].[Report_Event_Calendar_Month_sp]
@start_cymd DATE = NULL ,
@end_cymd DATE = NULL
AS
BEGIN
DECLARE @StartDate DATE = @start_cymd ,
@EndDate DATE = @end_cymd
IF @StartDate IS NULL
SET @StartDate = CONVERT(DATE, DATEADD(dd, -DATEPART(dd, GETDATE()) + 1, GETDATE()))
IF @EndDate IS NULL
SET @EndDate = CONVERT(DATE, EOMONTH(GETDATE()))
---First day of current month
SET @StartDate = CONVERT(DATE, DATEADD(s, 0, DATEADD(mm, DATEDIFF(m, 0, @StartDate), 0)))
---First day to display on calendar
SET @StartDate = CONVERT(DATE, DATEADD(DAY, -DATEPART(WEEKDAY, @StartDate) + 1, @StartDate))
---Last day of month
SET @EndDate = CONVERT(DATE, DATEADD(s, 0, DATEADD(mm, DATEDIFF(m, 0, @start_cymd) + 1, 0)))
---Last day to display on calendar
SET @EndDate = CONVERT(DATE, DATEADD(DAY, 6-DATEPART(WEEKDAY, @EndDate), @EndDate))
;WITH Dates([Date])
AS (
SELECT @StartDate AS [Date]
UNION ALL
SELECT DATEADD(DAY, 1, [Date])
FROM Dates
WHERE [Date] < @EndDate
) ,
Events
AS (
SELECT EventDesc ,
Notes ,
start_cymd AS EventDate,
end_cymd
FROM [Event].Event_Description ED
INNER JOIN [Event].SQL_Team_Events SE ON ED.EventDesc_ID = SE.EventDesc_ID
)
---Number the records based on the date, if multiple records have
---the same date then they will be numbered the same. Used in
---calculation to determine row record is to display
SELECT [Order] = DENSE_RANK() OVER (ORDER BY d.[Date]) ,
---Date used in all calculations for date
d.[Date] ,
---Generates matrix columns
[WeekDay] = DATEPART(WEEKDAY, d.[Date]) ,
---Used to display day of month on calendar
[Day] = DATEPART(DAY, d.[Date]) ,
---Used in some calculations for display
[Month] = DATEPART(MONTH, d.[Date]) ,
e.EventDesc ,
e.Notes ,
e.EventDate ,
e.end_cymd
FROM Dates d
LEFT JOIN Events e ON d.[Date] = e.EventDate
---Set the maximum number of times the CTE 'Dates' can recurse
OPTION (MAXRECURSION 100)
END