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

从SQL 2005一个表中输出某字段不重复的所有记录(包含表中所有字段)
我有一个数据库表,具体如下:
A B C D(字段名)
1 2 2 3
1 2 3 4
2 4 4 5
2 3 5 6
2 2 4 7  

我想输出字段A重复的记录中随机一条,通俗的说就是,只要字段A重复的话,我只要重复的其中一条就可以了,各位这个怎么解决啊??烦请大家给出个主意。

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

declare @table table (A int,B int,C int,D int)
insert into @table
select 1,2,2,3 union all
select 1,2,3,4 union all
select 2,4,4,5 union all
select 2,3,5,6 union all
select 2,2,4,7

;with maco as(
select row_number() over (partition by A order by newid()) as rid,
* from @table)

select A ,B ,C ,D from maco where rid=1

------解决方案--------------------
SQL code
select a,min(b),min(c),min(d) from tb group by a

------解决方案--------------------
探讨

这个有点复杂。引用:
SQL code


declare @table table (A int,B int,C int,D int)
insert into @table
select 1,2,2,3 union all
select 1,2,3,4 union all
select 2,4,4,5 union all
select 2,……

------解决方案--------------------
2000多万条数据?你这个表的主键是什么?
------解决方案--------------------
哗,没有主键,那样会害死服务器的