1 public SeqList<int> Purge(SeqList<int> La)
2 {
3 SeqList<int> Lb = new SeqList<int>(La.Maxsize);
4 //将a表中的第1个数据元素赋给b表
5 Lb.Append(La[0]);
6 //依次处理a表中的数据元素
7 for (int i = 1; i <= La.GetLength() - 1; ++i)
8 {
9 int j = 0;
10 //查看b表中有无与a表中相同的数据元素
11 for (j = 0; j <= Lb.GetLength() - 1; ++j)
12 {
13 //有相同的数据元素
14 if (La[i].CompareTo(Lb[j]) == 0)
15 {
16 break;
17 }
18 }
19 //没有相同的数据元素,将a表中的数据元素附加到b表的末尾。
20 if (j > Lb.GetLength() - 1)
21 {
22 Lb.Append(La[i]);
23 }
24 return Lb;
25 }
26 }
1 select distinct * into #Tmp from tableName
2 drop table tableName
3 select * into tableName from #Tmp
4 drop table #Tmp
我们分别在三种数据库中看一下处理办法,就是通常我们用的Sqlserver2000,Sqlserver2005,Oracle 10g.
方法一:
SELECT 序号=
(SELECT COUNT(客户编号) FROM 客户 AS a WHERE a.客户编号<= b.客户编号),
客户编号,公司名称 FROM 客户 AS b ORDER BY 1;
方法二:
SELECT 序号= COUNT(*),
a.客户编号, a.公司名称 FROM 客户 AS a, 客户 AS b
WHERE a.客户编号>= b.客户编号 GROUP BY a.客户编号, b.公司名称 ORDER BY 序号;
方法一:
SELECT RANK() OVER (ORDER BY 客户编号 DESC) AS 序号, 客户编号,公司名称 FROM 客户;
方法二:
WITH TABLE AS
(SELECT ROW_NUMBER() OVER (ORDER BY 客户编号 DESC) AS 序号, 客户编号,公司名称 FROM 客户)
SELECT * FROM TABLE
WHERE 序号 BETWEEN 1 AND 3;
select * from test;
select * from test group by id having count(*)>1
select * from test group by id
select distinct * from test
delete from test a where a.rowid!=(select max(rowid) from test b where a.id=b.id);
扯远了,回到原来的问题,除了采用数据结构的思想来处理,因为数据库特有的事务处理,能够把数据缓存在线程池里,这样也相当于临时表的功能,所以,我们还可以用游标来解决删除重复记录的问题。
1 declare @max int,
2 @id int
3 declare cur_rows cursor local for select id ,count(*) from test group by id having count(*) > 1
4 open cur_rows
5 fetch cur_rows into @id ,@max
6 while @@fetch_status=0
7 begin
8 select @max = @max -1
9 set rowcount @max --让这个时候的行数等于少了一行的统计数,想想看,为什么
10 delete from test where id = @id
11 fetch cur_rows into @id ,@max
12 end
13 close cur_rows
14 set rowcount 0