发布网友 发布时间:2022-04-23 07:06
共3个回答
懂视网 时间:2022-04-08 07:09
--第一个案例
CREATE TABLE t
(
id INT IDENTITY(1,1) PRIMARY KEY,
a VARCHAR(10),
b VARCHAR(10)
)
--插入数据
INSERT INTO t
SELECT ‘aa‘,‘bb‘ UNION ALL
SELECT ‘a1‘,‘bgb‘ UNION ALL
SELECT ‘aa‘,‘bb‘ UNION ALL
SELECT ‘a2‘,‘bb‘ UNION ALL
SELECT ‘aa3‘,‘beeb‘ UNION ALL
SELECT ‘aa‘,‘bb‘ UNION ALL
SELECT ‘a2‘,‘bb‘
SELECT * FROM t ORDER BY a
--第一种删除重复的方法(找出重复的项的最小标识,删除不包含最小标识的数据)
DELETE t WHERE id NOT IN(
SELECT min(id) id FROM t GROUP BY a,b
)
--第二种删除重复的方法(分区排序,给每个分区排序的结果给个行号,把行号大于1的删除)
DELETE t FROM (
SELECT *,ROW_NUMBER() OVER (PARTITION BY a,b ORDER BY ID) row FROM t)
WHERE ROW>1
--第二个案例
CREATE TABLE user_student(id decimal(18,0) identity(1,1),st_name nvarchar(30),class nvarchar(10),score decimal(18,2))
insert into user_student(st_name,class,score)
values(‘张三‘,‘甲‘,‘90‘)
insert into user_student(st_name,class,score)
values(‘张四‘,‘甲‘,‘65‘)
insert into user_student(st_name,class,score)
values(‘张五‘,‘甲‘,‘88‘)
insert into user_student(st_name,class,score)
values(‘李三‘,‘乙‘,‘97‘)
insert into user_student(st_name,class,score)
values(‘李四‘,‘乙‘,‘88‘)
insert into user_student(st_name,class,score)
values(‘李五‘,‘乙‘,‘78‘)
insert into user_student(st_name,class,score)
values(‘王三‘,‘丙‘,‘86‘)
insert into user_student(st_name,class,score)
values(‘王四‘,‘丙‘,‘69‘)
insert into user_student(st_name,class,score)
values(‘王五‘,‘丙‘,‘90‘)
select * from user_student
SELECT ST_NAME,CLASS,SCORE
FROM (
SELECT Row_number() OVER(PARTITION BY CLASS ORDER BY SCORE DESC) AS NUM,*
FROM user_student
) AS T
WHERE NUM<=2 --每个班级分数前两名的学生
DELETE user_student FROM user_student WHERE id NOT IN (SELECT max(id) AS id FROM user_student
GROUP BY st_name,class,score) --删除后来添加的重复数据
SqlServer去除重复行
标签:
热心网友 时间:2022-04-08 04:17
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select
* from people
where peopleId in (select peopleId from
people group by peopleId having count(peopleId)
> 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete
from people
where peopleId in (select peopleId from
people group by peopleId having
count(peopleId) > 1)
and rowid not in (select min(rowid) from
people group by peopleId having count(peopleId
)>1)
3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq)
in (select peopleId,seq from vitae group by peopleId,seq having
count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where
(a.peopleId,a.seq) in (select peopleId,seq from vitae group by
peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from
vitae group by peopleId,seq having count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a
where
(a.peopleId,a.seq) in (select peopleId,seq from vitae group by
peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from
vitae group by peopleId,seq having count(*)>1)
(二)
比方说
在A表中存在一个字段“name”,
而且不同记录之间的“name”值有可能会相同,
现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;
Select
Name,Count(*) From A Group By Name Having Count(*) > 1
如果还查性别也相同大则如下:
Select Name,sex,Count(*) From A Group By Name,sex Having
Count(*) > 1
热心网友 时间:2022-04-08 05:35
WITH CTE AS(
SELECT [col1], [col2], [col3], [col4], [col5], [col6], [col7],
RN = ROW_NUMBER()OVER(PARTITION BY col1 ORDER BY col1)
FROM dbo.Table1
)
DELETE FROM CTE WHERE RN > 1