1

我一直在寻找几天来解决我的 Fullcalender 上重复事件的问题。我最简单的方法可能是根据存储在表中的数据生成重复日期。

我过去没有使用过 CTE 查询。我需要的是为天生成重复日期示例

每个星期一和星期四,我们都会在办公室举行项目会议。在这种情况下,我将设置开始日期 = 2012-06-04 结束日期 = 2012-06-31

现在我想生成 SQL 查询,它将显示从 2012 年 6 月 4 日到 2012 年 8 月 31 日的所有日期,基于它们是否重复发生

EventID     Date          Title
1          2012-06-04     Project Meeting
1          2012-06-07     Project Meeting
1          2012-06-11     Project Meeting
1          2012-06-14     Project Meeting
1          2012-06-18     Project Meeting
1          2012-06-21     Project Meeting
1          2012-06-25     Project Meeting
1          2012-06-18     Project Meeting
2          2012-05-13     Meeting With Client
3          2012-06-25     Event 3

我能够理解我如何能够做到这一点,我将不胜感激这方面的帮助或指出类似的例子。如果我能够正确生成此数据,那么我可以将这些日期与 eventTitle 一起发送到 FullCalender 控件以显示事件日历,如下面的示例所示http://arshaw.com/fullcalendar/

如果有人愿意帮助他可以从下面的 sql 创建表和示例数据。这是 MS SQL SERVER 2008 r2 兼容的,因为我使用了仅受 MS SQL 2008 支持的数据类型

带有数据的架构

/****** Object:  Table [dbo].[EventCalender]    Script Date: 05/24/2012 20:18:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[EventCalender](
    [EventID] [int] IDENTITY(1,1) NOT NULL,
    [EventTitle] [nvarchar](900) NULL,
    [EventDetails] [nvarchar](max) NULL,
    [EventStartDate] [date] NOT NULL,
    [EventEndDate] [date] NULL,
    [EventStartTime] [time](0) NULL,
    [EventEndTime] [time](0) NULL,
    [EventRecurring] [bit] NULL,
    [EventEnumDays] [varchar](50) NULL,
    [EventBlockDate] [bit] NULL,
    [EventVisibleOnWebsite] [bit] NULL,
    [EventActive] [bit] NULL,
    [EventType] [nvarchar](200) NULL,
    [LanguageID] [int] NOT NULL,
    [EventCreatedOn] [date] NULL,
    [EventShowTime] [bit] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[EventCalender] ON
INSERT [dbo].[EventCalender] ([EventID], [EventTitle], [EventDetails], [EventStartDate], [EventEndDate], [EventStartTime], [EventEndTime], [EventRecurring], [EventEnumDays], [EventBlockDate], [EventVisibleOnWebsite], [EventActive], [EventType], [LanguageID], [EventCreatedOn], [EventShowTime]) VALUES (1, N'Project Meeting', N'Every Monday, Thrusday', CAST(0xC1350B00 AS Date), CAST(0x19360B00 AS Date), CAST(0x00A08C0000000000 AS Time), CAST(0x00B8A10000000000 AS Time), 1, N'2,5,7', 0, 0, 1, N'EVENT', 1, CAST(0xB6350B00 AS Date), NULL)
INSERT [dbo].[EventCalender] ([EventID], [EventTitle], [EventDetails], [EventStartDate], [EventEndDate], [EventStartTime], [EventEndTime], [EventRecurring], [EventEnumDays], [EventBlockDate], [EventVisibleOnWebsite], [EventActive], [EventType], [LanguageID], [EventCreatedOn], [EventShowTime]) VALUES (2, N'Meeting With Client', N'Event Details 2', CAST(0xBA350B00 AS Date), CAST(0xBA350B00 AS Date), NULL, NULL, 0, NULL, 0, 0, 1, N'EVENT', 1, CAST(0xB6350B00 AS Date), NULL)
INSERT [dbo].[EventCalender] ([EventID], [EventTitle], [EventDetails], [EventStartDate], [EventEndDate], [EventStartTime], [EventEndTime], [EventRecurring], [EventEnumDays], [EventBlockDate], [EventVisibleOnWebsite], [EventActive], [EventType], [LanguageID], [EventCreatedOn], [EventShowTime]) VALUES (3, N'Event 3', N'Event Details 3', CAST(0xAB350B00 AS Date), CAST(0xAB350B00 AS Date), NULL, NULL, 0, NULL, 0, 0, 0, N'EVENT', 1, CAST(0xB6350B00 AS Date), 0)
INSERT [dbo].[EventCalender] ([EventID], [EventTitle], [EventDetails], [EventStartDate], [EventEndDate], [EventStartTime], [EventEndTime], [EventRecurring], [EventEnumDays], [EventBlockDate], [EventVisibleOnWebsite], [EventActive], [EventType], [LanguageID], [EventCreatedOn], [EventShowTime]) VALUES (5, N'Weekly Meeting', N'Weekly Event', CAST(0xA3350B00 AS Date), CAST(0x01380B00 AS Date), CAST(0x00D0B60000000000 AS Time), NULL, 1, N'2,', 0, 1, 1, N'ACTIVITY', 2, NULL, 0)
SET IDENTITY_INSERT [dbo].[EventCalender] OFF
/****** Object:  Default [DF_EventCalender_EventEndDate]    Script Date: 05/24/2012 20:18:00 ******/
ALTER TABLE [dbo].[EventCalender] ADD  CONSTRAINT [DF_EventCalender_EventEndDate]  DEFAULT (NULL) FOR [EventEndDate]
GO
/****** Object:  Default [DF_EventCalender_EventStartTime]    Script Date: 05/24/2012 20:18:00 ******/
ALTER TABLE [dbo].[EventCalender] ADD  CONSTRAINT [DF_EventCalender_EventStartTime]  DEFAULT (NULL) FOR [EventStartTime]
GO
/****** Object:  Default [DF_EventCalender_EventEndTime]    Script Date: 05/24/2012 20:18:00 ******/
ALTER TABLE [dbo].[EventCalender] ADD  CONSTRAINT [DF_EventCalender_EventEndTime]  DEFAULT (NULL) FOR [EventEndTime]
GO
/****** Object:  Default [DF_EventCalender_EventRecurring]    Script Date: 05/24/2012 20:18:00 ******/
ALTER TABLE [dbo].[EventCalender] ADD  CONSTRAINT [DF_EventCalender_EventRecurring]  DEFAULT ((0)) FOR [EventRecurring]
GO
/****** Object:  Default [DF_EventCalender_EventEnumDays]    Script Date: 05/24/2012 20:18:00 ******/
ALTER TABLE [dbo].[EventCalender] ADD  CONSTRAINT [DF_EventCalender_EventEnumDays]  DEFAULT (NULL) FOR [EventEnumDays]
GO
/****** Object:  Default [DF_EventCalender_EventBlockDate]    Script Date: 05/24/2012 20:18:00 ******/
ALTER TABLE [dbo].[EventCalender] ADD  CONSTRAINT [DF_EventCalender_EventBlockDate]  DEFAULT ((0)) FOR [EventBlockDate]
GO
/****** Object:  Default [DF_EventCalender_EventVisibleOnWebsite]    Script Date: 05/24/2012 20:18:00 ******/
ALTER TABLE [dbo].[EventCalender] ADD  CONSTRAINT [DF_EventCalender_EventVisibleOnWebsite]  DEFAULT ((0)) FOR [EventVisibleOnWebsite]
GO
/****** Object:  Default [DF_EventCalender_EventActive]    Script Date: 05/24/2012 20:18:00 ******/
ALTER TABLE [dbo].[EventCalender] ADD  CONSTRAINT [DF_EventCalender_EventActive]  DEFAULT ((0)) FOR [EventActive]
GO
/****** Object:  Default [DF_EventCalender_EventShowTime]    Script Date: 05/24/2012 20:18:00 ******/
ALTER TABLE [dbo].[EventCalender] ADD  CONSTRAINT [DF_EventCalender_EventShowTime]  DEFAULT ((0)) FOR [EventShowTime]
GO
4

