Gereralise the query for all user_id I have a mysql query

select sum(duration),app_name,app_id,user_id 
from timesheet 
where user_id=164 
group by app_id 
order by sum(duration) desc 
limit 5;


The result is 

sum(duration), app_name,       app_id,  user_id
'626919371',   'Idle.exe',     '0',     '164'
'38220511',    'LockApp.exe',  '2204',  '164'
'36675000',    '',             '1',     '164'
'27713000',    'LockApp.exe',  '8148',  '164'
'16698661',    'chrome.exe',   '8548',  '164'


However I want the top 5 app for every user_id instead of just 164. Can you pelase guide me as to how this can be achieved in a single query. Or do I need to fire it for every individual user
Test this:

select sum_duration, 
       app_name, 
       app_id, 
       user_id
from ( select get_data.*,
              if( @user_id = get_data.user_id, 
                  @rownum := @rownum + 1, 
                  @rownum := 1 + least(0, @user_id := get_data.user_id)) row_number
       from ( select sum(duration) sum_duration, 
                     app_name, 
                     app_id, 
                     user_id 
              from timesheet 
              group by app_id ) get_data, 
            ( select @rownum := 1, @user_id := 0 ) init_vars
       order by user_id, sum_duration DESC
     ) subquery
where row_number &lt;= 5