2

I have a simple MySQL db listing the total time spent each user has spent online in seconds (just a string) per day. I would like to select the top 10% of those users for a given day and return there usernames.

Is there a way to do this in MySQL in a single query..? I've seen examples where you select the top 10 users, but not based on a percentage of total users...

DavidJB
  • 2,170
  • 12
  • 28
  • 36

3 Answers3

0

try this

     SELECT 0.1 * count(username)  as percented_users FROM prices
     GROUP BY username
echo_Me
  • 36,552
  • 5
  • 55
  • 77
0

Hope this will help you

I used CURRENT_DATE to limit search.

SELECT user_id, date, total_time FROM
(
  SELECT user_id, @rownum:=@rownum+1 AS rownum 
  FROM TABLE_USER , (SELECT @rownum:=0) R
  WHERE date = CURRENT_DATE 
  ORDER by total_time desc
) temp 
where rownum < (select count(*) from TABLE_USER where date = CURRENT_DATE) / 10
agarici
  • 592
  • 1
  • 4
  • 9
  • this does not appear to work, I just get an _mysql_exceptions.OperationalError: (1054, "Unknown column 'user_id' in 'field list'") error, despite the column being in the db – DavidJB Jun 12 '13 at 10:54
0

You may try this way:

select * from table limit floor((select count(1) from table)*0.1)
tryingToLearn
  • 9,007
  • 10
  • 70
  • 99