首页 > 数据库 >SQL语句删除2条重复数据一条保留一条

SQL语句删除2条重复数据一条保留一条

时间:2023-02-23 11:14:31浏览次数:44  
标签:语句 rvinfo code dbo his 一条 zsb SQL SELECT

CREATE TABLE test_delete(
name varchar(10),
value INT
);
go
-- 张三100 与 王五80 是有重复的
INSERT INTO test_delete
SELECT '张三', 100
UNION ALL SELECT '张三', 100
UNION ALL SELECT '李四', 80
UNION ALL SELECT '王五', 80
UNION ALL SELECT '王五', 80
UNION ALL SELECT '赵六', 90
UNION ALL SELECT '赵六', 70
go
-- 测试检索数据
SELECT
ROW_NUMBER() OVER (PARTITION BY name, value ORDER BY (SELECT 1) ) AS no,
name,
value
FROM
test_delete
CREATE VIEW tmp_view AS
SELECT
ROW_NUMBER() OVER (PARTITION BY name, value ORDER BY (SELECT 1) ) AS no,
name,
value
FROM
test_delete
--查重
SELECT ear_id,ctm_code,rvi_code,count(1)
FROM [his_hefei].[dbo].[zsb_rvinfo_2022] group by ear_id,ctm_code,rvi_code HAVING COUNT(1) > 1

--创建临时视图
CREATE VIEW tmp_view AS
select ROW_NUMBER() OVER (PARTITION BY ear_id, ctm_code, rvi_code ORDER BY (SELECT 1) ) AS num,
ear_id, ctm_code, rvi_code from [his_hefei].[dbo].[zsb_rvinfo_2022]

--删除重复数据
DELETE FROM tmp_view WHERE num != 1

--释放视图
DROP VIEW tmp_view 

--检查执行结果
select * from [his_hefei].[dbo].[zsb_rvinfo_2022] where ear_id='000' and ctm_code='000C00000724' and rvi_code='000AD28163606'

--剩余数据
select COUNT(1) rvinfo from [his_hefei].[dbo].[zsb_rvinfo]
select COUNT(1) rvinfo_2018 from [his_hefei].[dbo].[zsb_rvinfo_2018]
select COUNT(1) rvinfo_2019 from [his_hefei].[dbo].[zsb_rvinfo_2019]
select COUNT(1) rvinfo_2020 from [his_hefei].[dbo].[zsb_rvinfo_2020]
select COUNT(1) rvinfo_2021 from [his_hefei].[dbo].[zsb_rvinfo_2021]
select COUNT(1) rvinfo_2022 from [his_hefei].[dbo].[zsb_rvinfo_2022]
select COUNT(1) rvinfo_view from [his_hefei].[dbo].[zsb_rvinfo_view]

 

标签:语句,rvinfo,code,dbo,his,一条,zsb,SQL,SELECT
From: https://www.cnblogs.com/mapstar/p/17147201.html

相关文章

  • SQL中模糊查询(like、REGEXP)的用法
    like关键字用法SELECT*FROM`web_game`wherekeywordlike'%动漫%'orkeywordlike'%漫画%'orkeywordlike'%嘿嘿%'orkeywordlike'%直播%'简化用法(使用R......
  • php-mysql 函数练习和使用!
    1.回顾:上篇学习和使用了wamp+eclipse的环境搭建2.这篇将学习一个例子,使用mysql函数来学习3.基本操作  3.1设置编码header("Content-type:text/html;charset=utf-8......
  • Pandas从SQL型数据库读写数据
    Pandas读写SQL型数据库主要用到pandas.read_sql()和DataFrame.to_sql()两个函数。read_sql()read_sql主要有如下几个参数:sql:SQL命令字符串con:连接sql数据库的......
  • mysql查询语句简单优化
    showprocesslist;killID;2. explain分析查询语句explainSELECT*FROMCASE_TESTPCPLEFTJOINSYS_USERSUONSU.USERID=PCP.UPDATE_PERSON......
  • 10个常见的 PostgreSQL 错误及避坑指南
    PostgreSQL作为当下流行的数据库,不少开发者因其开源、可靠、可扩展等特性把它应用到实际的生产环境中,帮助无数PostgreSQL厂商的Percona编制了一个最常见......
  • 10个常见的 PostgreSQL 错误及避坑指南
    PostgreSQL作为当下流行的数据库,不少开发者因其开源、可靠、可扩展等特性把它应用到实际的生产环境中,帮助无数PostgreSQL厂商的Percona编制了一个最常见......
  • 10个常见的 PostgreSQL 错误及避坑指南
    PostgreSQL作为当下流行的数据库,不少开发者因其开源、可靠、可扩展等特性把它应用到实际的生产环境中,帮助无数PostgreSQL厂商的Percona编制了一个最常见......
  • T-SQL——将字符串转换为多列
    目录0.使用ParseName1.具体到拆分为两列2.具体到拆分的指定的列数志铭-2023年2月21日0.使用ParseName注意:ParseName最多只能拆分为四列ParseName只能针对.,所以若......
  • 决战圣地玛丽乔亚Day18----关于mysql死锁+ mysql索引的数据结构相关
    Mysql为什么会产生死锁? 事务A、B分别拿到1,4的独占行锁,然后再去拿4,1的行锁,都会由于另一个事务不释放锁等待,造成了循环等待的局面。(1)同一时刻只能有一个事务持有这......
  • python 把mysql数据导入到execl中
    importpymysqlimportpandasaspddb=pymysql.connect(host='127.0.0.1',user='root',passwd='123456',port=3306,database='world',......