попробуйте этот код
WITH cte_ssrslastexecution
AS ( SELECT ReportPath ,
TimeStart ,
TimeEnd ,
DATEDIFF(DAY, TimeEnd, GETDATE()) Days ,
RequestType ,
UserName ,
Format
FROM ExecutionLog2
WHERE ( ReportPath LIKE @folder )
),
cte_suscriptions
AS ( SELECT USR.UserName AS SubscriptionOwner ,
SUB.ModifiedDate ,
SUB.[Description] ,
SUB.EventType ,
SUB.DeliveryExtension ,
SUB.LastStatus ,
SUB.LastRunTime ,
DATEDIFF(DAY, SUB.LastRunTime, GETUTCDATE()) AS Days ,
SCH.NextRunTime ,
SCH.Name AS ScheduleName ,
CAT.[Path] AS ReportPath ,
CAT.[Description] AS ReportDescription
FROM dbo.Subscriptions AS SUB
INNER JOIN dbo.Users AS USR ON SUB.OwnerID = USR.UserID
INNER JOIN dbo.[Catalog] AS CAT ON SUB.Report_OID = CAT.ItemID
INNER JOIN dbo.ReportSchedule AS RS ON SUB.Report_OID = RS.ReportID
AND SUB.SubscriptionID = RS.SubscriptionID
INNER JOIN dbo.Schedule AS SCH ON RS.ScheduleID = SCH.ScheduleID
-- WHERE CAT.[Path] LIKE N'%Marketing%'
),
cte_last
AS ( SELECT ReportPath ,
MAX(TimeStart) AS TimeStart
FROM ExecutionLog2
WHERE ( ReportPath LIKE @folder )
GROUP BY ReportPath
)
SELECT DISTINCT
a.* ,
CASE WHEN s.ScheduleName IS NULL THEN 'NO'
ELSE 'YES'
END AS Subscriptions
FROM cte_ssrslastexecution a
INNER JOIN cte_last b ON a.ReportPath = b.ReportPath
AND a.TimeStart = b.TimeStart
LEFT JOIN cte_suscriptions s ON a.ReportPath = s.ReportPath
ORDER BY a.Days;