在上周的人们的帮助下,我能够完成一个相当复杂的请求。但现在我被要求(或建议)将查询分解为多个简单查询,而不是复杂查询。 当前查询是:
select i.IncidentNumber,
r.unit_type,
r.unit,
r.arv_dttm as Onscene_time,
r.clr_dttm as Clear_time,
datediff(minute, arv_time, clr_time) as time_diff
from (select CADIncidentNumber,
unit_type, unit,
arv_dttm,
clr_dttm,
arv_time,
clr_time,
sum(case when unit_type = 'Ambulance' then 1 else 0 end) over (partition by CADIncidentNumber) as ambulance_cnt,
count(*) over (partition by CADIncidentNumber) as cnt
from dw_prod.dbo.vw_unit_response
where CallTypeGrp2 = 'ALS'
and unit_type in ('Ambulance', 'Medic', 'Paramedic Engine', 'Paramedic Truck', 'Paramedic Tower', 'Paramedic Rescue Engine', 'Paramedic Brush Engine', 'Paramedic Rescue Squad')
and DATEDIFF(DAY, arv_dttm,GETDATE()) < 8
and datediff(minute, arv_time, clr_time) > 5
) r
left join INC_UnitInformation u on u.IncidentNumber = r.CADIncidentNumber
left join INC_Incident i on i.IncidentNumber = r.CADIncidentNumber
where ambulance_cnt > 0 and cnt >= 2
and not (u.PrimaryRoleOfUnit = 411000 or u.PrimaryRoleOfUnit = 411005)
and r.unit_type not like 'Ambulance'
group by i.IncidentNumber, r.unit_type, r.unit, r.arv_dttm, r.clr_dttm,
r.arv_time,r.clr_time
having COUNT(i.IncidentID) < 2
and sum(case when u.EMSUnitNumber like 'A%' then 1 else 0 end) > 0
order by r.arv_dttm
查询的结果需要在其值中包含“Ambulance”,它现在正在执行。查询结果没有问题。我只是想看看这个复杂的查询是否可以分解为多个连接在一起的小型查询。除了计算和确保救护车价值包含在结果中的功能之外,我认为这不是那么复杂,但我不知道....
谢谢!
答案 0 :(得分:0)
我看到我将查询分解为多个(两个)查询的最明显的方法是获取派生表:
from (select CADIncidentNumber,
unit_type, unit,
arv_dttm,
clr_dttm,
arv_time,
clr_time,
sum(case when unit_type = 'Ambulance' then 1 else 0 end) over (partition by CADIncidentNumber) as ambulance_cnt,
count(*) over (partition by CADIncidentNumber) as cnt
from dw_prod.dbo.vw_unit_response
where CallTypeGrp2 = 'ALS'
and unit_type in ('Ambulance', 'Medic', 'Paramedic Engine', 'Paramedic Truck', 'Paramedic Tower', 'Paramedic Rescue Engine', 'Paramedic Brush Engine', 'Paramedic Rescue Squad')
and DATEDIFF(DAY, arv_dttm,GETDATE()) < 8
and datediff(minute, arv_time, clr_time) > 5
) r
并单独使用它来填充表变量。然后将其替换为主查询中的表变量。
答案 1 :(得分:0)
希望,我理解你的问题。
建议您为内部查询创建一个视图。
*为您的观点使用适当的名称。
create view view_1 select CADIncidentNumber,
unit_type, unit,
arv_dttm,
clr_dttm,
arv_time,
clr_time,
sum(case when unit_type = 'Ambulance' then 1 else 0 end) over (partition by CADIncidentNumber) as ambulance_cnt,
count(*) over (partition by CADIncidentNumber) as cnt
from dw_prod.dbo.vw_unit_response
where CallTypeGrp2 = 'ALS'
and unit_type in ('Ambulance', 'Medic', 'Paramedic Engine', 'Paramedic Truck', 'Paramedic Tower', 'Paramedic Rescue Engine', 'Paramedic Brush Engine', 'Paramedic Rescue Squad')
and DATEDIFF(DAY, arv_dttm,GETDATE()) < 8
and datediff(minute, arv_time, clr_time) > 5 ;
select i.IncidentNumber,
r.unit_type,
r.unit,
r.arv_dttm as Onscene_time,
r.clr_dttm as Clear_time,
datediff(minute, arv_time, clr_time) as time_diff
from view_1 r
left join INC_UnitInformation u on u.IncidentNumber = r.CADIncidentNumber
left join INC_Incident i on i.IncidentNumber = r.CADIncidentNumber
where ambulance_cnt > 0 and cnt >= 2
and not (u.PrimaryRoleOfUnit = 411000 or u.PrimaryRoleOfUnit = 411005)
and r.unit_type not like 'Ambulance'
group by i.IncidentNumber, r.unit_type, r.unit, r.arv_dttm, r.clr_dttm,
r.arv_time,r.clr_time
having COUNT(i.IncidentID) < 2
and sum(case when u.EMSUnitNumber like 'A%' then 1 else 0 end) > 0
order by r.arv_dttm