Метод 1 (Динами c SQL):
CREATE TABLE #temp ( companyid INT, control_number INT )
INSERT INTO #temp
VALUES (5555, 777),
(5555, 720),
(5555, 234),
(111, 345),
(111, 356)
SELECT * FROM #temp
DECLARE @controlNum VARCHAR(MAX)
SELECT @controlNum = ISNULL(@controlNum + ', ', '') + CAST(control_number AS VARCHAR) FROM #temp WHERE companyid = 5555
DECLARE @sql NVARCHAR(MAX) = 'SELECT * FROM #temp WHERE control_number IN ( ' + @controlNum + ')'
EXEC sp_executesql @sql
Метод 2:
DECLARE @temp TABLE( companyid INT, control_number INT )
INSERT INTO @temp
VALUES (5555, 777),
(5555, 720),
(5555, 234),
(111, 345),
(111, 356)
SELECT * FROM @temp
SELECT * FROM @temp WHERE control_number IN (SELECT control_number FROM @temp WHERE companyid = 5555)