on 04-20-2017 1:16 PM
SQL Anywhere allows expressions in the ORDER BY,
and the CASE expression can be used here.
CREATE TABLE t (
FIRST_NAME VARCHAR ( 10 ),
POSITION VARCHAR ( 20 ),
LISTORDER INTEGER );
INSERT t VALUES ( 'Dejah', 'CEO', 1 );
INSERT t VALUES ( 'John', 'DEVELOPER', 2 );
INSERT t VALUES ( 'Tosho', 'SALES ASSOCIATE', 3 );
INSERT t VALUES ( 'Mike', 'ACCOUNT MANAGER', 4 );
INSERT t VALUES ( 'Cacko', 'MARKETING', 5 );
INSERT t VALUES ( 'Bapco', 'IT', 6 );
INSERT t VALUES ( 'Blazo', 'SALES', 7 );
INSERT t VALUES ( 'Risto', 'ACCOUNTANT', 8 );
COMMIT;
SELECT * FROM t
ORDER BY CASE LISTORDER
WHEN 1 THEN 4
WHEN 2 THEN 5
WHEN 3 THEN 2
WHEN 4 THEN 6
WHEN 5 THEN 1
WHEN 6 THEN 8
WHEN 7 THEN 3
WHEN 8 THEN 9
ELSE 99999
END CASE;
FIRST_NAME POSITION LISTORDER
---------- -------------------- -----------
Cacko MARKETING 5
Tosho SALES ASSOCIATE 3
Blazo SALES 7
Dejah CEO 1
John DEVELOPER 2
Mike ACCOUNT MANAGER 4
Bapco IT 6
Risto ACCOUNTANT 8
You could also make it more powerful/flexible/complicated by
creating a separate table that defines [ LISTORDER, order-by-order ]
pairs and use that in a join.
You probably don't want to actually ALTER the base table definition.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
82 | |
10 | |
10 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.