Следующий запрос работает
select TOP 100 T.DWH_ID,T.date_time, T.TimeDiff, T.[End Date], T.SPS_Bereich, T.txtName from (
SELECT sto.[DWH_ID]
,sto.[SPS_Bereich]
,FORMAT(sto.[DateTime], 'dd-MM-yyyy HH:mm') as date_time
,sto.[txtName]
,sto.[TimeDiff]
, DATEADD(second,sto.[TimeDiff],FORMAT(sto.[DateTime], 'dd-MM-yyyy HH:mm'))as [End Date]
FROM [Stoerdaten].[sta].[Stoerungen] sto where sto.Classname='Alarm' and sto.TimeDiff>60 ) as T
join [IgnitionServer].[dbo].[scheduled_events_ISTProduction] cal on
((T.date_time between cal.start_date and cal.end_date) and T.[End Date] between cal.start_date and cal.end_date) where cal.typ=1 order by [DWH_ID] desc
Но когда я перехожу на CTE, он не дал мне никакого результата.
Запрос CTE
;with q1 as
(
select TOP 1000 [DWH_ID],
SPS_Bereich ,
FORMAT([DateTime], 'dd-MM-yyyy HH:mm') as date_time,
[txtName],
[TimeDiff]
, DATEADD(second,[TimeDiff],FORMAT([DateTime], 'dd-MM-yyyy HH:mm'))as [End_Date]
FROM [Stoerdaten].[sta].[Stoerungen] where Classname='Alarm' and TimeDiff>60
)
select q1.DWH_ID,
q1.date_time,
q1.TimeDiff, q1.[End_Date], q1.txtName, q1.SPS_Bereich from q1 join [IgnitionServer].[dbo].[scheduled_events_ISTProduction] cal on
((q1.date_time between cal.start_date and cal.end_date) and q1.[End_Date] between cal.start_date and cal.end_date) where cal.typ=1
Я не Я не понимаю, что мне здесь не хватает. Любая помощь приветствуется.