从上到下选择唯一的值集

时间:2015-09-06 10:11:29

标签: sql sql-server database

我有一个包含特定顺序记录的表,我想选择没有具有重复值的列的唯一行。

像这是我的表

enter image description here

从上到下,在每个集合中(按Column2排序)如果以前没有选择,我需要选择唯一集合。

上表的结果将是这样的

enter image description here

我尝试使用group byRow_NumberCommon Table Expression但都徒劳无功。

我需要单个查询,没有循环或光标。

2 个答案:

答案 0 :(得分:0)

您的表会错过一列来识别每个块并保持一个块中的顺序。 您可以使用此类查询添加列

select id = identity(int,1,1), *
into new_table
from old_table

然后您可以使用此查询

with cte as (
Select id, c1, c2, chunck = id/4, rn = row_number() over (partion by c1, c2 order by id) 
from new_table
),
cte2 as (
select c1, c2,  rn2 = row_number() over (partion by chunk order by id)
from cte
where rn=1
)
select c1, c2 
from cte2
where rn2 = 1

答案 1 :(得分:0)

对我而言,这是一个非常冗长的答案,我会使用一个动态循环'这将压缩大部分代码,但你说循环是不可能的,所以......我们走了:

create table uniq
(
row_id int identity(1,1),
column1 int,
column2 int,
value as (convert(varchar,column1)+''+convert(varchar,column2))
)
insert into uniq (column1,column2) values (1,1)
insert into uniq (column1,column2) values (2,2)
insert into uniq (column1,column2) values (3,3)
insert into uniq (column1,column2) values (4,4)
insert into uniq (column1,column2) values (3,1)
insert into uniq (column1,column2) values (2,2)
insert into uniq (column1,column2) values (1,3)
insert into uniq (column1,column2) values (4,4)
insert into uniq (column1,column2) values (4,1)
insert into uniq (column1,column2) values (3,2)
insert into uniq (column1,column2) values (2,3)
insert into uniq (column1,column2) values (1,4)
insert into uniq (column1,column2) values (4,1)
insert into uniq (column1,column2) values (3,2)
insert into uniq (column1,column2) values (2,3)
insert into uniq (column1,column2) values (1,4)
insert into uniq (column1,column2) values (4,1)
insert into uniq (column1,column2) values (3,2)
insert into uniq (column1,column2) values (2,3)
insert into uniq (column1,column2) values (1,4)

select value into #1 from uniq where row_id = 1
go
insert into #1
select case when 
(select value from uniq where row_id = 5) not in (select value from #1) 
then 
(select value from uniq where row_id = 5)
else
(
select case when 
(select value from uniq where row_id = 6) not in (select value from #1) 
then 
(select value from uniq where row_id = 6)
else
(
select case when 
(select value from uniq where row_id = 7) not in (select value from #1) 
then 
(select value from uniq where row_id = 7)
else
(select value from uniq where row_id = 8)
end
)
end
)
end
from uniq where row_id = 5
go
insert into #1
select case when 
(select value from uniq where row_id = 9) not in (select value from #1) 
then 
(select value from uniq where row_id = 9)
else
(
select case when 
(select value from uniq where row_id = 10) not in (select value from #1) 
then 
(select value from uniq where row_id = 10)
else
(
select case when 
(select value from uniq where row_id = 11) not in (select value from #1) 
then 
(select value from uniq where row_id = 11)
else
(select value from uniq where row_id = 12)
end
)
end
)
end
from uniq where row_id = 5
go
insert into #1
select case when 
(select value from uniq where row_id = 13) not in (select value from #1) 
then 
(select value from uniq where row_id = 13)
else
(
select case when 
(select value from uniq where row_id = 14) not in (select value from #1) 
then 
(select value from uniq where row_id = 14)
else
(
select case when 
(select value from uniq where row_id = 15) not in (select value from #1) 
then 
(select value from uniq where row_id = 15)
else
(select value from uniq where row_id = 16)
end
)
end
)
end
from uniq where row_id = 5
insert into #1
select case when 
(select value from uniq where row_id = 17) not in (select value from #1) 
then 
(select value from uniq where row_id = 17)
else
(
select case when 
(select value from uniq where row_id = 18) not in (select value from #1) 
then 
(select value from uniq where row_id = 18)
else
(
select case when 
(select value from uniq where row_id = 19) not in (select value from #1) 
then 
(select value from uniq where row_id = 19)
else
(select value from uniq where row_id = 20)
end
)
end
)
end
from uniq where row_id = 5

select left(value,1) as column1,right(value,1) as column2 into #2 from #1

select * from #2