I am creating a view with this query, but since I added the subquery to it, the script has been taking too much time to run without completion.
any help on how to make the query work faster and better?
CREATE VIEW [dbo].[Vw_KPI]
AS
WITH
CTE_ReadyAS
(SELECT
equipmentname, equipmentfleet
,ShiftMonth, ShiftYear,sum(duration)as Duration from [EquipmentStatus] WHERE EquipmentStatusName
='Ready'
groupby
ShiftMonth, ShiftYear,
equipmentname, equipmentfleet) ,
CTE_DelayAS (SELECT equipmentname, equipmentfleet,ShiftMonth, ShiftYear,sum(duration)as
Duration from [EquipmentStatus]
WHERE EquipmentStatusName =Delay
groupby ShiftMonth, ShiftYear,equipmentname, equipmentfleet) ,
CTE_StandbyAS (SELECT equipmentname, equipmentfleet,ShiftMonth, ShiftYear,sum(duration)as
Duration from [EquipmentStatus]
WHERE EquipmentStatusName =Standby
groupby ShiftMonth, ShiftYear,equipmentname, equipmentfleet) ,
CTE_DownAS(SELECT equipmentname, equipmentfleet,ShiftMonth, ShiftYear,sum(duration)as
Duration from [EquipmentStatus]
WHERE EquipmentStatusName =Down
groupby ShiftMonth, ShiftYear,equipmentname, equipmentfleet) ,
selectE.equipmentfleet, E.equipmentname,
E.shiftmonth, E.ShiftYear,ISNULL(r.Duration,0)as ReadyDuration ,ISNULL(d.Duration,0)as DelayDuration,ISNULL(S.Duration,0)as StandbyDuration ,ISNULL(do.Duration,0)as DownDuration
,Past7Days_AP
=(SELECTCASEWHEN(Sum(cast(isnull(d.Duration,0)asbigint)))=(Sum(cast(isnull(r.Duration,0)asbigint))+Sum(cast(isnull(d.Duration,0)asbigint))+Sum(cast(isnull(do.Duration,0)asbigint))+Sum(cast(isnull(S.Duration,0)asbigint)))THEN
0WHEN (Sum(cast(isnull(r.Duration,0)asbigint))+Sum(cast(isnull(d.Duration,0)asbigint))+Sum(cast(isnull(S.Duration,0)asbigint)))=0THEN-1
ELSECast(Round(((Sum(cast(isnull(r.Duration,0)asbigint))+Sum(cast(isnull(d.Duration,0)asbigint))+Sum(cast(isnull(s.Duration,0)asbigint)))*100.0)/
(Sum(cast(isnull(r.Duration,0)asbigint))+Sum(cast(isnull(d.Duration,0)asbigint))+Sum(cast(isnull(S.Duration,0)asbigint))+Sum(cast(isnull(Do.Duration,0)asbigint))),1)asNumeric(4,1))ENDASAP FROM [DownEvents] DE
LEFTJOIN CTE_Ready RON R.equipmentname= E.equipmentname
LEFTJOIN CTE_Delay D ON D.equipmentname= E.equipmentname
LEFTJOIN CTE_Standby S ON S.equipmentname= E.equipmentname
LEFTJOIN CTE_Down Do ON Do.equipmentname= E.equipmentname WHERE enddatetimebetweendateadd(day,-7,enddatetime)andgetdate()and E.EquipmentName= DE.EquipmentName)FROM [EquipmentStatus] Ewith(nolock)
LEFTJOIN CTE_Ready R ON R.equipmentname= E.equipmentnameand R.ShiftMonth
= E.ShiftMonth and R.ShiftYear= E.ShiftYear
LEFTJOIN CTE_Delay D ON D.equipmentname= E.equipmentnameand D.ShiftMonth
= E.ShiftMonth and D.ShiftYear= E.ShiftYear
LEFTJOIN CTE_Standby S ON S.equipmentname= E.equipmentnameand S.ShiftMonth
= E.ShiftMonth and S.ShiftYear= E.ShiftYear
LEFTJOIN CTE_Down Do ON Do.equipmentname= E.equipmentnameand Do.ShiftMonth= E.ShiftMonth
and Do.ShiftYear
= E.ShiftYear
leftjoin [DownEvents] DE WITH(nolock)on DE.EquipmentName= e.EquipmentNameand e.ShiftID
= DE.ShiftID
groupby E.equipmentfleet, E.equipmentname, E.ShiftYear, E.shiftmonth,r.Duration, d.Duration
, S.Duration
, do.Duration