1 回答 1

2

这可以为您提供如何使用 CTE 创建重复日期的起点:

;with Dates as (
    select cast('2012-06-04' as date) as StartDate, cast('2012-08-31' as date) as EndDate
),
RecursiveCTE as (
    select StartDate, EndDate, StartDate as EventDate
    from Dates
    union all
    select StartDate, EndDate, dateadd(dd, 7, EventDate)
    from RecursiveCTE
    where dateadd(dd, 7, EventDate) < Enddate
)
select * from RecursiveCTE

更新

使用您的模式和插入,这是一个递归 CTE,它给出了所需的结果:

;with Calendar as (
    select EventID, EventTitle, EventStartDate, EventEndDate, EventEnumDays, EventRecurring, EventStartDate as PlannedDate
    from EventCalender
    where EventActive = 1
    union all
    select EventID, EventTitle, EventStartDate, EventEndDate, EventEnumDays, EventRecurring, dateadd(dd, 1, PlannedDate)
    from Calendar
    where EventRecurring = 1
        and dateadd(dd, 1, PlannedDate) <= EventEndDate
)
select EventID, PlannedDate as [Date], EventTitle
from Calendar
where ',' + EventEnumDays + ',' like '%,' + cast(datepart(dw, PlannedDate) as char(1)) + ',%'
    or EventEnumDays is null
order by EventID, PlannedDate
option (maxrecursion 0)

请注意,如果您想包含每月或每年的事件,则必须进行一些修改(可能是案例/切换),因为我在这里依靠“星期几”(dw)来获取每周的内容。

此外,递归 CTE 可能是最慢的计数方法之一,这基本上就是我们在这里所做的。如果速度是最重要的,那么计数表可能是更好的选择。

于 2012-05-24T12:52:43.370 回答