I don't know the logic either, but I took a shot in the dark:declare @a table(col1 varchar(10), col2 varchar)insert into @a select 'a', 1 union all select'a', 5 union all select'a', 7 union all select'b', 1 union all select'b', 3 union all select'c', 1;WITH Cte AS( SELECT col1, col2, ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col2 ASC) AS RowNum FROM @a)SELECT A.Col1, COALESCE(B.Col2, A.Col2) AS col2FROM Cte AS ALEFT OUTER JOIN Cte AS B ON A.col1 = B.col1 AND A.RowNum + 1 = B.RowNumWHERE A.RowNum = 1 OR B.RowNum = 2
If that doesn't work, please explain the logic for how to select the values.