求高手帮忙优化语句
求高手帮忙优化
with hisdata
as (select sum(1) over(partition by usersub.mobile,sub.substation_id,alarm.type,alarm.level,ai.pid
order by usersub.mobile,sub.substation_id,alarm.type,alarm.level,ai.pid,cdatetime ) xh
,sub.pname
,sub.substation_id
,ai.pid
,ai.item_code
,itm.item_name item_name
,his.cdatetime
,(case when sub.sub_type=5 then his.cvalue else his.cvtval end) val
,std.high
,usersub.mobile
,alarm.type
,(case when sub.sub_type=5 then (case when info.admin_code in (1,2) then alarm.alarm_value else 2*alarm.alarm_value end)
when sub.sub_type=6 then (case when info.admin_code in (1,2)
then (case alarm.alarm_value when 6 then 2 when 10 then 4 when 14 then 6 when 24 then 8 end)
else (case alarm.alarm_value when 6 then 4 when 10 then 8 when 14 then 12 when 24 then 16 end) end)
end) alarm_value
,max(cdatetime) over(partition by ai.pid) maxdate
from hisdata_1h his
inner join tai ai
on his.pid=ai.pid
and ai.item_code in (201,311,316)
and his.cdatetime>=trunc(sysdate,'hh')-2.5 and his.cdatetime<=trunc(sysdate,'hh') - 1/24
inner join item itm
on ai.item_code=itm.item_code
inner join substation sub
on ai.substation_id=sub.substation_id
and sub.sub_type in (5,6)
inner join ai ai0
on ai0.substation_id=sub.substation_id
and ai0.item_code in (210,492)
inner join hisdata_1h his0
on ai0.pid=his0.pid
and his0.cdatetime=his.cdatetime