我有以下格式的数据
userid amount term APR
1 10 5 1
1 10 4 2
2 20 6 1
2 20 4 3
2 20 3 1
我想按金额、期限、APR 排序,所以在输出中我想要最大金额,它是相应的期限,APR。如果金额相同,请选择具有最大期限的一项,如果期限也相同,则情况相同。但是这三者的结合总是独一无二的。
输出列:
userid, amount, term, apr, numberOfRowsForEachUser
1 10 5 1 2
2 20 6 1 3
问题:我能够获得前四列,但不确定如何获得“报价总数”或“每位用户的总行数”。
我的查询看起来像这样。
select
userid,amount,term,apr
from
( select userid, amount, term, apr
RANK() OVER (PARTITION BY userid ORDER BY amount,term,apr) amount_rank,
from
tableXYZ
)
where amount_rank = 1
最佳答案
只需添加一个 COUNT(*)
解析函数
select
userid,amount,term,apr, cnt
from
( select userid, amount, term, apr
RANK() OVER (PARTITION BY userid ORDER BY amount,term,apr) amount_rank,
COUNT(*) OVER (PARTITION BY userid) cnt
from
tableXYZ
)
where amount_rank = 1
关于sql - Oracle SQL 查询 : how to use count,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11802384/