我有两个不同的表,我正在尝试创建一个案例,如果'A date'在'rollment date'之后的90天之内。
第一张表
Member ID rollment date
1 11/1/2014
2 4/1/2015
3 1/1/2014
4 2/1/2014
5 6/1/2014
第二张表
Member ID A A date
1 Yes 12/5/2014
2 Yes 6/9/2015
3 Yes 6/5/2014
4 Null Null
5 Yes 7/25/2014
我希望我的输出看起来像这样:
Member ID Enrollment Within 90 days
1 11/1/2014 Yes
2 4/1/2015 Yes
3 1/1/2014 No
4 2/1/2014 No
5 6/1/2014 Yes
我的sql查询如下所示:
SELECT
a.MemberID
,a.rollment
,Case when B.A date between a.rollment < '90' then
'yes' else 'no' end
FROM [Database].[dbo].[Test] a
left join [database].[dbo].[Test2] b on a.MemberID = B.MemberID
有人会帮助我。
答案 0 :(得分:1)
你应该使用datediff,就像这样:
SELECT
a.MemberID
,a.rollment
,Case when datediff(day, B.[A date], a.rollment) < 90
then 'yes' else 'no' end
FROM
[Database].[dbo].[Test] a
left join [database].[dbo].[Test2] b
on a.MemberID = B.MemberID