【问题标题】:SQL Check if a file was in open status during specific timeframeSQL 检查文件在特定时间范围内是否处于打开状态
【发布时间】:2017-05-19 20:55:45
【问题描述】:

我需要查看给定时间范围内的每个月/年,并检查在该月和该年,文件是否处于打开状态。在每个文件上都有一个打开日期和关闭日期,因此对于每个文件,我想检查打开日期的月份和年份是否小于或等于该范围内的每个月/年,然后关闭日期是否为空或有大于该范围内的每个月/年的月/年。但是,我不知道如何通知查询我正在查看的当前月份年份是什么。我的查询如下所示:

-- 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 个月的病假,所以我对这一切都很生疏。谢谢!

【问题讨论】:

  • 我正在使用 MS SQL 服务器

标签: sql sql-server date-range


【解决方案1】:

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

所以, 最好有

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

[编辑] 根据要求添加更多细节

月份表
MonthStartDate, MonthEndDate

文件表
CaseOpenDate, CaseCloseDate

MonthsTable 左外连接文件表
开启
CaseOpenDate 和
(CaseCloseDate > MonthEndDate 或 CaseCloseDate 为 NULL)

希望对您有所帮助。

【讨论】:

  • 谢谢!听起来它可能会满足我的需求。我会试一试!
  • 我将使用什么标准将主日期表连接到打开文件表?
  • 编辑了我的答案以在底部添加更多详细信息。
猜你喜欢
  • 1970-01-01
  • 2021-05-20
  • 2020-07-28
  • 2013-04-16
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 1970-01-01
相关资源
最近更新 更多