`
liliang_xf
  • 浏览: 64229 次
  • 性别: Icon_minigender_1
  • 来自: 湖北
社区版块
存档分类
最新评论
  • yangqk1: 不知道楼主还在关注这个么,你做的这个项目还在继续么。我真正研究 ...
    webim
  • 周超亿: 你好,我想请问下, http://code.faqee.com ...
    webim
  • 周超亿: 你好,能不能把项目打包发给我一份,谢谢 Email:zhouc ...
    webim
  • liliang_xf: SQL子查询,连接查询,数据汇总,GROUP BY,ORDER ...
    sql的
  • liliang_xf: http://www.ibm.com/developerwor ...
    webim
阅读更多

 

SQL查询重复数据和清除重复数据[转]
2009-08-16 21:53

有例表:emp

emp_no   name    age     
    001           Tom      17     
    002           Sun       14     
    003           Tom      15     
    004           Tom      16

要求:

列出所有名字重复的人的记录

(1)最直观的思路:要知道所有名字有重复人资料,首先必须知道哪个名字重复了:

select   name   from   emp       group   by   name     having   count(*)>1


所有名字重复人的记录是:

select   *   from   emp 
    where name   in   (select   name   from   emp group   by   name having count(*)>1)

(2)稍微再聪明一点,就会想到,如果对每个名字都和原表进行比较,大于2个人名字与这条记录相同的就是合格的 ,就有

select   *   from   emp   where   (select   count(*)   from   emp   e    where   e.name=emp.name)   >1

--注意一下这个>1,想下如果是 =1,如果是 =2 如果是>2 如果 e 是另外一张表 而且是=0那结果 就更好玩了:)

这个过程是 在判断工号为001的 人 的时候先取得 001的 名字(emp.name) 然后和原表的名字进行比较 e.name

注意e是emp的一个别名。

再稍微想得多一点,就会想到,如果有另外一个名字相同的人工号不与她他相同那么这条记录符合要求:

select   *   from   emp     
    where   exists     
                  (select   *   from   emp   e    where   e.name=emp.name   and   e.emp_no<>emp.emp_no)

此思路的join写法:

select   emp.*       from   emp,emp e
        where emp.name=e.name and emp.emp_no<>e.emp_no/**/
/*     这个语句较规范的   join   写法是     
select emp.* from   emp   inner join emp   e     on emp.name=e.name and emp.emp_no<>e.emp_no     
但个人比较倾向于前一种写法,关键是更清晰     */     
b、有例表:emp     
name     age     
Tom       16     
Sun        14     
Tom       16     
Tom       16

----------------------------------------------------清除重复----------------------------------------------------
过滤掉所有多余的重复记录 
(1)我们知道distinct、group by 可以过滤重复,于是就有最直观的 

select   distinct   *   from   emp     或     select   name,age   from   emp   group   by   name,age

获得需要的数据,如果可以使用临时表就有解法: 

select   distinct   *   into   #tmp    from   emp   
    delete   from   emp   
    insert   into   emp   select   *   from   #tmp

(2)但是如果不可以使用临时表,那该怎么办? 
我们观察到我们没办法区分数据(物理位置不一样,对 SQL Server来说没有任何区别),思路自然是想办法把数据区分出来了,既然现在的所有的列都没办法区分数据,唯一的办法就是再加个列让它区分出来,加什么列好?最佳选择是identity列: 

alter   table   emp   add   chk   int   identity(1,1)

表示例: 

name   age   chk     
    Tom     16     1     
    Sun      14     2     
    Tom     16     3     
    Tom     16     4

重复记录可以表示为: 

select   *   from   emp where (select   count(*)   from   emp   e   where   e.name=emp.name)>1

要删除的是: 

delete   from   emp 
    where (select   count(*)   from   emp   e     where   e.name=emp.name   and   e.chk>=emp.chk)>1 

再把添加的列删掉,出现结果。 

alter   table   emp   drop   column   chk


(3)另一个思路: 
视图 

select   min(chk) from   emp group   by   name having   count(*)   >1

获得有重复的记录chk最小的值,于是可以 

delete from   emp where chk   not   in (select min(chk) from   emp group   by   name)

写成join的形式也可以: 

(1)有例表:emp 

emp_no    name    age     
    001            Tom      17     
    002            Sun       14     
    003            Tom      15     
    004            Tom      16

◆要求生成序列号 
(1)最简单的方法,根据b问题的解法: 

alter   table   emp   add   chk   int   identity(1,1)   或   
    select   *,identity(int,1,1)   chk   into   #tmp   from   emp

◆如果需要控制顺序怎么办? 

select   top   100000   *,identity(int,1,1)   chk   into   #tmp   from   emp   order   by   age

(2) 假如不可以更改表结构,怎么办? 
如果不可以唯一区分每条记录是没有办法的,在可以唯一区分每条记录的时候,可以使用a 中的count的思路解决这个问题 

select   emp.*,(select   count(*)   from   emp   e   where   e.emp_no<=emp.emp_no)   
    from   emp   
    order   by   (select   count(*)   from   emp   e   where   e.emp_no<=emp.emp_no)

 

分享到:
评论
1 楼 liliang_xf 2010-01-11  
SQL子查询,连接查询,数据汇总,GROUP BY,ORDER BY子句的使用
2009-04-14 22:49
/*1、子查询的使用*/

/*(1)查找在财务部工作的雇员的情况*/
select *
from employees
where departmentid in
(select departmentid
from departments
where departmentname='财务部')
go

/*(2)查找所有收入在2500元以下的雇员的情况*/
select*
from employees
where employeeid in
(select employeeid
from salary
where income<2500)
go

select name,salary.*
from employees,salary
where employees.employeeid=salary.employeeid and
income<2500
go

/*(3)查找财务部年龄不低于研发部雇员年龄的雇员的姓名*/
select*
from employees
where departmentid in
(select departmentid
from departments
where departmentname='财务部')
and birthday!>all(select birthday
from employees
where departmentid in
(select departmentid
from departments
where departmentname='研发部'))
go
/*显示财务部和研发部人员的姓名生日部门名*/
select name,birthday,departmentname
from employees,departments
where employees.departmentid=departments.departmentid and
(departmentname='研发部'or
departmentname='财务部')
go
select name,birthday,departmentname
from employees,departments
where employees.departmentid=departments.departmentid and
departmentname in ('研发部','财务部')
go

/*(4)查找比所有财务部的雇员收入都高的雇员的姓名*/
select*
from employees
where employeeid in
(select employeeid
from salary
where income>all
(select income
from salary
where employeeid in
(select employeeid
from employees
where departmentid in
(select departmentid
from departments
where departmentname like'财务部'))))
go

/*(5)查找所有年龄比研发部雇员年龄都大的雇员的姓名*/
select *
from employees
where birthday<all
(select birthday
from employees
where departmentid in
(select departmentid
from departments
where departmentname='研发部'))
go

/*2、连接查询的使用*/
/*(1)查找每个雇员的情况以及其薪水的情况*/
select employees.*,salary.*
from employees,salary
where employees.employeeid=salary.employeeid
go

/*(2)查找每个雇员的情况及其工作部门的情况*/
select employees.*,departments.*
from employees,departments
where employees.departmentid=departments.departmentid
go

/*(3)查找财务部收入在2200元以上的雇员姓名及其薪水详情*/
select name,salary.*
from employees,salary,departments
where employees.employeeid=salary.employeeid and
employees.departmentid=departments.departmentid and
departmentname='财务部' and income>2200
go

/*(4)查找研发部在1966年以前出生的雇员姓名及其薪水详情*/
select employees.*,salary.*
from employees,salary,departments
where employees.employeeid=salary.employeeid and
employees.departmentid=departments.departmentid and
departmentname='研发部'and
birthday<'1966' /*注意1966必须有单引号*/
go


/*3、数据汇总*/

/*(1)求财务部雇员的平均收入*/
select avg(income) as '财务部平均收入'
from salary
where employeeid in
(select employeeid
from employees
where departmentid in
(select departmentid
from departments
where departmentname='财务部'))
go

/*(2)查询财务部雇员的最高收入和最低收入*/
select max(income)as'最高收入',min(income)as'最低收入'
from salary
where employeeid in
(select employeeid
from employees
where departmentid in
(select departmentid
from departments
where departmentname='财务部'))
go

/*(3)求财务部雇员的平均实际收入*/
select avg(income-outcome)as'平均实际收入'
from salary
where employeeid in
(select employeeid
from employees
where departmentid in
(select departmentid
from departments
where departmentname='财务部'))
go

/*(4)查询财务部雇员的最高和最低实际收入*/
select max(income-outcome)as'最高收入',min(income-outcome)as'最低收入'
from salary
where employeeid in
(select employeeid
from employees
where departmentid in
(select departmentid
from departments
where departmentname='财务部'))
go

/*(5) 求财务部雇员的总人数*/
select count(employeeid)as'财务部总人数'
from employees
where departmentid in
(select departmentid
from departments
where departmentname='财务部')
go

/*(6)统计财务部收入在2500元以上雇员的人数*/
select count(employeeid)as'财务部收入在2500元以上总人数'
from employees,salary,departments
where employees.employeeid=salary.employeeid and
employees.departmentid=departments.departmentid and
income>'2500'and departmentname='财务部'
go

select count(employeeid)as'财务部收入在2500元以上总人数'
from employees
where departmentid in
(select departmentid
from departments
where departmentname='财务部'in
(select departmentname='财务部'
from departments
where departmentid in
(select departmentid
from employees
where employeeid in
(select employeeid
from salary
where income>'2500'))))
go

/*4、GROUP BY,ORDER BY子句的使用*/

/*(1)求各部门的雇员数*/
select count(employeeid)as'各部门的雇员人数 '
from employees
group by departmentid
go

/*(2)统计各部门收入在2000元以上雇员的人数*/
select count(employeeid)as'各部门入在2000元以上的人数 '
from employees
where employeeid in
(select employeeid
from salary
where income>'2000')
group by departmentid
go

/*(3)将各雇员的情况按收入由低到高排列*/
select employees.*,salary.*
from employees,salary
where employees.employeeid=salary.employeeid
order by income
go

/*(4)将各雇员的情况按出生时间先后排列*/
select*
from employees
order by birthday /*由大到小*/
go

一、ORDER BY是一个可选的子句,它允许你根据指定要order by的列来以上升或者下降的顺序来显示查询的结果,它不需要查询结果中出现order by的栏位.
更改Order by里的栏位只会影响查询结果的顺序,而不影响查询出的记录总数,和每条记录的内容.

二、group by 从英文里理解就是分组。必须有“聚合函数”来配合才能使用,使用时至少需要一个分组标志字段。

什么是“聚合函数”?
像sum()、count()、avg()等都是“聚合函数”
使用group by 的目的就是要将数据分类汇总。

一般如:
    select 单位名称,count(职工id),sum(职工工资) form [某表]
    group by 单位名称
    这样的运行结果就是以“单位名称”为分类标志统计各单位的职工人数和工资总额。

相关推荐

    通过SqlCmd执行超大SQL文件

    ##通过sqlcmd执行sql文件 由于sql文件过大,超过了100M,再数据库的窗口执行,结果超出内存了,对于特别大的sql文件可以使用sqlcmd进行执行 ###1.打开cmd窗口 运行–cmd–进入到sql文件所在的文件夹。 如果是win7可...

    java sql操作工具类 java sql操作工具类

    java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作...

    SQLServer_2000-2008_R2查询智能分析器RedGate_SQL_Prompt_V5.3.4.1_Crack_Keygen破解教程注册机免费

    在我个人编写SQL脚本时,至少会把SQL的格式排列成易于阅读的,因为其他人会阅读到你的SQL,无论是在程序中或是脚本文件中,良好的排版不仅让人看起来赏心悦目,在和他人之间做交流时也省时省力,不会因为揉成一团的...

    SQL优化 SQL优化软件 SQL优化工具

    SQL优化是数据库管理中的关键环节,它涉及到提升查询性能、减少资源消耗以及改善系统整体效率。SQL优化软件和工具能够帮助数据库管理员(DBA)和开发人员找出性能瓶颈,优化查询逻辑,从而提高数据库系统的响应速度...

    SQLPrompt5.3破解

    本人在Windows7 64位+SQL Server 2012环境下测试通过(系统是全新安装) 使用方法: 1,安装SQLPrompt v5.3,这个不多说。 2,安装完毕后,断开网络连接。 3,打开Visual Studio或者SQL Server Management Studio(版本...

    SQLPrompt for SQLServer2016 智能提示插件 SQL2016 提示

    SQLPrompt for SQLServer2016 智能提示插件 SQL2016 提示 SQLPrompt最新版本 绿色版 SQL Prompt 是一款拥有SQL智能提示功能的SQL Server和VS插件。SQL Prompt能根据数据库的对象名称,语法和用户编写的代码片段自动...

    sqlserver自动生成sql语句工具sqlserver转oracle

    在IT行业中,数据库管理系统是核心组成部分,SQL Server和Oracle分别是微软和甲骨文公司推出的两款广泛应用的关系型数据库系统。在企业级应用中,有时需要在不同的数据库系统间进行数据迁移或兼容性处理,这就涉及到...

    sql server 导入超大SQL脚本文件

    SQL Server 导入超大 SQL 脚本文件 SQL Server 是一种关系型数据库管理系统,广泛应用于各种行业。然而,在实际应用中,我们经常会遇到导入超大 SQL 脚本文件的问题。本文将介绍如何使用 osql 工具来导入超大 SQL ...

    Android通过webservice连接Sqlserver实例

    在Android开发中,有时我们需要与远程数据库进行交互,例如SQLServer。这个场景通常是通过Web服务,如WebService来实现。本文将详细介绍如何在Android应用中利用WebService接口连接到SQLServer数据库,实现数据的增...

    SQL SQLPrompt 9 SQL 2016/2017可用

    SQL Prompt是Redgate Software开发的一款高效SQL代码编辑工具,它为SQL Server的开发人员提供了智能提示、格式化、重构和代码分析等功能,极大地提升了编写和维护SQL代码的效率。SQL Prompt 9是该系列的最新版本,...

    SQL 语法 SQL 总结 SQL教程

    SQL 基础 SQL 首页 SQL 简介 SQL 语法 SQL select SQL distinct SQL where SQL AND & OR SQL Order By SQL insert SQL update SQL delete SQL 高级 SQL Top SQL Like SQL 通配符 SQL In SQL Between ...

    SQLMonitor oracle跟踪SQL工具

    《SQLMonitor:Oracle数据库SQL跟踪与分析利器》 在IT行业中,数据库的高效管理与优化是至关重要的。针对Oracle数据库,有一款名为SQLMonitor的工具,它专为跟踪和监控SQL语句而设计,帮助开发者和DBA们找出程序...

    AI自动生成SQL语句的开源代码 sqlcoder-main.zip

    开源的AI自动生成SQL语句源代码,这款SQLCoder-70B-Alpha在文本到SQL的转换能力上超越了包括GPT-4在内的所有通用模型,它能更准确地理解你的需求,并生成相应的SQL查询。SQLCoder2和SQLCoder-7B模型已经向公众开放,...

    Sql Server 2014 安装包

    Sql Server 2014 安装包 SQL Server 2014是微软推出的一款关系型数据库管理系统,它在企业级数据管理和分析领域扮演着重要的角色。此安装包包含两个主要组件:SQL Management Studio和SQL Server Express。 1. **...

    sqlserver驱动包 jdbc驱动 sqljdbc.jar和sqljdbc4.jar

    SQL Server驱动包是用于Java应用程序通过JDBC(Java Database Connectivity)接口与Microsoft SQL Server数据库进行交互的必备组件。本文将详细介绍这两个重要的驱动文件——sqljdbc.jar和sqljdbc4.jar,以及如何...

    sqlserver驱动包:sqljdbc4.jar

    SQL Server驱动包`sqljdbc4.jar`是微软官方提供的Java数据库连接器(JDBC),用于在Java应用程序中与Microsoft SQL Server进行通信。JDBC是Java编程语言中的一个标准API,它使得开发人员能够以标准化的方式访问各种...

    sqlservr32和sqlservr64.zip

    标题中的"sqlservr32和sqlservr64.zip"指的是SQL Server 2005服务中的两个关键组件,`sqlservr32.exe`和`sqlservr64.exe`。这两个文件是SQL Server服务的核心执行文件,分别对应于32位和64位操作系统。在Windows 8和...

    SQLTracker,抓取sql语句的工具

    SQLTracker是一款专为数据库操作监控设计的工具,它在IT领域中主要用于跟踪和记录SQL语句的执行情况。SQL(Structured Query Language)是用于管理关系数据库的编程语言,包括查询、更新、插入和删除数据等操作。SQL...

    sql server2019安装包

    SQL Server 2019是Microsoft推出的一款关系型数据库管理系统,是SQL Server系列中的一个重要版本。它提供了强大的数据存储、处理和分析能力,广泛应用于企业级数据库应用开发和数据分析。在本安装包中,主要包含的是...

    Oracle Sql语句转换成Mysql Sql语句

    在数据库管理领域,Oracle SQL和MySQL SQL是两种广泛使用的SQL方言,它们在语法和功能上存在一定的差异。当需要将一个基于Oracle SQL的应用程序迁移到MySQL环境时,就需要进行SQL语句的转换工作。本项目提供了一个...

Global site tag (gtag.js) - Google Analytics