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 ...
随机推荐
- intellij idea 用 Gradle新建 spring boot
intellij idea用的是ideaIU-2017.1.4 .spring boot用的是2.10 出现错误 ERROR StatusLogger Log4j2 could not find a ...
- Java 03-基础 堆和栈(另,理解Java内存)
概述: 栈区(STACK)堆区(HEAP) 1.栈中主要存放一些基本类型的变量,且每一个基本数据类型有固定的大小(byte,short,int,long,float,double,boolean,ch ...
- ubuntu20.04系统openjdk11变更openjdk-8-jdk
ubuntu20.04系统openjdk11变更openjdk-8-jdk一.卸载openjdk11先检查是否安装,命令:dpkg --list | grep -i jdk移除openjdk包,命令: ...
- robots.txt详解[通俗易懂]
大家好,又见面了,我是你们的朋友全栈君. 怎样查看robots文件? 浏览器输入 主域名/robots.txt robots.txt的作用 robots.txt 文件规定了搜索引擎抓取工具可以访问网站 ...
- Counting Triangles
- 043_关于Salesforce集中权限的解释
1.创建Object的时候,一定要选中Deploy,避免在All Tabs 中找不到 2.在Profile里,选择 Standart tab Setting.Custom tab setting,有三 ...
- Cadvisor+prometheus+grafana
部署Cadvisor [root@localhost ~]# docker run -d \ --volume=/:/rootfs:ro \ --volume=/var/run:/var/run:ro ...
- How to Check and Repair EXT4 Filesystem in Linux
The fsck (stands for File System Consistency Check) is used to check and repair one or more Linux fi ...
- Codeforces Round #728 (Div. 2) C. Great Graphs
Great Graphs 题意 给你一个数组\(d\),\(d[i]\)表示从节点\(1\)到其他各个节点的最短路的长度,然后你可以对这个图进行加边(可以是负边),但不允许存在一个权值和为负数的回路. ...
- javascript向tabale中动态添加数据
<table width="600" border="1" cellspacing="0"> <thead> < ...