我需要查看给定时间范围内的每个月/每年,并检查在该月份和年份中文件是否处于打开状态。在每个文件上都有一个开放日期和结束日期,因此对于每个文件,我想检查开放日期的月份和年份是否小于或等于该范围内的每个月/年,然后如果关闭日期为空或已经一个月/年大于该范围内的每个月/年。但是,我很难知道如何通知查询我正在看的当前月份是什么。我的查询如下所示:
-- 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 :(得分:1)
以下是我认为问题的要点 - 对于新文件,如果文件在01/01/2016打开,那么新文件的结果集将只有一个月的记录 如果是打开文件,如果在01/01/2016打开一个文件并且关闭日期为空(我猜这意味着仍然打开),那么打开文件的结果集将需要每个月都有记录(即15个记录) 15个月以来,记录已经开放了所有这几个月。因此,一个文件记录的开始日期为01/01/2016,结束日期为null,应在结果集中生成15条记录。而在新文件的情况下,此文件记录将仅生成一个记录,即结果集中的1月份。
所以,
是个好主意[编辑] 按要求添加更多细节
月份表
MonthStartDate,MonthEndDate
文件表
CaseOpenDate,CaseCloseDate
MonthsTable Left Outer Join FilesTable
ON
CaseOpenDate&lt; = MonthEndDate
和
(CaseCloseDate&gt; MonthEndDate或CaseCloseDate IS NULL)
希望有所帮助。