;
with cntval
AS
(
SELECT code
, id = MIN(CAST(id AS BINARY(16)))
FROM [table]
where (code = 1 or code =2 )
GROUP BY code
)
SELECT
cast ( max(case con.code when 1 then id end) as uniqueidentifier)
, cast ( max(case con.code when 2 then id end) as uniqueidentifier)
FROM
cntval con