加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Teradata与Oracle rank区别

(2012-11-06 14:21:40)
标签:

it

分类: IT

Teradata:

Teradata创建表 test_index_val_rank

 create   MULTISET TABLE dwmart_bip.test_index_val_rank ,NO FALLBACK ,

     NO BEFORE JOURNAL,

     NO AFTER JOURNAL,

     CHECKSUM = DEFAULT,

     DEFAULT MERGEBLOCKRATIO

 (

   val DECIMAL(18,2)

 )

 插入值:

 insert  dwmart_bip.test_index_val_rank  values (100.00)

查询列表:共七条

100.03

100.04

100.02

100.01

100.00

100.01

100.01

 

查询前三名:

1)第一种方法

sel val ,rank(val) val

 from

 dwmart_bip.test_index_val_rank   

 order by val desc

 QUALIFY rank(val) <= 3;

 2)第二种方法

  sel top 3 val

 from

 dwmart_bip.test_index_val_rank   

  order by val desc

排名rank, row_number 使用方法。

 sel rank(val) ,val from  dwmart_bip.test_index_val_rank ;  --默认降序

查询结果:

1     100.04

2     100.03

3     100.02

4     100.01

4     100.01

4     100.01

7     100.00

sel  rank()  over (order by val desc),val from  dwmart_bip.test_index_val_rank;-- 同上是一样的

1    100.04

2    100.03

3    100.02

4    100.01

4    100.01

4    100.01

7    100.00

 sel  row_number()  over (order by val desc),val from  dwmart_bip.test_index_val_rank ;

1     100.04

2     100.03

3     100.02

4     100.01

5     100.01

6     100.01

7     100.00

总结:rankrow_number的不同可以看结果。Rank可以有并列排名,row_numbern+1排列。

 Teradata创建表  create   MULTISET TABLE dwmart_bip.test_index_val_rank1 ,NO FALLBACK ,

     NO BEFORE JOURNAL,

     NO AFTER JOURNAL,

     CHECKSUM = DEFAULT,

     DEFAULT MERGEBLOCKRATIO

 (

   Stat_Dt DATE FORMAT 'YYYYMMDD' TITLE '统计日期',

   val DECIMAL(18,2)  TITLE '指标'

 )

分组排名查询: 

sel row_number() over (partition by Stat_Dt   order by val ),val ,Stat_Dt from  dwmart_bip.test_index_val_rank1  ;

1    100.02     2012-11-05

2    100.03     2012-11-05

3    100.04     2012-11-05

1    100.02     2012-11-06

1    100.01     2012-11-07

1    100.00     2012-11-08

序列以n开始,依次往上累计:

sel csum(1,val),val ,Stat_Dt  from    dwmart_bip.test_index_val_rank1 group by Stat_Dt;   

1     100.02      2012-11-05

2     100.03      2012-11-05

3     100.04      2012-11-05

1     100.02      2012-11-06

1     100.01      2012-11-07

1     100.00      2012-11-08

分组排名查询:

sel rank() over ( partition by Stat_Dt order by val),val ,Stat_Dt from dwmart_bip.test_index_val_rank1;

1     100.02      2012-11-05

2     100.03      2012-11-05

3     100.04      2012-11-05

1     100.02      2012-11-06

1     100.01      2012-11-07

1     100.00      2012-11-08

Oralce

Oralce OlAP 分析函数 的区别:

首先介绍一下有哪些排名函数。

ROW_NUMBER

Row_number函数返回一个唯一的值,当碰到相同数据时,排名按照记录集中记录的顺序依次递增。 

DENSE_RANK(TD里没有)
Dense_rank函数返回一个唯一的值,除非当碰到相同数据时,此时所有相同数据的排名都是一样的。 

RANK
Rank函数返回一个唯一的值,除非遇到相同的数据时,此时所有相同数据的排名是一样的,同时会在最后一条相同记录和下一条不同记录的排名之间空出排名。

建表 CREATE  TABLE test_index_val_rank1 

 (

   Stat_Dt DATE ,

   val number(18,2)    

 )

 comment on table test_index_val_rank1

  is '测试表';

-- Add comments to the columns

comment on column test_index_val_rank1.Stat_Dt

  is '日期';

comment on column test_index_val_rank1.val

  is '指标';

插入数据:

INSERT INTO test_index_val_rank1   VALUES(TO_DATE('2012-11-08','YYYY-MM-DD'),'100.00')

INSERT INTO test_index_val_rank1   VALUES(TO_DATE('2012-11-05','YYYY-MM-DD'),'100.04')

INSERT INTO test_index_val_rank1   VALUES(TO_DATE('2012-11-07','YYYY-MM-DD'),'100.01')

INSERT INTO test_index_val_rank1   VALUES(TO_DATE('2012-11-06','YYYY-MM-DD'),'100.02')

INSERT INTO test_index_val_rank1   VALUES(TO_DATE('2012-11-05','YYYY-MM-DD'),'100.03')

INSERT INTO test_index_val_rank1   VALUES(TO_DATE('2012-11-05','YYYY-MM-DD'),'100.02')

这里oracle多了一个Dense_rank,TD里是没有的。

     

查询前三名:

使用oracle特有的rownum进行排序。

select rownum, t.*
      
from (select * 
            
from 
dwmart_bip.test_index_val_rank1
            
order by val desc  

where rownum <= 3

   order by val desc;

     1     2012-11-05  100.04

     2     2012-11-05  100.03

     3     2012-11-06  100.02

 

 

select     val,

           rank() over(order by val desc) rank,

            dense_rank() over(order by val desc) dense_rank,

           row_number() over(order by val desc) row_number

       from test_index_val_rank1

               rank dense_rank row_number

     100.04      1      1       1

     100.03      2      2       2

     100.02      3      3       3

     100.02      3      3       4

     100.01      5      4       5

     100.00      6      5       6

 

分组和TD语法是一样的。

总结:

DENSE_RANK Teradata里没有。其它语法一样。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有