日期:2014-05-18  浏览次数:20516 次

mssql 按字段重复行取第一行的问题
如题。假设表abcd,有字段a,b,c,d.
现有数据如下:
a b c d
1 1 1 1
1 1 1 2
2 2 2 2
2 2 2 3
3 3 3 3
4 4 4 4

需求为:行与行比较,就针对字段a,b,c如果行行之间这3个字段值都相等,就取第一行的数据。
现需要得到下面的数据:
a b c d
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
请教各位大侠,如何实现?
需要将全部字段都显示出来。谢谢

------解决方案--------------------
SQL code

--> 测试数据:[tbl]
if object_id('[tbl]') is not null drop table [tbl]
create table [tbl]([a] int,[b] int,[c] int,[d] int)
insert [tbl]
select 1,1,1,1 union all
select 1,1,1,2 union all
select 2,2,2,2 union all
select 2,2,2,3 union all
select 3,3,3,3 union all
select 4,4,4,4


select a,b,c,d from(select *,row_number()over(partition by a,b,c 
order by getdate()) as id from tbl)a where id=1

/*
a    b    c    d
1    1    1    1
2    2    2    2
3    3    3    3
4    4    4    4
*/