【发布时间】:2018-10-05 14:36:59
【问题描述】:
有以下数据:
Declare @t Table
(
Name Varchar(1),
VisitDate Date
)
Insert Into @t select 'A','2017-01-05'
Insert Into @t select 'A','2017-03-05'
Insert Into @t select 'A','2017-04-05'
Insert Into @t select 'A','2017-05-05'
Insert Into @t select 'A','2017-08-05'
Insert Into @t select 'B','2017-03-05'
Insert Into @t select 'C','2017-01-05'
Insert Into @t select 'C','2017-02-05'
Insert Into @t select 'C','2017-04-05'
Insert Into @t select 'D','2017-01-05'
Insert Into @t select 'D','2017-02-05'
Insert Into @t select 'D','2017-03-05'
Insert Into @t select 'D','2017-06-05'
Insert Into @t select 'B','2018-01-05'
Insert Into @t select 'B','2018-02-05'
Insert Into @t select 'B','2018-03-05'
Insert Into @t select 'E','2018-01-05'
Insert Into @t select 'E','2018-02-05'
Insert Into @t select 'E','2018-03-05'
Insert Into @t select 'E','2018-06-05'
我需要编写一个查询,该查询将返回在任何一年的任何连续三个月中具有 VisitDates 的年份和名称。
根据数据,我希望看到:
2017 A
2017 D
2018 B
2018 E
说实话,我不知道从哪里开始使用 SQL。
如果能得到任何帮助,我将不胜感激。
谢谢!!
【问题讨论】:
-
12 月后计数器会重置吗?
-
@Salman:是的。确实如此。谢谢。
-
序列
1st Feb, 31st Mar, 30th Apr算不算?您实际上只是在寻找连续的 3 个日历月,即使两个事件相隔两个月还有 1 天? -
@MatBalie:是的。即使他们相隔两个月只有一天——即使他们相隔一天 [2 月 28 日,3 月 1 日]。只要他们在连续三个日历月内。
标签: sql-server tsql date datetime