从SQL 2005一个表中输出某字段不重复的所有记录(包含表中所有字段)

apiba 2011-07-17 10:34:30
我有一个数据库表,具体如下:
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重复的话,我只要重复的其中一条就可以了,各位这个怎么解决啊??烦请大家给出个主意。
...全文
125 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
apiba 2011-07-21
  • 打赏
  • 举报
回复
设置个自增主键没问题,关键是我问题没解决。假如我设置了主键id,请问大家这个问题怎么解决???[Quote=引用 9 楼 qifachen 的回复:]
哗,没有主键,那样会害死服务器的
[/Quote]
qifachen 2011-07-19
  • 打赏
  • 举报
回复
哗,没有主键,那样会害死服务器的
apiba 2011-07-18
  • 打赏
  • 举报
回复
没有主键,我是从text文档中导入的数据,需要用数据库处理。[Quote=引用 7 楼 maco_wang 的回复:]
2000多万条数据?你这个表的主键是什么?
[/Quote]
叶子 2011-07-17
  • 打赏
  • 举报
回复
2000多万条数据?你这个表的主键是什么?
apiba 2011-07-17
  • 打赏
  • 举报
回复
我现在情况是那个表里面有2000多万条数据,按照朋友你的思路的话,少量的数据可以处理,大量的似乎很吃力。[Quote=引用 5 楼 maco_wang 的回复:]
引用 4 楼 chenwen1988104 的回复:

这个有点复杂。引用 1 楼 maco_wang 的回复:
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……
[/Quote]
叶子 2011-07-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chenwen1988104 的回复:]

这个有点复杂。引用 1 楼 maco_wang 的回复:
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,……
[/Quote]
就是随机排序,取每个A的最上面的。
apiba 2011-07-17
  • 打赏
  • 举报
回复
这个有点复杂。[Quote=引用 1 楼 maco_wang 的回复:]
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……
[/Quote]
apiba 2011-07-17
  • 打赏
  • 举报
回复
这个我尝试过,时可以选择出其中的一条,但是不是随机的,而是取这些字段min(b),min(c),min(d)最小的记录。不符合我这边的实际情况,不过还是谢谢你。[Quote=引用 2 楼 fredrickhu 的回复:]
SQL code

select a,min(b),min(c),min(d) from tb group by a
[/Quote]
--小F-- 2011-07-17
  • 打赏
  • 举报
回复
select a,min(b),min(c),min(d) from tb group by a
叶子 2011-07-17
  • 打赏
  • 举报
回复

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

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