0

I want to order query results by name, then on a specific table alphanumerically.

This works for all results by alphanumeric:

`ORDER BY 'name'+0 ASC"`;

But I need only table 'week' sorted alphanumerically, all other results alpha. I have tried the following but not working:

ORDER BY name, week('name'+0) ASC";

Table week results should be like:

Week 1
Week 2
Week 3
Week 4
...
Week10

Not:

Week 1
Week 10...

See db-fiddle

IlludiumPu36
  • 4,196
  • 10
  • 61
  • 100

1 Answers1

2

For order by clause, you can use custom field like below. For week name field use below in ORDER BY clause.

As per our conversation, added one column as seq in select clause and then based on data we did following ORDER BY clause

1. Added seq in select clause of all unions.

2. Order by changes:

ORDER BY seq ASC, 
    CASE seq
        WHEN 1 THEN 0 
        WHEN 2 THEN CAST(SUBSTRING(name, 6) AS SIGNED)
        WHEN 3 THEN CAST(SUBSTRING(name, -2) AS SIGNED)
        WHEN 4 THEN 0
        WHEN 5 THEN CAST(SUBSTRING(name, 6) AS SIGNED)
        WHEN 6 THEN 0
    END ASC,
    name  ASC
  • First, each union will contain their data as selected i.e. the First query in the union have results at the top and then second and so on.
  • In those union, in which name is a string, will be not ordered in second order by clause, only year, week and unit_name unions are sorted based on number
  • Sort all remaining unions by name.

See in Fiddle

Dark Knight
  • 6,116
  • 1
  • 15
  • 37