sql - TSQL - How to return rows with a specific value last? -
my question reverse of question.
tsql - possible define sort order?
i want return records sql server database, sorted in ascending order based on column's value 2 particular records should on end.
the original sql (which needs changed) below:
select code_id categoryid, code_desc categorydescription codes code_id > '000001' , code_id < '000100' order categorydescription
the results returned
- 000003 ***first record
- 000002 ***another record
- 000004 apple
- 000016 books
- 000014 cables
i want below result (first 2 asteriks on last):
- 000004 apple
- 000016 books
- 000014 cables
- 000003 ***first record
- 000002 ***another record
i tried below union statement resultset automatically sorted in ascending order , 2 rows in beginning default.
select code_id categoryid, code_desc categorydescription codes code_id > '000003' , code_id < '000100' --order categorydescription union select code_id categoryid, code_desc categorydescription codes code_id > '000001' , code_id < '000004'
if wish maintain union this:
select * ( select 't' success, code_id categoryid, code_desc categorydescription, 1 order codes code_id > '000003' , code_id < '000100' union select 't' success, code_id categoryid, code_desc categorydescription, 2 order codes code_id > '000001' , code_id < '000004' ) x order x.order
Comments
Post a Comment