zoukankan      html  css  js  c++  java
  • sql 查询重复数据,删除重复数据,过滤重复数据

    select * from (
    SELECT titleid,count(titleid) c
    FROM [DragonGuoShi].[dbo].[ArticleInfo]
    group by titleid,[CategoryCode]
    having count(titleid)>1)as t
    order by c


    delete [DragonGuoShi].[dbo].[ArticleInfo] where ID not in (
    SELECT max(ID) c
    FROM [DragonGuoShi].[dbo].[ArticleInfo]
    group by titleid ,[CategoryCode]
    )

    Sql查询语句过滤重复的数据

    情况一:表中存在完全重复的的数据,即所有字段内容都是相同的
     
    create table #
    (用户ID int, 姓名 varchar(10), 年龄 int )
    insert into #
    select 111, '张三', 26 union all
    select 222, '李四', 25 union all
    select 333, '王五', 30 union all
    select 111, '张三', 26

    方法: select distinct * from #
     
     情况2:表中存在部分数据重复的字段,即 重复数据中至少有一个字段不重复
     
    create table #
    (用户ID int, 姓名 varchar(10), 年龄 int, 日期 DateTime )
    insert into #
    select 111, '张三', 26 2010-02-23 union all
    select 222, '李四', 25 2010-03-13 union all
    select 333, '王五', 30 2011-03-25 union all
    select 111, '张三', 26 2011-07-07
     
    方法:--当两条重,取日期大的一条 select * from t a where not exists (select 1 from t where a.用户ID=用户ID a.姓名=姓名 and 日期>a.日期)
     
  • 相关阅读:
    002使用代码和未经编译的XMAL文件创建WPF程序
    001使用代码创建WPF应用程序
    制作地图PPT
    数据库基本知识学习(sql server)
    虚拟现实技术对人类是福还是祸?
    计算机中的数学
    软件架构
    extracts
    bootstrap
    omron欧姆龙自动化应用
  • 原文地址:https://www.cnblogs.com/shiyh/p/9273707.html
Copyright © 2011-2022 走看看