在语句和周期之间使用 - 指示月份是否在日期之间

时间:2015-07-21 14:31:57

标签: sql sql-server date sql-server-2008-r2

基本上我有一个包含开始和结束日期的名称列表。由于我目前无法重新设计之前设计人员的流程以实现更简单的操作,因此我需要找到一种方法来遍历名称列表以及介于开始日期和结束日期之间的每个月将具有该月/年的表更新为true。

这是目前在Access中完成的方式:

SELECT 
    dbo_Elig.MEM_ID, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#12/1/2011# And [END_COVERAGE_DATE]>=#11/1/2011#,1,0)) AS mm201111, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#1/1/2012# And [END_COVERAGE_DATE]>=#12/1/2011#,1,0)) AS mm201112, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#2/1/2012# And [END_COVERAGE_DATE]>=#1/1/2012#,1,0)) AS mm201201, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#3/1/2012# And [END_COVERAGE_DATE]>=#2/1/2012#,1,0)) AS mm201202, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#4/1/2012# And [END_COVERAGE_DATE]>=#3/1/2012#,1,0)) AS mm201203, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#5/1/2012# And [END_COVERAGE_DATE]>=#4/1/2012#,1,0)) AS mm201204, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#6/1/2012# And [END_COVERAGE_DATE]>=#5/1/2012#,1,0)) AS mm201205, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#7/1/2012# And [END_COVERAGE_DATE]>=#6/1/2012#,1,0)) AS mm201206, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#8/1/2012# And [END_COVERAGE_DATE]>=#7/1/2012#,1,0)) AS mm201207, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#9/1/2012# And [END_COVERAGE_DATE]>=#8/1/2012#,1,0)) AS mm201208, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#10/1/2012# And [END_COVERAGE_DATE]>=#9/1/2012#,1,0)) AS mm201209, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#11/1/2012# And [END_COVERAGE_DATE]>=#10/1/2012#,1,0)) AS mm201210, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#12/1/2012# And [END_COVERAGE_DATE]>=#11/1/2012#,1,0)) AS mm201211, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#1/1/2013# And [END_COVERAGE_DATE]>=#12/1/2012#,1,0)) AS mm201212, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#2/1/2013# And [END_COVERAGE_DATE]>=#1/1/2013#,1,0)) AS mm201301, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#3/1/2013# And [END_COVERAGE_DATE]>=#2/1/2013#,1,0)) AS mm201302, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#4/1/2013# And [END_COVERAGE_DATE]>=#3/1/2013#,1,0)) AS mm201303, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#5/1/2013# And [END_COVERAGE_DATE]>=#4/1/2013#,1,0)) AS mm201304, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#6/1/2013# And [END_COVERAGE_DATE]>=#5/1/2013#,1,0)) AS mm201305, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#7/1/2013# And [END_COVERAGE_DATE]>=#6/1/2013#,1,0)) AS mm201306, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#8/1/2013# And [END_COVERAGE_DATE]>=#7/1/2013#,1,0)) AS mm201307, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#9/1/2013# And [END_COVERAGE_DATE]>=#8/1/2013#,1,0)) AS mm201308, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#10/1/2013# And [END_COVERAGE_DATE]>=#9/1/2013#,1,0)) AS mm201309, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#11/1/2013# And [END_COVERAGE_DATE]>=#10/1/2013#,1,0)) AS mm201310, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#12/1/2013# And [END_COVERAGE_DATE]>=#11/1/2013#,1,0)) AS mm201311, 
    Max(IIf([BEGIN_COVERAGE_DATE]<#1/1/2014# And [END_COVERAGE_DATE]>=#12/1/2013#,1,0)) AS mm201312 
INTO [t_mm11-13] 
FROM dbo_Elig 
GROUP BY dbo_Elig.MEM_ID;

我试图避免在SQL中编写case语句来模仿这个过程。

这篇Loop/Iterate through date range while inserting文章的版本似乎适用于PL / SQL,但我不确定如何使其适用于SQL Server 2008 R2。

有什么建议吗?

0 个答案:

没有答案