0

My table consists of two columns:

id | my_group
-------------
1  | 3
2  | 2
3  | 1
4  | no_group
5  | 3
6  | 1
7  | 2
8  | 3
9  | no_group
10 | 1

Can you please help me to get the correct sorting order presented below, using using MySQL?

id | my_group
-------------
10 | 1
6  | 1
3  | 1
9  | no_group
8  | 3
5  | 3
1  | 3
7  | 2
2  | 2
4  | no_group

The my_group column is sorted descending but if my_group is not equal "no_group", my_group should group the results first.

6
  • 1
    What queries have you tried so far? Commented Aug 24, 2014 at 10:08
  • Select * from table order by my_group asc Commented Aug 24, 2014 at 10:17
  • @Undefined_variable - the query you wrote would return only 4 results. Commented Aug 24, 2014 at 10:32
  • @Strawberry - Let's imagine you have a database with transactions. Id is a transaction number. Some clients may purchase more than one item. In the case of these clients I need to group their purchases so that the transactions appear as presented in the second table. My_group means that a client bought more than 1 item. If some client bought only one item, the my_group field value is no_group. Commented Aug 24, 2014 at 10:41
  • Nope. Still don't get it. What determines th order of the results - 1 then no_group then 3 then 2 then no_group. Commented Aug 24, 2014 at 10:43

1 Answer 1

1

You could get this complicated order with following statement:

SELECT
    e.id,
    e.my_group
FROM
    example e
LEFT JOIN (
    SELECT 
        MAX(e1.id) maxid,
        e1.my_group
    FROM
        example e1
    WHERE
        e1.my_group <> 'no_group'
    GROUP BY e1.my_group
) t
ON
    e.my_group = t.my_group
ORDER BY COALESCE(t.maxid, id) DESC, id DESC;

Demo

Explanation:

We do a left join with the subselect that returns the maximum id per group, if there is a group.

The result of

SELECT
    e.id,
    e.my_group,
    t.maxid,
    COALESCE(t.maxid, e.id)
FROM
    example e
LEFT JOIN (
    SELECT 
        MAX(e1.id) maxid,
        e1.my_group
    FROM
        example e1
    WHERE
        e1.my_group <> 'no_group'
    GROUP BY e1.my_group
) t
ON
    e.my_group = t.my_group

is

id  my_group  maxid  COALESCE(t.maxid, e.id)
10  1         10     10
 6  1         10     10
 3  1         10     10
 9  no_group  NULL    9
 5  3         8       8
 1  3         8       8
 8  3         8       8
 2  2         7       7
 7  2         7       7
 4  no_group  NULL    4

So for the rows with 'no_group' we get NULL in our maxid column. For these rows we've got to take the id. COALESCE returns the first non-null value of it's arguments, so it returns the maximum id per group and the id value for the rows with 'no_group'.

Note:

If there would be NULL values instead of the string 'no_group' then you could simply omit the WHERE clause of the subselect. I would prefer this.

Sign up to request clarification or add additional context in comments.

3 Comments

You're a friggin' genius. How you got that from the information provided, I've no idea!
@Strawberry Thanks. First I didn't get it too. But there was that comment, that said, that every no_group was principally "a group with a single item". Then I saw the pattern, that the order was by the greatest id per group. Getting the greatest id for the no_group values was simpler than I thought.
@Strawberry I forgot to sort within the same group. Added , id DESC to ORDER BY clause.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.