我正在尝试获取给定帐户的最大余额
ACCT_BALANCE table:
ACCOUNT_JOIN,BALANCE_UPDATE_DATE,ACCT_VALUE
1 11:49 AM 3/21/2017 1000
1 10:49 AM 5/29/2017 2121
2 11:49 AM 3/29/2017 3332
2 13:21 AM 3/33/2017 110
3 11:50 AM 4/4/2017 888
4 10:49 AM 3/29/2017 66
4 11:50 AM 3/3/2017 57000
4 10:49 AM 2/29/2017 2265
4 13:21 AM 3/2/2017 555
4 11:50 AM 8/9/2017 555
例如,从 2017 年 5 月 29 日起,账户加入 1 的最大余额为 2121
性能方面什么会更好?使用 MAX 和 Inner join 或 rank() over?
CREATE OR REPLACE VIEW MAX_VAL_VIEW
AS
SELECT ACCT_VALUE,ACCOUNT_JOIN
FROM (
SELECT ACCT_VALUE,ACCOUNT_JOIN,
rank() over (partition by ACCOUNT_JOIN order by BALANCE_UPDATE_DATE desc) rnk
FROM ACCT_BALANCE
)
WHERE rnk = 1
/
CREATE OR REPLACE VIEW MAX_VAL_VIEW
AS
SELECT A.ACCT_VALUE ,A.ACCOUNT_JOIN
FROM ACCT_BALANCE A
WHERE A.BALANCE_UPDATE_DATE = (
SELECT max(B.BALANCE_UPDATE_DATE)
FROM ACCT_BALANCE B
WHERE B.ACCOUNT_JOIN = A.ACCOUNT_JOIN
)
/