3

I am looking for a special kind of requirement from my query on MySQL database where I want to apply order by in a different manner. E.g. In a DB with the fields user_id and user_rating and user_department with following data.

+------------------------------------------------------+
|      user_id    |   user_rating  |  user_department  |
+------------------------------------------------------+
|         1       |        102     |         A         |
|         2       |        33      |         B         |
|         3       |        43      |         C         |
|         4       |        54      |         A         |
|         5       |        63      |         A         |
|         6       |        214     |         B         |
|         7       |        82      |         A         |
|         8       |        87      |         C         |
|         9       |        43      |         A         |
|         10      |        98      |         A         |
|         11      |        73      |         C         |
|         12      |        31      |         A         |
+------------------------------------------------------+

Given above structure I want to sort results in the order of their user_rating for each_department and I need at max 5 records from each user_department among which intial 3 records should be in order of their rating but rest 2 should be random.

So In above case Output will be something similar to:

+------------------------------------------------------+
|      user_id    |   user_rating  |  user_department  |
+------------------------------------------------------+
|         1       |       102      |         A         |
|         10      |        98      |         A         |
|         7       |        82      |         A         |
|         12      |        31      |         A         |
|         5       |        63      |         A         |
|         6       |       214      |         B         |
|         2       |        33      |         B         |
|         8       |        87      |         C         |
|         11      |        73      |         C         |
|         3       |        43      |         C         |
+------------------------------------------------------+

I tried options available over web for custom order by, like using FIELD function But couldn't find that much useful here. Also tried to solve it using subquery but that option also does Not look feasible as MySql does Not allow me to use IN and LIMIT keywords together in my query.

Is there any better/simpler way to solve this.

2
  • In your context, is it feasible to run a series of SQL. E.g. to create a temp table that has the top three for each department, and then to run another that has 2 random from each department, not already existing in the temp table?
    – Darius X.
    Jul 15, 2013 at 18:07
  • Yeah that can be done and for second query random records should be chose among remaining records left after first query.
    – lalit
    Jul 15, 2013 at 18:11

1 Answer 1

1

This isn't exactly what you are looking for but it offers one approach.

The idea is to use group_concat() to put the top 5 values for each department in a single column. This column has the form:

user_id:rating

repeated up to five times, separated by a comma. As in:

1:182,10:98,7:82,12:31,5:63

The query that does this is:

select user_department,
       substring_index(group_concat(concat(user_id, ':', user_rating)
                                    order by user_rating desc
                                   ), ',', 5)
from t
group by user_department;

This does not handle the randomization of the last two values. And it puts everything in one row. But, I thought it might help.

My next attempt technically does what you ask for, but it runs the risk that the last two "random" users could be the same.

It uses the same group_concat() trick above. However, it goes beyond that by choosing different values from the list, using substring_index():

select u.user_id, user_id.user_rating, u.user_department
from (select (case when n.n in (1, 2, 3) or ud.numusers <= 5
                   then cast(substring_index(substring_index(users, ',', n.n), ',', -1) as unsigned)
                   else CAST(substring_index(substring_index(users, ',', 4 + rand()*(num_users - 3)), ',', -1) as unsigned)
              end) as user_id
      from (select user_department,
                   group_concat(user_id order by user_rating desc) as users,
                   count(*) as numusers
            from t
            group by user_department
           ) ud join
           (select 1 as n union all select 2 union all select 3 union all select 4 union all select 5
           ) n
           on n.n <= ud.numusers
    ) u join
    t
    on u.user_id = t.user_id
order by user_department, user_rating desc
1
  • Thanks for your reply. Yeah it looks useful. For me the main problem is to merge second requirement into first query, As it should be applied to the remaining records.
    – lalit
    Jul 15, 2013 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.