I have data in following format
userid amount term APR
1 10 5 1
1 10 4 2
2 20 6 1
2 20 4 3
2 20 3 1
I want to do order by on amount, term, APR so in output I want maximum amount and it's corresponding term, APR. In case amount is same, choose one with max term and same happens if term is also same. but combination of these three is always unique.
output columns:
userid, amount, term, apr, numberOfRowsForEachUser
1 10 5 1 2
2 20 6 1 3
Question: I am able to get first four columns, but not sure how to get 'total no of offers' or 'total number of rows per user'.
My query looks like this.
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