读书人

从SQL 2005一个表中输出某字段不重复的

发布时间: 2012-03-16 16:34:56 作者: rapoo

从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 @tableselect 1,2,2,3 union allselect 1,2,3,4 union allselect 2,4,4,5 union allselect 2,3,5,6 union allselect 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多万条数据?你这个表的主键是什么?
[解决办法]
哗,没有主键,那样会害死服务器的

读书人网 >SQL Server

热点推荐