如何取表中某个字段的最大值的记录,当然有很多办法,可是一直想一个简单sql搞定,今年看着例子想了好久才蹩过这个弯儿来,趁没忘记录一下——其实是例子没看仔细,这个悄悄说,不要让别人听到 :oops:
取某人在去年的最高 月均资产(avg_asset) 记录
取某人在去年的最高 月均资产(avg_asset) 记录
select
account_id,
month_id,
end_balance_rmb+end_balance_usd+end_balance_hk,
end_stockvalue_rmb+end_balance_hk+end_balance_usd,
end_asset,
end_balance_rmb+end_balance_usd+end_balance_hk+end_gj_stockvalue,
(end_asset+end_unbond_balance)-(begin_asset+begin_unbond_balance)-(total_in_deposit+total_out_deposit+total_in_stockvalue+total_out_stockvalue),
(total_out_deposit+total_in_deposit),
total_in_stockvalue+total_out_stockvalue,
total_commis,
case when gj_match_balance=0 then 0 else
gj_commis/gj_match_balance end,
total_interest,
total_commis+total_interest,
total_match_balance,
case when total_match_balance=0 then 0 else
nolocal_match_balance / total_match_balance end,
case when total_match_balance=0 then 0 else
web_match_balance / total_match_balance end,
case when avg_asset = 0 then 0 else
total_match_balance / avg_asset end,
end_of_stockvalue
from
(
select max(t.AVG_ASSET) over (partition by t.account_id) asset, t.*
from olap.vw_fact_account_by_month t
where t.account_id=i_account_id and t.month_id >= 200801 and t.month_id <= 200812
) e
where e.asset=e.AVG_ASSET;