当前位置: 代码迷 >> Sql Server >> mssql 按字段重复行取第一行的有关问题
  详细解决方案

mssql 按字段重复行取第一行的有关问题

热度:25   发布时间:2016-04-27 13:26:30.0
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 allselect 1,1,1,2 union allselect 2,2,2,2 union allselect 2,2,2,3 union allselect 3,3,3,3 union allselect 4,4,4,4select 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    d1    1    1    12    2    2    23    3    3    34    4    4    4*/
  相关解决方案