-----------------------------------------------------------------------------模拟数据---------------------------------------------------------------------------
--创建测试数据及表结构

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 去重的更多相关文章

  1. oracle去重

    oracle去重 create table tmp_table3 as (SELECT seqno FROM (SELECT t.seqno,ROWID, ROW_NUMBER() OVER(PART ...

  2. Oracle 去重查询

      Oracle 去重查询 CreateTime--2018年2月28日15:38:45 Author:Marydon (一)使用distinct --查询指定区间内表停诊字段的值 SELECT DI ...

  3. oracle去重等基础问题

    --去重查询方法一:根据id select * from sxe where id in(select min(id) from sxe group by username) order by id ...

  4. 转转转--oracle 去重并按时间排序取第一条

    select t.* from (select a.*, row_number() over(partition by 需要分组的字段 order by 更新时间 desc) rw from 表 a) ...

  5. Oracle 去重后排序

    因项目需求,需要将查询结果,去重后,在按照主键(自增列)排序,百度一番,记录下来 DEMO SELECT * FROM (SELECT ROW_NUMBER() OVER(PARTITION BY S ...

  6. Oracle 去重并显示所有列数据

    一.原始数据(默认会生成一个 rownum 的序列,如下图的第一列) select t.* from ZD_DIC t where t.zdlx = '人员类型' 二.先分组,再给组内的内容进行排序 ...

  7. oracle去重试验

    http://blog.csdn.net/lunajiao/article/details/76014488

  8. oracle 多字段去重查询

      oracle 多字段去重查询 CreationTime--2018年6月29日15点11分 Author:Marydon 1.情景展示 需要对表BASE_MRI_DEVICE的COMPNAME.F ...

  9. 【Oracle&SQLServer】并集、交际、补集

    1.并集(UNION/UNION ALL) Oracle&SQLServer中用法一致 UNION 去重 UNION ALL 不去重 -- 去重 select * from tablea un ...

  10. oracle 根据字段查询重复数据

      1.情景展示 由上图可知,APPUSERID字段和VIRTUAL_CARDID字段存在一对多的关系,如何将重复的APPUSERID字段的数据查询出来呢? 2.原因分析 先查出重复的APPUSERI ...

随机推荐

  1. 关于pytorch一些基础知识的备份

    压缩conda环境用于备份目的实际上没有意义,因为还有其他方法可以做到这一点,这可能更合适,并使用专为此而设计的内置功能. 您可以创建一个环境.txt版本conda,详细说明其中的每个模块和版本,然后 ...

  2. 7款WordPress图片分离对象存储插件 含国内主流云服务存储商

    如果我们有用WordPress搭建网站的朋友应该会发现网站文件和数据库其实没有多大,在网站运营几年后数据最大的就是图片.而且图片占用服务器的空间会搬家比较麻烦,而且占用服务求的带宽.我们看到很多的Wo ...

  3. python flatten()函数的作用和用法

    flatten()函数可以执行展平操作,返回一个一维数组. 函数的作用对象是数组array.矩阵mat,不能直接用于列表list. x.flatten()是把numpy对象x降低到一维,默认是按照 行 ...

  4. 1.Easy Touch 3.1

    Easy Touch 3.1 Hedgehog Team(导入 Easy Touch 插件时自动在菜单栏) Extensions: 拓展 Adding a new joytick: 虚拟摇杆 Addi ...

  5. P7074 [CSP-J2020] 方格取数

    目录 题目 思路 code 题目 题目链接https://www.luogu.com.cn/problem/P7074 思路 用dp[i][j][0]表示在(i,j)从左边来的最大值 用dp[i][j ...

  6. 函数调用_通过apply和call方法调用

    不同类型函数调用之间的主要区别在于:最终作为函数上下文(可以通过this参数隐式引用到)传递给执行函数对象不同.对于方法而言,即为所在的对象:对于函数而言是window或是undefined(取决于是 ...

  7. I2C总线简介-转载

    I2C总线简介 - 立创社区 (szlcsc.com) 简介 NXP半导体(原Philips半导体)于20多年前发明了一种简单的双向二线制串行通信总线,这个总线这个总线被称为IIC.Inter-IC或 ...

  8. go: go.mod file not found in current directory or any parent directory; see 'go help mod 解决

    go: go.mod file not found in current directory or any parent directory; see 'go help mod go:在当前目录或任何 ...

  9. 060_关于Component Event的介绍 (本文为转载)

    转载自:https://www.cnblogs.com/zero-zyq/p/8977093.html lightning component基于事件驱动模型来处理用户界面的交互.这种事件驱动模型和j ...

  10. window 安装淘宝镜像

    打开cmd, 输入如下命令 npm config set registry https://registry.npm.taobao.org 判断是否配置淘宝镜像成功 npm config get re ...