又一个表,如下:
字段: KK a1 a2 a3 .....
内容: 1 3 5 8 .....
2 4 8 8 .....
1 5 5 4 .....
3 2 1 5 .....
... ... ... ....要想只保留KK段值唯一,重复的就不要,比如第3条记录就不要了,有简单快速的办法么?
字段: KK a1 a2 a3 .....
内容: 1 3 5 8 .....
2 4 8 8 .....
1 5 5 4 .....
3 2 1 5 .....
... ... ... ....要想只保留KK段值唯一,重复的就不要,比如第3条记录就不要了,有简单快速的办法么?
解决方案 »
- 向高手请教
- 申明一个@Table ,可以不定义这个@Table的表结构,我把另一个表的结构和数据都传给这个@Table吗?
- 求一存储过程的写法,在线等,先谢了
- exec master..xp_cmdshell net use 问题,谢谢!请进入:)
- 求 SQL 语句
- 如何有效率地根据一个表里的字段取出另一个表里的更多字段
- sp_executesql问题,求帮忙(在线等,给分)
- 哪位dx用过sql server 64-bit了,出现问题????求救!!!!
- ODBC是什么东东?和Access,PowerBuilder,Delphi(数据库功能)有什么区别?用在哪?
- datediff计算出的字段如何在同一个程序中被其他函数应用
- 求助:各位大哥请看我这句 SQL 语句错误在哪里?
- 请教邹建,关于精确的全文检索问题
Select KK,Count(1) Cnt From Table Group By KK Having Count(KK) > 1Fetch Next From curCnt Into @KK,@Cnt
While @@Fetch_Status <> 0
BeginSet @Cnt = @Cnt - 1
Set @@RowCount = @Cnt
Delete Table Where KK = @KK
Set @@RowCount = 0Fetch Next From curCnt Into @KK,@Cnt
End
大概就是这个样子,可能语法会有不对的地方,我没有试过!
思路就是这样的 ̄ ̄ ̄
where not exists(
select * from tb where id<a.id and kk=a.kk)
create index table1 on table1(kk)
再申明游标:
declare @c1 varchar(100),
@c2 varchar(100)
declare atmp1 cursor for
select kk from table1
for update
open atmp1
fetch next from atmp1 into @c1
while @@fetch_status=0
begin
fetch next from atmp1 into @c2
if @c1=@c2
begin
delete from table1 where current of atmp1
end
else
begin
set @c1=@c2
end
end
close atmp1
deallocate atmp1
可以实现你的目的。
表没有唯一键,zjcxc(邹建)的也不行了。
想用视图,最好不要用存储过程、函数什么的,能行么?
其实邹老大说不行,我就觉得够呛能实现了,不过还想放两天再结帖,呵呵。
godelete from tb
where (xh not in
(select min(xh) as tt
from tb group by kk))
goalter table tb drop column [xh]
go
From Table t,
(Select KK,Min(A1) A1 From Table Group By KK) T2 --这里根据KK分组,取出一个最小的
--A1,也可以是其他列,然后再与原表连接查询
Where t.KK = T2.KK And t.A1 = T2.A1
group by num
having count(Name) >1
这样的话就把所有重复的都删除了。