如何实现 只保留N条相同记录中的一条记录??

flashasp 2005-03-18 11:18:14
在插入表A的过程中,如何实现:

只保留N条相同记录中的一条记录,我不想在查询语句中用distinct,只想在插入时直接保留一条记录,谢谢指点




...全文
279 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
paoluo 2005-03-21
  • 打赏
  • 举报
回复
回复人: flashasp(flashasp) ( ) 信誉:98 2005-03-18 14:02:00 得分: 0


现在加拉个WHERE条件:
select distinct * into #t from (select * from test where t1='1') a---正确
truncate table (select * from test where t1='1') a ---报错,为何
insert test select * from #t
drop table #t



----------------------------------------
这个A为什么要删除,那只是一个别名而已,不用删除的。
truncate table (select * from test where t1='1') a ---报错,为何
这句可以去掉。
TOMATOTO 2005-03-21
  • 打赏
  • 举报
回复
select t1,t2,t3,t4 into #t
from test
group by t1,t2,t3,t4
having count(*) > 1
go
insert into a
select * from #t
godhome_bxs 2005-03-18
  • 打赏
  • 举报
回复
建立两个结构相同的表。表A没有唯一索引,用做保存录入记录;表B有唯一索引,用做最终结果。做读取表A记录的循环插入到表B,如果插入失败,就更新已存在的记录,或者跳过。
flashasp 2005-03-18
  • 打赏
  • 举报
回复
或者
如何查询出B表中具有相同记录的所有记录,如果现有记录
1,1,1
1,2,3,4
1,2,3,4
4,5,6,7
4,5,6,7
9,9,9
-------------------------------------
只要这样的结果也可以:就是将具有多条相同的记录查出来
1,2,3,4
1,2,3,4
4,5,6,7
4,5,6,7

lxzm1001 2005-03-18
  • 打赏
  • 举报
回复
那可以先在插入之前用distinct
flashasp 2005-03-18
  • 打赏
  • 举报
回复
insert into 表A select * from 表B where id in (select max(id) from 表B group by 重复的字段列表) --id为主键
这个只能判断A和B有相同记录的情况下发生的情况,现在是B中根本就不会出现有相同记录的情况,


现在问题是在进入B后才出现的
$扫地僧$ 2005-03-18
  • 打赏
  • 举报
回复
Select Distinct * Into A from B where B.id <> A.id
631799 2005-03-18
  • 打赏
  • 举报
回复
insert into 表A select * from 表B where id in (select max(id) from 表B group by 重复的字段列表) --id为主键
paoluo 2005-03-18
  • 打赏
  • 举报
回复
你的意思是将A表的数据插入B表??那B表中重复的数据怎么来的??
flashasp 2005-03-18
  • 打赏
  • 举报
回复
我不想从插入的角度考语,只想在入库后保留一条记录,
flashasp 2005-03-18
  • 打赏
  • 举报
回复
A有这样的记录
1,2,3
但是当我插入B后,就出现
1,2,3
1,2,3
1,2,3
。。。。。相同的记录,我现在只要一条保留在B中,该如何实现
flashasp 2005-03-18
  • 打赏
  • 举报
回复
Select Distinct * Into A from 表 这样是不行的,
现在问题关键在于:当A插入B时,A中的数据没有重复的现象:
比如A有这样的记录
1,2,3
但是当我插入B后,就出现
1,2,3
1,2,3
1,2,3
。。。。。相同的记录,我现在只要一条保留在B中,该如何实现

paoluo 2005-03-18
  • 打赏
  • 举报
回复
在插入表A的过程中??那可以用Distinct啊。


Select Distinct * Into A from 表
spring_504 2005-03-18
  • 打赏
  • 举报
回复
插入数据的时候,判断表A中是否有该条记录?没有就插入,有就跳过
flashasp 2005-03-18
  • 打赏
  • 举报
回复
现在加拉个WHERE条件:
select distinct * into #t from (select * from test where t1='1') a---正确
truncate table (select * from test where t1='1') a ---报错,为何
insert test select * from #t
drop table #t
flashasp 2005-03-18
  • 打赏
  • 举报
回复
select distinct * into #t from test
truncate table test
insert test select * from #t
drop table #t
搞定
flashasp 2005-03-18
  • 打赏
  • 举报
回复
select t1,t2,t3,t4 from test
group by t1,t2,t3,t4
having count(*) > 1
能查处相同记录的单条记录,但
如何删除上面查询的记录(就是删除相同的记录后还保留一条)

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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