OraclevsPostgreSQLDevelop(14)-分析函数KEEPDENSE_RANK-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

OraclevsPostgreSQLDevelop(14)-分析函数KEEPDENSE_RANK

在Oracle中聚合函数KEEP DENSE_RANK用于获取在某个列分组的情况下按某个字段排序得到的聚合函数(如MAX/MIN等)值.

为香河等地区用户提供了全套网页设计制作服务,及香河网站建设行业解决方案。主营业务为成都网站设计、网站制作、香河网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

现有测试数据,先在account分组的情况下,每个分组按id正序排序(即最大id)的max(credit).

-- Oracle
drop table t_event;
create table t_event(id int,account int,type varchar2(30),credit number,delta_balance number);
truncate table t_event;
insert into t_event(id,account,type,credit,delta_balance) values(1,1,'created',0,0);
insert into t_event(id,account,type,credit,delta_balance) values(2,1,'deposited',null,100);
insert into t_event(id,account,type,credit,delta_balance) values(3,1,'withdraw',null,-50);
insert into t_event(id,account,type,credit,delta_balance) values(4,1,'credit_set',50,null);
insert into t_event(id,account,type,credit,delta_balance) values(5,1,'withdraw',-30,null);
insert into t_event(id,account,type,credit,delta_balance) values(6,1,'credit_set',100,null);
insert into t_event(id,account,type,credit,delta_balance) values(7,1,'withdraw',null,-100);
-- 
insert into t_event(id,account,type,credit,delta_balance) values(8,2,'credit_set',150,null);
insert into t_event(id,account,type,credit,delta_balance) values(9,2,'credit_set',110,null);
insert into t_event(id,account,type,credit,delta_balance) values(10,2,'credit_set',20,-100);
commit;
-- PG
drop table if exists t_event;
create table t_event(id int,account int,type varchar(30),credit int,delta_balance int);
truncate table t_event;
insert into t_event(id,account,type,credit,delta_balance) values(1,1,'created',0,0);
insert into t_event(id,account,type,credit,delta_balance) values(2,1,'deposited',null,100);
insert into t_event(id,account,type,credit,delta_balance) values(3,1,'withdraw',null,-50);
insert into t_event(id,account,type,credit,delta_balance) values(4,1,'credit_set',50,null);
insert into t_event(id,account,type,credit,delta_balance) values(5,1,'withdraw',-30,null);
insert into t_event(id,account,type,credit,delta_balance) values(6,1,'credit_set',100,null);
insert into t_event(id,account,type,credit,delta_balance) values(7,1,'withdraw',null,-100);
-- 
insert into t_event(id,account,type,credit,delta_balance) values(8,2,'credit_set',150,null);
insert into t_event(id,account,type,credit,delta_balance) values(9,2,'credit_set',110,null);
insert into t_event(id,account,type,credit,delta_balance) values(10,2,'credit_set',20,-100);
commit;

Oracle
Oracle可使用KEEP DENSE_RANK实现

TEST-orcl@DESKTOP-V430TU3>SELECT
  2      account,
  3      MAX(credit)
  4          KEEP (DENSE_RANK LAST ORDER BY id) AS credit
  5  FROM
  6      t_event
  7  WHERE type = 'credit_set'
  8  GROUP BY
  9      account;
   ACCOUNT     CREDIT
---------- ----------
         1        100
         2         20

PG
PG没有KEEP DENSE_RANK实现,但可通过数组的比较来实现.

[local]:5432 pg12@testdb=# SELECT
pg12@testdb-#     account,
pg12@testdb-#     (MAX(ARRAY[id, credit]) FILTER (WHERE type = 'credit_set'))[2] AS credit
pg12@testdb-# FROM
pg12@testdb-#     t_event
pg12@testdb-# GROUP BY
pg12@testdb-#     account
pg12@testdb-# ORDER BY account;
 account | credit 
---------+--------
       1 |    100
       2 |     20
(2 rows)
Time: 1.206 ms

注意(MAX(ARRAY[id, credit]) FILTER (WHERE type = ‘credit_set’))[2],把id和credit组成Element作为数组中的元素,由于id为第一个元素,因此在比较数组元素时,会首先比较id值得到最大id值的数组元素,然后取数组元素中的第2个成员的值([2]的含义).

参考资料
FIRST
MAX() KEEP (DENSE_RANK LAST ORDER BY ) OVER() PARTITION BY()
How to Get the First or Last Value in a Group Using Group By in SQL


标题名称:OraclevsPostgreSQLDevelop(14)-分析函数KEEPDENSE_RANK
文章链接:http://kswsj.cn/article/jocgpg.html

其他资讯