Tintin Tintin - 6 months ago 16
SQL Question

SQLITE - Get Top 5 paid customers from Bill Table

I have Bill Table with rowid, customer mobile and amount field. Bill table can have any number of entries for particular mobile number. I want to extract top 5 customers order by amount paid.


Table Bill
-----------------------------------
id mobile amount
-----------------------------------
1 8800 100
2 2244 200
3 8800 50
4 2134 100
5 2244 500
6 6789 100
7 2134 50
8 2244 100
9 8800 200


Note - First I want to make sum of all amount paid by particular customer, then order them in descending order then get top 5 out of that data.

Answer

try with:

select mobile, sum(amount) as total_amount 
from Bill 
group by mobile 
order by total_amount 
desc limit 5
Comments