我有一个每小时刷新一次的表,其中包含唯一的case id。我需要返回以下内容:
表格中前一小时但不在当前时间内的案例数量(已关闭)
Date WorkLevel Inventory New Closed
01/30/2018 7:00 AM Intake 55 7 2
01/30/2018 7:00 AM Case Setup 1 21 1 5
01/30/2018 7:00 AM Case Setup 2 0 0 0
01/30/2018 6:00 AM Intake 50 3 8
01/30/2018 6:00 AM Case Setup 1 25 4 5
01/30/2018 6:00 AM Case Setup 2 0 0 0
答案 0 :(得分:0)
试试这个,当然,你的表名和列名取代我的猜测。这是纯粹的小说;我没有建立测试环境,因此可能存在拼写错误或其他愚蠢行为。我们会弄清楚..如果您收到错误或意外结果,请告诉我。
with
opencases as (select * from tblCase
where datediff(hour,casedate,getdate()) = 0),
prevcases as (select * from tblCase where datediff(hour,casedate,getdate()) = 1)
select
case when o.currdate is not null then o.currdate else p.currdate end as 'Date / Time',
case when o.worklevel is not null then o.worklevel else p.worklevel end as 'Work Level',
count(o.case_id) as 'Inventory',
sum(
case
when o.case_id is NOT NULL and p.case_id is NULL then 1 else 0
end) as 'New',
sum(
case
when o.case_id is NULL and p.case_id is NOT NULL then 1 else 0
end) as 'Closed'
from prevcases p
full outer join opencases o
on o.case_id = p.case_id
group by
o.currdate desc,
p.currdate desc,
o.worklevel,
p.worklevel
编辑:不确定如何配置日期和工作级别,但我猜他们也需要CASE语句,如果它们不在两者中,则从正确的派生表中提取它们。