溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Oracle vs PostgreSQL Develop(14) - 分析函數(shù)KEEP DENSE_RANK

發(fā)布時間:2020-08-12 00:19:18 來源:ITPUB博客 閱讀:217 作者:husthxd 欄目:關(guān)系型數(shù)據(jù)庫

在Oracle中聚合函數(shù)KEEP DENSE_RANK用于獲取在某個列分組的情況下按某個字段排序得到的聚合函數(shù)(如MAX/MIN等)值.

現(xiàn)有測試數(shù)據(jù),先在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實現(xiàn)

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實現(xiàn),但可通過數(shù)組的比較來實現(xiàn).

[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作為數(shù)組中的元素,由于id為第一個元素,因此在比較數(shù)組元素時,會首先比較id值得到最大id值的數(shù)組元素,然后取數(shù)組元素中的第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

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI