`
liliang_xf
  • 浏览: 64189 次
  • 性别: 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 单位名称
    这样的运行结果就是以“单位名称”为分类标志统计各单位的职工人数和工资总额。

相关推荐

    63.基于51单片机的酒精气体检测器设计(实物).pdf

    63.基于51单片机的酒精气体检测器设计(实物).pdf

    13.基于51单片机的简易计算器设计(仿真).pdf

    13.基于51单片机的简易计算器设计(仿真)

    12.基于单片机的可语音播报的脉搏检测仪设计(实物).pdf

    12.基于单片机的可语音播报的脉搏检测仪设计(实物)

    模块六:云原生训练营-Kubernetes 控制平面组件API Server

    内容概要:本文档详细介绍了Kubernetes控制平面中最为核心的API Server组件。内容涵盖认证、鉴权、准入控制(Mutating、Validating Admission)、限流机制和API服务器的高可用实现等重要方面。通过认证插件如X509证书、OAuth2、Webhook等方式保障API服务器的安全性,通过各种准入控制器如ResourceQuota、LimitRanger等确保资源的有效管理和安全性,以及如何通过对并发请求的数量、速率进行精确控制,提升系统的稳定性。API服务器的设计思路不仅考虑到了大规模部署的需求,同时也强调了安全性、可靠性和扩展性的平衡。文档同时提供了构建高可用多副本API服务器的具体指导,并探讨了与企业现有认证系统的集成方式。 适合人群:云计算领域的架构师、运维工程师、安全专家、开发人员和技术管理人员等。这些人需要深刻理解Kubernetes的工作机制,特别是在安全管理和性能优化方面的挑战。 使用场景及目标:①掌握API Server的各种访问控制手段(认证和鉴权);②学习如何使用API Server实现细粒度的资源访问控制;③熟悉不同限流策略,解决

    实验一 Simulink仿真环境及模型的建立.docx

    实验一 Simulink仿真环境及模型的建立.docx

    GLPI开源资产管理系统Docker部署方式,2025最新教程

    GLPI开源资产管理系统Docker部署方式,2025最新教程

    275.基于51单片机的电子钟【秒表,闹钟,倒计时,整点报时,数码管,矩阵键盘】(仿真).pdf

    275.基于51单片机的电子钟【秒表,闹钟,倒计时,整点报时,数码管,矩阵键盘】(仿真).pdf

    基于多车协同感知与烟花算法的局部路径规划技术及其在自动驾驶中的应用(可复现,有问题请联系博主)

    内容概要:该论文提出了一种基于多车协同感知与烟花算法的局部路径规划方法。首先通过烟花算法实现基于网格地图相似性的多车地图合并,然后结合人工势场方法进行实时路径规划,解决了复杂环境中自动驾驶汽车存在的视野盲区和路径规划不畅的问题。文中详细介绍了网格地图合并的原理和技术步骤、动态排斥势场的构建以及路径规划的实验验证。通过对不同算法进行比较实验,并验证了该方法在安全性和效率上的优越性,尤其是在处理动态障碍物时的优势。最终通过仿真分析展示了基于多车协同感知的地图融合方法能够显著提升路径规划的安全性和舒适性,为未来的自动化驾驶提供了坚实的技术支持。 适用人群:研究自动驾驶技术和智能交通系统的学术研究人员,及相关领域的工程师和技术人员。 使用场景及目标:该研究的应用场景主要是复杂的城市道路环境和其他具有多变环境的场景,旨在解决车辆因遮挡而形成的感知盲区,提供更加精确的局部路径规划方法。通过多车协同和动态排斥势场的方法,提升自动驾驶系统的实时性能、避障能力和安全性。 其他说明:尽管研究集中在城市结构化道路环境中验证,但是提出了未来工作的几个方向,包括如何扩展至更复杂的农村公路或环形交叉路口。同时也探

    <数据集>轨道异物识别数据集<目标检测>

    YOLO与VOC格式的轨道异物识别数据集,适用于YOLO系列、Faster Rcnn、SSD等模型训练,类别:person、obsticle_oc、Animal、vehicle、motor_bicycle、Train,图片数量1659。文件中包含图片、txt标签、指定类别信息的yaml文件、xml标签,已将图片和txt标签划分为训练集、验证集和测试集,可直接用于YOLOv5、YOLOv6、YOLOv7、YOLOv8、YOLOv9、YOLOv10、YOLOv11、YOLOv12等YOLO系列算法的训练。

    基于java的ssm课程在线教育资源管理系统(含LW+PPT+源码+系统演示视频+安装说明).7z

    在线教育资源管理系统,主要的模块包括管理员;个人中心、学生管理、教师管理、公告信息管理、课程信息管理、试题管理、留言板管理、管理员管理、试卷管理、系统管理、考试管理,学生;个人中心、留言板管理、考试管理,教师;个人中心、课程信息管理、试题管理、留言板管理、试卷管理、考试管理,前台首页;首页、公告信息、课程信息、试卷列表、留言反馈、我的、跳转到后台等功能。系统中管理员主要是为了安全有效地存储和管理各类信息,还可以对系统进行管理与更新维护等操作,并且对后台有相应的操作权限。 要想实现在线教育资源管理系统的各项功能,需要后台数据库的大力支持。管理员验证注册信息,收集的用户信息,并由此分析得出的关联信息等大量的数据都由数据库管理。本文中数据库服务器端采用了Mysql作为后台数据库,使Web与数据库紧密联系起来。在设计过程中,充分保证了系统代码的良好可读性、实用性、易扩展性、通用性、便于后期维护、操作方便以及页面简洁等特点。 本系统的开发使获取在线教育资源管理系统信息能够更加方便快捷,同时也使在线教育资源管理系统信息变的更加系统化、有序化。系统界面较友好,易于操作。 关键词:在线教育资源管理系统 ;jsp技术 ;Mysql数据库

    基于Python卷积神经网络进行猫狗识别源码+报告(99分大作业)

    基于Python卷积神经网络进行猫狗识别源码+报告(99分大作业),个人大三期末大作业项目、经导师指导并认可通过的高分设计项目,评审分99分,代码完整确保可以运行,小白也可以亲自搞定,主要针对计算机相关专业的正在做大作业的学生和需要项目实战练习的学习者,可作为课程设计、期末大作业。 基于Python卷积神经网络进行猫狗识别源码+报告(99分大作业)基于Python卷积神经网络进行猫狗识别源码+报告(99分大作业)基于Python卷积神经网络进行猫狗识别源码+报告(99分大作业)基于Python卷积神经网络进行猫狗识别源码+报告(99分大作业)基于Python卷积神经网络进行猫狗识别源码+报告(99分大作业)基于Python卷积神经网络进行猫狗识别源码+报告(99分大作业)基于Python卷积神经网络进行猫狗识别源码+报告(99分大作业)基于Python卷积神经网络进行猫狗识别源码+报告(99分大作业)基于Python卷积神经网络进行猫狗识别源码+报告(99分大作业)基于Python卷积神经网络进行猫狗识别源码+报告(99分大作业)基于Python卷积神经网络进行猫狗识别源码+报告(9

    cef109.1.11+chromium 109.0.5414.120

    最后一个win7稳定运行版本,支持视频和pdf查看,因为之前下载的别人打包好的文件,可以播放视频,但是打开pdf会闪退,所以自己编译了一个,有需要的可以试试

    前端分析-2023071100789s3

    前端分析-2023071100789s3

    基于 OpenCV 和 Flask 的 Python 3 人脸检测

    人脸检测演示 (Flask 应用程序) 这是一个非常简单的 Flask 应用程序,它允许用户上传图像并检测图片中有多少张(如果有)面孔

    山东大学软件学院2022级操作系统实验

    山东大学软件学院2022级操作系统实验

    20.基于51单片机的简易电梯系统的设计(实物).pdf

    20.基于51单片机的简易电梯系统的设计(实物)

    山东大学软件学院2022级四史专题

    山东大学软件学院2022级四史专题

    基于tensorflow开发的YOLO和YOLO9000提供训练以及测试功能以VOC2012数据集作为训练测试集500.zip

    yolo

    openjdk-8-jre-alpine.tar

    openjdk 8-jre-alpine f7a292bbb70c 5 years ago 84.9MB docker openjdk 离线镜像包 使用方法:docker load -i openjdk-8-jre-alpine.tar

    43.基于51单片机的红外遥控器设计(实物).pdf

    43.基于51单片机的红外遥控器设计(实物).pdf

Global site tag (gtag.js) - Google Analytics