ORACLE 去重
-----------------------------------------------------------------------------模拟数据---------------------------------------------------------------------------
--创建测试数据及表结构
create table tablename(N1 varchar2(10), N2 varchar2(10), N3 varchar2(10));
insert into tablename 1, 2, 3 from dual union allselect 1, 2, 3 from dual union allselect 5, 2, 3 from dual union allselect 10, 20, 30 from dual ;commit;select*from tablename;
--数据集(针对指定列,查出去重后的结果集)
--N1 N2 N3
--1 2 3
--1 2 3
--5 2 3
--10 20 30
-----------------------------------------------------------------------------distinct-----------------------------------------------------------------------------
select distinct t1.* from tablename t1;
N1 N2 N3
10 20 30
1 2 3
5 2 3
--方法局限性很大,因为它只能对全部查询的列做去重。如果我想对N2,N3去重,那我的结果集中就只能有N2,N3列,而不能有N1列。
select distinct t1.N2, N3 from tablename t1
N2 N3
2 3
20 30
--不过它也是最简单易懂的写法。
-----------------------------------------------------------------------------row_number-----------------------------------------------------------------------------
select * from (select t1.*,
row_number() over(partition by t1.N2, t1.N3 order by 1) rn
from tablename t1) t1 where t1.rn = 1;
N1 N2 N3 RN
1 2 3 1
10 20 30 1
--写法上要麻烦不少,但是有更大的灵活性。
--针对指定列,查出所有重复的行
-----------------------------------------------------------------------------count having-----------------------------------------------------------------------------
select * from tablename t
where (t.N2, t.N3) in (select t1.N2, t1.N3
from tablename t1
group by t1.N2, t1.N3
having count(1) > 1)
N1 N2 N3
1 2 3
1 2 3
5 2 3
--要查两次表,效率会比较低。不推荐。
------------------------------------------------------------------------------count over----------------------------------------------------------------------------
select * from (select t1.*,
count(1) over(partition by t1.N2, t1.N3) rn
from tablename t1) t1 where t1.rn > 1;
N1 N2 N3 RN
1 2 3 3
1 2 3 3
5 2 3 3
--只需要查一次表,推荐。
ORACLE 去重的更多相关文章
- oracle去重
oracle去重 create table tmp_table3 as (SELECT seqno FROM (SELECT t.seqno,ROWID, ROW_NUMBER() OVER(PART ...
- Oracle 去重查询
Oracle 去重查询 CreateTime--2018年2月28日15:38:45 Author:Marydon (一)使用distinct --查询指定区间内表停诊字段的值 SELECT DI ...
- oracle去重等基础问题
--去重查询方法一:根据id select * from sxe where id in(select min(id) from sxe group by username) order by id ...
- 转转转--oracle 去重并按时间排序取第一条
select t.* from (select a.*, row_number() over(partition by 需要分组的字段 order by 更新时间 desc) rw from 表 a) ...
- Oracle 去重后排序
因项目需求,需要将查询结果,去重后,在按照主键(自增列)排序,百度一番,记录下来 DEMO SELECT * FROM (SELECT ROW_NUMBER() OVER(PARTITION BY S ...
- Oracle 去重并显示所有列数据
一.原始数据(默认会生成一个 rownum 的序列,如下图的第一列) select t.* from ZD_DIC t where t.zdlx = '人员类型' 二.先分组,再给组内的内容进行排序 ...
- oracle去重试验
http://blog.csdn.net/lunajiao/article/details/76014488
- oracle 多字段去重查询
oracle 多字段去重查询 CreationTime--2018年6月29日15点11分 Author:Marydon 1.情景展示 需要对表BASE_MRI_DEVICE的COMPNAME.F ...
- 【Oracle&SQLServer】并集、交际、补集
1.并集(UNION/UNION ALL) Oracle&SQLServer中用法一致 UNION 去重 UNION ALL 不去重 -- 去重 select * from tablea un ...
- oracle 根据字段查询重复数据
1.情景展示 由上图可知,APPUSERID字段和VIRTUAL_CARDID字段存在一对多的关系,如何将重复的APPUSERID字段的数据查询出来呢? 2.原因分析 先查出重复的APPUSERI ...
随机推荐
- 单文件WSDL,非模块化
最近在使用CXF做WebService Sever端,接口与实现类不在一个包下. 实现类如下: 1 @WebService(serviceName = "Demo" 2 , tar ...
- aspose word导出表格
[HttpGet] [Route("GetPurchaseItemWord")] public IHttpActionResult Get_PurchaseItemWord(str ...
- git ssh 22 失效
Host github.com User 2070152270@qq.com Hostname ssh.github.com PreferredAuthentications publickey Id ...
- 什么叫IOCSABS呢
所谓的IOCSABS,就是全新的管理技术,是结合客户端管理与网络平台管理为一体的创新观念及技术, IOCSABS? (what is IOCSABS?) 英文的全称为Integrated Of ...
- ANSYS Electronics Suite 19.2下载地址及其安装教程
ANSYS Electronics Suite 19.2下载安装教程 1.下载地址https://getintopc.site/ansys-electronics-suite-19-2-free-do ...
- python 判断一个字符串中是否存在另一个字串中的元素
如 判断str是否包含str_list中的元素 str = "this is string example....wow!!!" str_list = ['aa','bb','st ...
- css 背景渐变
1.渐变从左到右 background: linear-gradient(to right,#000,#fff); 2.渐变从上到下 background: linear-gradient(tobot ...
- vue搭建项目iview+axios+less
项目地址:https://github.com/CinderellaStory/vue-iview-project vue搭建项目壳子已安装:iview.axios.less 已有界面:登录.左侧菜单 ...
- switch组件的使用
正常情况下,path和component是一一对应的关系 switch可以提高路由匹配效率(单一匹配)
- 美国:KDB 986446 D01已生效
1.美国FCC认证新要求 继2022年11月25日FCC发布了FCC 22-84法规禁止授权被认为对美国国家安全构成威胁的通信和视频监控设备后,2023年1月24日FCC又发布了KDB 986446 ...