ORACLE 分析函数实例[处理冗余数据]

[b]一.创建数据库表结构[/b]

create table test1(
id number primary key,
code1 VARCHAR2(20) not null,
code2 varchar2(20) not null,
code3 number
)

[b]二.创建测试数据[/b]

id code1 code2 code3
=======================================
1 1 1 a 30
2 2 1 b 40
3 3 1 c 50
4 4 2 a 26
5 5 2 e 11
6 6 2 f 33
7 7 3 g 25
8 8 3 t 25
9 9 1 a 21

[b]三.业务描述[/b]

[i]1.查询code1,code2 两个字段全部出现重复的记录信息[/i]

select t.* from (
select id,code1,code2,code3,count(*) over (partition by code1,code2) as 重复次数 from test1
) t where t.重复次数 > 1

运行结果:

id code1 code2 code3 重复次数
==================================================
1 1 1 a 30 2
2 9 1 a 21 2


[i]2.业务追加:在以上基础上追加条件,只显示code3值最小的记录[/i]

select t.* from (
select id,code1,code2,code3,count(*) over (partition by code1,code2) as 重复次数,min(code3) over(partition by code1,code2) as c3 from test1
) t where t.重复次数 > 1 and t.code3 = t.c3

执行结果:

id code1 code2 code3 重复次数
==================================================
1 9 1 a 21 2


[i]3.追加业务:在以上基础上继续追加条件,如果code3值最小的记录有多条,那么只显示其中rowid最小的那条记录[/i]
为了体现这个业务 特在库表中加入一条数据


id code1 code2 code3
=====================================================
10 1 a 21

select * from (
select t.*,min(rowid) over(partition by code1,code2,code3) as m_rwid from (
select id,code1,code2,code3,count(*) over (partition by code1,code2) as 重复次数,min(code3) over(partition by code1,code2) as c3,
rowid from test1
) t where t.重复次数 > 1 and t.code3 = t.c3
) where m_rwid = rowid
--三层的select 这sql写的比冗余数据都冗余 有好想法的请留言 - -

执行结果:

==================================================================================
1 9 1 a 21 3 21 AAARbLAABAAAV9RAAI AAARbLAABAAAV9RAAI



关于over函数,可以点击
[url]http://www.iteye.com/topic/575434[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值