I have a table like this:
id col1 col2
----------- ---- -----------
1 A 5
2 A 6
3 B 5
4 B
try the following using cursor:
create table #final (id int, col1 varchar(10), col2 int)
declare @id int, @col1 varchar(10), @col2 int
declare cur cursor for select id, col1, col2 from #t order by newid()
open cur
fetch next from cur into @id, @col1, @col2
while @@FETCH_STATUS = 0
begin
if (@col1 in (select col1 from #final) or @col2 in (select col2 from #final))
begin
fetch next from cur into @id, @col1, @col2
continue
end
insert into #final
select id, @col1, @col2 from #t where col1 = @col1 and col2 = @col2 and id = @id
fetch next from cur into @id, @col1, @col2
end
close cur
deallocate cur
select * from #final order by id
drop table #final
Result:
Try this query:
SELECT id, col1, col2
FROM ( SELECT * ,
RANK() OVER ( PARTITION BY col2 ORDER BY col2 ) Row#
FROM #t
) x
WHERE x.Row# = 1
This is not an easy one to solve through straight SQL. I figured I'd give it a crack using a recursive CTE. This spits out all possible combinations, but... in one field called "path" here. Perhaps it will present a way forward using straight tsql.
With recCTE AS
(
SELECT
col1,
col2,
0 as depth,
CAST(col1 + '|' + CAST(col2 AS varchar(10)) AS VARCHAR(30)) as path
FROM t
UNION ALL
SELECT
t1.col1,
t1.col2,
rec.depth + 1,
CAST(rec.path + '>' + t1.col1 + '|' + cast(t1.col2 as varchar(10)) as varchar(30))
FROM t t1
INNER JOIN recCTE rec
ON rec.path NOT LIKE '%|' + CAST(t1.col2 as varchar(10)) + '%'
AND rec.path NOT LIKE '%' + CAST(t1.col2 as varchar(10)) + '|%'
WHERE depth + 1 <= 3
)
SELECT *
FROM recCTE
WHERE depth = 2
SELECT id, col1, col2
FROM ( SELECT * ,
ROW_NUMBER() OVER ( PARTITION BY col2 ORDER BY col2 ) Row#
FROM #t
) x
WHERE x.Row# = 1