Это может работать для вас:
Настройка:
Create Table #Errs
(
Id Int
);
Insert Into #Errs Values
(104), (110)
Create Table #tbl
(
Id Int,
Info Decimal(6,2)
);
Insert Into #tbl Values
(100,1.0),
(101,2.0),
(102,6.0),
(103,9.0),
(104,15.0),
(105,8.0),
(106,6.0),
(107,8.0),
(108,6.0),
(109,8.0),
(110,6.0)
Запрос
With cte As
(
Select
Id,
LAG(Id,1,Null) OVER(Order By Id) As iderror1,
LAG(Id,2,Null) OVER(Order By Id) As iderror2,
LAG(Id,3,Null) OVER(Order By Id) As iderror3
From #tbl t
)
Select iderror1 As ErrorList From cte Where Id In (Select Id From #Errs)
Union ALL
Select iderror2 As ErrorList From cte Where Id In (Select Id From #Errs)
Union ALL
Select iderror3 As ErrorList From cte Where Id In (Select Id From #Errs)
Order By ErrorList
Если ваши идентификаторы не в последовательном порядке, вы можете использовать (ВыбратьNULL) как ниже:
With cte As
(
Select
Id,
LAG(Id,1,Null) OVER(Order By (Select NULL)) As iderror1,
LAG(Id,2,Null) OVER(Order By (Select NULL)) As iderror2,
LAG(Id,3,Null) OVER(Order By (Select NULL)) As iderror3
From #tbl t
)
Select iderror1 As ErrorList From cte Where Id In (Select Id From #Errs)
Union ALL
Select iderror2 As ErrorList From cte Where Id In (Select Id From #Errs)
Union ALL
Select iderror3 As ErrorList From cte Where Id In (Select Id From #Errs)
Order By ErrorList
Результаты
ErrorList
--------
101
102
103
107
108
109