日期:2014-05-18  浏览次数:20575 次

求一sql语句,删除重复记录
SQL code
CREATE TABLE STU
(
    stu_number varchar(20),
    stu_name varchar(20),
    stu_age int
)
INSERT INTO STU 
SELECT '110','张三',10 UNION 
SELECT '110','张三',10 UNION 
SELECT '111','李四',11 UNION 
SELECT '111','李四',11 UNION 
SELECT '112','王五',10


------解决方案--------------------
SQL code

CREATE TABLE STU
(
    stu_number varchar(20),
    stu_name varchar(20),
    stu_age int
)

INSERT INTO STU 
SELECT '110','张三',10 UNION all
SELECT '110','张三',10 UNION all 
SELECT '111','李四',11 UNION all 
SELECT '111','李四',11 UNION all
SELECT '112','王五',10

select * from STU

stu_number           stu_name             stu_age
-------------------- -------------------- -----------
110                  张三                   10
110                  张三                   10
111                  李四                   11
111                  李四                   11
112                  王五                   10

delete STU 
from 
(select row_number() over(partition by stu_number,stu_name,stu_age order by (select 0)) rn, * from STU) STU
where rn>1

select * from STU

stu_number           stu_name             stu_age
-------------------- -------------------- -----------
110                  张三                   10
111                  李四                   11
112                  王五                   10

------解决方案--------------------
SQL code
CREATE TABLE STU
(
    stu_number nvarchar(20),
    stu_name nvarchar(20),
    stu_age int
)
INSERT INTO STU 
SELECT '110',N'张三',10 UNION all
SELECT '110',N'张三',10 UNION all
SELECT '111',N'李四',11 UNION all
SELECT '111',N'李四',11 UNION all
SELECT '112',N'王五',10
go
alter table  STU add ID int identity
go

DELETE  STU where ID not in(select min(ID) from  STU group by stu_number,stu_name,stu_age)
go

alter table  STU drop COLUMN ID
GO
SELECT * FROM STU

/*
stu_number    stu_name    stu_age
110    张三    10
111    李四    11
112    王五    10
*/
DROP TABLE STU

------解决方案--------------------
SQL code

CREATE TABLE studentage
(
    tid int identity(1,1) not null,
    primary key(tid),
    stu_number nvarchar(20),
    stu_name nvarchar(20),
    stu_age int
)
--select * from studentage
--drop table studentage
--delete studentage
INSERT INTO studentage values('110',N'张三',10 )
INSERT INTO studentage values('110',N'张三',10 )
INSERT INTO studentage values('111',N'李四',11 )
INSERT INTO studentage values('111',N'李四',11 )
INSERT INTO studentage values('112',N'王五',10 )

/*select stu_number as 学生编号,stu_name as 学生姓名,stu_age as 学生年龄
   
from studentage group by stu_number,stu_name,stu_age*/

delete studentage where tid not in(select max(tid) from studentage group by stu_number,stu_name,stu_age)

select * from studentage

------解决方案--------------------
SQL code

IF OBJECT_ID('STU','U') IS NOT NULL
   DROP TABLE STU
GO
CREATE TABLE STU
(
    stu_number varchar(20),
    stu_name varchar(20),
    stu_age int
)
INSERT INTO STU 
SELECT '110','张三',10 UNION ALL
SELECT '110','张三',10 UNION ALL
SELECT '111','李四',11 UNION ALL
SELECT '111','李四',11 UNION ALL
SELECT '112','王五',10
--方法一使用临时表
select distinct stu_number,stu_name,stu_age into #t from STU
delete stu
insert into stu select * from #t
select * from stu
--方法二
delete a from (select *,row=row_number() over(partition by stu_number,stu_name,stu_age order by getdate()) from stu) a where row>1
/*
stu_number           stu_name             stu_age
-------------------- -------------------- -----------
110                  张三                   10
111                  李四                   11
112                  王五                   10

(3 行受影响)
*/

------解决方案-----------------