2017-05-19 36 views
1

我需要查看给定时间范围内的每个月/每年,并检查在该月份和年份中文件是否处于打开状态。在每个文件上都有一个开放日期和结束日期,以便我想检查每个文件是否开放日期的月份和年份小于或等于该范围中的每个月份/年份,然后如果结束日期为空或有一个月/一年大于该范围内的每月/年。然而,我很难说如何通知查询当前的月份是什么,我正在看。我的查询如下所示:SQL检查文件在特定时间段内是否处于打开状态

-- New Files 
select distinct datepart(MM, ws.CaseOpenDate) as 'Month', 
    datepart(yyyy, ws.CaseOpenDate) as 'Year', 
    count(ws.WorksheetID) as 'Count' 
into #newfiles 
from LGSIntersect lgs 
    join Worksheet ws on ws.LGSIntersectID = lgs.LGSIntersectID 
where ws.CaseOpenDate between '01/01/2016' and '03/01/2017' 
    and lgs.GroupCodeID = '307' 
group by datepart(MM, ws.CaseOpenDate), datepart(yyyy, ws.CaseOpenDate) 

-- Open Files 
select distinct datepart(MM, ws.CaseOpenDate) as 'Month', 
    datepart(yyyy, ws.CaseOpenDate) as 'Year', 
    datepart(MM, ws.CloseDate) as 'CloseMonth', 
    datepart(yyyy, ws.CloseDate) as 'CloseYear', 
    count(ws.WorksheetID) as 'Count' 
into #openfiles 
from LGSIntersect lgs 
    join Worksheet ws on ws.LGSIntersectID = lgs.LGSIntersectID 
where ws.CaseOpenDate <= '03/01/2017' 
    and (ws.CloseDate is null or ws.CloseDate >= '01/01/2016') 
    and lgs.GroupCodeID = '307' 
group by datepart(MM, ws.CaseOpenDate), datepart(yyyy, ws.CaseOpenDate), 
    datepart(MM, ws.CloseDate), datepart(yyyy, ws.CloseDate) 

-- New Investigations 
select distinct datepart(MM, ws.InvestigationDate) as 'Month', 
    datepart(yyyy, ws.InvestigationDate) as 'Year', 
    count(ws.WorksheetID) as 'Count' 
into #newinv 
from LGSIntersect lgs 
    join Worksheet ws on ws.LGSIntersectID = lgs.LGSIntersectID 
where ws.InvestigationDate between '01/01/2016' and '03/01/2017' 
    and lgs.GroupCodeID = '307' 
group by datepart(MM, ws.InvestigationDate), 
    datepart(yyyy, ws.InvestigationDate) 

-- Ques Sent 
select distinct 
    datepart(MM, qpd.PrintDate) as 'Month', 
    datepart(yyyy, qpd.PrintDate) as 'Year', 
    count(qpd.QuestionnairePrintDetailID) as 'Count' 
into #ques 
from LGSIntersect lgs 
    join QuestionnairePrintDetail qpd 
     on qpd.LGSIntersectID = lgs.LGSIntersectID 
where qpd.PrintDate between '01/01/2016' and '03/01/2017' 
    and lgs.GroupCodeID = '307' 
group by datepart(MM, qpd.PrintDate), 
    datepart(yyyy, qpd.PrintDate) 

select distinct 
    nf.Month, 
    nf.Year, 
    isnull(sum(q.Count), 0) as '# of Ques Sent', 
    isnull(sum(ni.Count), 0) as '# of New Investigations', 
    isnull(sum(nf.Count), 0) as '# of New Files', 
    isnull(sum(opf.Count), 0) as '# of Open Files' 
from #newfiles nf 
    left join #ques q on q.Month = nf.Month and q.Year = nf.Year 
    left join #newinv ni on ni.Month = nf.Month and ni.Year = nf.Year 
    left join #openfiles opf on (opf.Month + opf.Year <= nf.Month + nf.Year) 
     and (opf.CloseMonth is null 
      or (opf.CloseMonth + opf.CloseYear > nf.Month + nf.Year)) 
group by nf.Month, nf.Year 

现在没有办法,以确保它是唯一有问题每个月/年期间被打开的文件总数(总结不像“发送疑问句的#”和“#的新文件“,当我删除打开文件的逻辑时,它们可以正确计算)。任何提示将不胜感激 - 让我知道如果我需要提供额外的信息。我正在休6个月的病假,所以我很生气。谢谢!

+0

我正在使用MS SQL服务器 – Jana

回答

1

这是我认为是问题的要点 - 在新文件的情况下,如果文件在01/01/2016打开,那么新文件的结果集将只有一个月的记录一月 如果打开文件,如果文件在01/01/2016打开,并且关闭日期为空(我猜这意味着仍然打开),那么打开文件的结果集需要每个月都有记录(即15记录15个月),因为这些记录已经在这些月份开放。因此,开始日期为01/01/2016和结束日期为空的一个文件记录应在结果集中生成15条记录。而在新文件的情况下,该文件记录将只在结果集中产生一个记录,即一月份的月份。

所以, 这将是一个不错的主意,有

  • 一个单独的表来维护所有年份,月份列表。
  • 在此年 - 月份表上应用日期范围过滤器。
  • 使用此年份表作为主表 - 左外部连接到 您的文件表。如果年份在单个文件的开始日期和日期之间,则计为1。否则为0.
  • 总计/总计上述计数,并按年份分组。这应该会为您提供指定日期范围内每个月的打开文件数。

[编辑] 添加更多细节的请求

月表
MonthStartDate,MonthEndDate

文件表
CaseOpenDate,CaseCloseDate

MonthsTable左外部联接FilesTable
ON
CaseOpenDate < = MonthEndDate

(CaseCloseDate> MonthEndDate OR CaseCloseDate IS NULL)

希望有所帮助。

+0

谢谢!这听起来像它可能会做我所需要的。我会给它一个镜头! – Jana

+0

我将使用什么标准将主日期表连接到打开的文件表中? – Jana

+0

编辑我的答案在底部添加更多细节。 – RKRC

相关问题