SQL Select 特定日期范围内的生日
SQL Select birthdays in specific date range
我有以下内容:
DECLARE @TempTable TABLE
(
[Id] [int],
[FirstName] [varchar](40) NULL,
[Birthdate] [date] NULL
);
insert into @TempTable values (1, 'A', convert(date, '05/25/2017', 101))
insert into @TempTable values (2, 'B', convert(date, '06/25/2017', 101))
我需要的是 return 从 StartDate 到 EndDate 范围内的所有生日的查询。
这里是我期望得到的结果:
案例一:
如果日期范围设置为:
DECLARE @StartDate datetime = '05/01/2017';
DECLARE @EndDate datetime = '07/01/2017';
查询应该return:
1 A 2017-05-25
2 B 2017-06-25
案例二:
如果日期范围设置为:
DECLARE @StartDate datetime = '05/01/2017';
DECLARE @EndDate datetime = '06/01/2017';
查询应该return:
1 A 2017-05-25
案例三:
如果日期范围设置为:
DECLARE @StartDate datetime = '05/01/2015';
DECLARE @EndDate datetime = '07/01/2017';
查询应该return:
1 A 2017-05-25
1 A 2016-05-25
1 A 2015-05-25
2 B 2017-06-25
2 B 2016-06-25
2 B 2015-06-25
案例四:
如果日期范围设置为:
DECLARE @StartDate datetime = '05/01/2015';
DECLARE @EndDate datetime = '06/01/2017';
查询应该return
1 A 2017-05-25
1 A 2016-05-25
1 A 2015-05-25
2 B 2016-06-25
2 B 2015-06-25
在最基本的层面上,您可以通过创建一个包含每个人 100 年生日的列表,然后过滤...
with numbers as
(
select 0 as NN
union all
select NN+1
from numbers
where NN < 100
)
select id, dateadd(yy,NN,BirthDate) as Birthdays
from numbers
cross join @TempTable
where dateadd(yy,NN,BirthDate) between @StartDate and @EndDate
首先我们创建给定范围内的所有日期。然后应用逻辑
DECLARE @StartDate datetime = '05/01/2015';
DECLARE @EndDate datetime = '06/01/2017';
;With DateSequence as
(
Select @StartDate as Bdate
union all
Select dateadd(day, 1, Bdate)
from DateSequence
where Bdate < @EndDate
)
Select ID,FirstName,Bdate as BirthDate from DateSequence
cross join @TempTable
where Bdate between @StartDate and @EndDate and
month(Bdate)= month(BirthDate) and day(Bdate) = day(BirthDate)
order by ID asc , Bdate desc
option (MaxRecursion 2000)
OutPut :
我有以下内容:
DECLARE @TempTable TABLE
(
[Id] [int],
[FirstName] [varchar](40) NULL,
[Birthdate] [date] NULL
);
insert into @TempTable values (1, 'A', convert(date, '05/25/2017', 101))
insert into @TempTable values (2, 'B', convert(date, '06/25/2017', 101))
我需要的是 return 从 StartDate 到 EndDate 范围内的所有生日的查询。
这里是我期望得到的结果:
案例一: 如果日期范围设置为:
DECLARE @StartDate datetime = '05/01/2017';
DECLARE @EndDate datetime = '07/01/2017';
查询应该return:
1 A 2017-05-25
2 B 2017-06-25
案例二: 如果日期范围设置为:
DECLARE @StartDate datetime = '05/01/2017';
DECLARE @EndDate datetime = '06/01/2017';
查询应该return:
1 A 2017-05-25
案例三: 如果日期范围设置为:
DECLARE @StartDate datetime = '05/01/2015';
DECLARE @EndDate datetime = '07/01/2017';
查询应该return:
1 A 2017-05-25
1 A 2016-05-25
1 A 2015-05-25
2 B 2017-06-25
2 B 2016-06-25
2 B 2015-06-25
案例四: 如果日期范围设置为:
DECLARE @StartDate datetime = '05/01/2015';
DECLARE @EndDate datetime = '06/01/2017';
查询应该return
1 A 2017-05-25
1 A 2016-05-25
1 A 2015-05-25
2 B 2016-06-25
2 B 2015-06-25
在最基本的层面上,您可以通过创建一个包含每个人 100 年生日的列表,然后过滤...
with numbers as
(
select 0 as NN
union all
select NN+1
from numbers
where NN < 100
)
select id, dateadd(yy,NN,BirthDate) as Birthdays
from numbers
cross join @TempTable
where dateadd(yy,NN,BirthDate) between @StartDate and @EndDate
首先我们创建给定范围内的所有日期。然后应用逻辑
DECLARE @StartDate datetime = '05/01/2015';
DECLARE @EndDate datetime = '06/01/2017';
;With DateSequence as
(
Select @StartDate as Bdate
union all
Select dateadd(day, 1, Bdate)
from DateSequence
where Bdate < @EndDate
)
Select ID,FirstName,Bdate as BirthDate from DateSequence
cross join @TempTable
where Bdate between @StartDate and @EndDate and
month(Bdate)= month(BirthDate) and day(Bdate) = day(BirthDate)
order by ID asc , Bdate desc
option (MaxRecursion 2000)
OutPut :