Label data according to percentile I have a table name temp with column customer_id , sum and I need to add extra column customer_label (may be on view) which is 1 if customer lies in top 10% according to value of sum, 2 if lies within 10%-20%, 3 if lies within 20%-60%, 4 if lies within 60-100%. How can I do this?

Some sample data is included at sqlfiddle
select customer_id,sum,case 
when pct_row&lt;=0.10 then 1
when pct_row&gt;0.10 and pct_row&lt;=0.20 then 2
when pct_row&gt;0.20 and pct_row&lt;=0.60 then 3
when pct_row&gt;0.60 then 4
end as customer_label from (
select customer_id,sum,(@curRow := @curRow+1)/c as pct_row
from temp t 
jOIN (SELECT @curRow := 0) r
JOIN (SELECT @curRow2 := 0) r2 
join (select count(*) c from temp) s
order by sum desc) p;


sqlfiddle :- here

I don't know whether this is efficient method or not but work fine for small data set.