I have the following rows returned from my SQL query- can someone please help with a generic sorting code to sort the values in ascending order?
('High_Speed'),
('M1 Speed'),
('M13 Speed'),
('M14 Speed'),
('M2 Speed'),
('M3 Speed'),
('Medium_Speed'),
('Test1 zone1 High_Speed'),
('Test1 zone11 High_Speed'),
('Test1 zone2 High_Speed'),
('Test1 zone21 High_Speed'),
('Medium_Speed'),
('Zone206 Speed')
expected sorting-
('High_Speed'),
('M1 Speed'),
('M2 Speed'),
('M3 Speed'),
('M13 Speed'),
('M14 Speed'),
('Medium_Speed'),
('Test1 zone1 High_Speed'),
('Test1 zone2 High_Speed'),
('Test1 zone11 High_Speed'),
('Test1 zone21 High_Speed'),
('Medium_Speed'),
('Zone206 Speed')
The most generic method is to use a case
expression:
order by (case col
when 'High_Speed' then 1
when 'M1 Speed' then 2
when 'M2 Speed' then 3
when 'M3 Speed' then 4
when 'M13 Speed' then 5
when 'M14 Speed' then 6
when 'Medium_Speed' then 7
when 'Test1 zone1 High_Speed' then 8
when 'Test1 zone2 High_Speed' then 9
when 'Test1 zone11 High_Speed' then 10
when 'Test1 zone21 High_Speed' then 11
when 'Medium_Speed' then 12
when 'Zone206 Speed' then 13
else 999
end)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.