Reputation: 3342
I have a table and need to it sort on two columns, first one is a varchar column and sencond is a date column...
problem is... for the first column I dont want sort based on alphabetic order
table stracture is
id name status date
---------------------------------------
1 aaaa completed '2014-05-02'
2 aaaa Running '2015-05-03'
3 aaaa new '2015-05-03'
4 aaaa Pause '2015-05-04'
5 aaaa completed '2015-05-08'
I want status to be sorted like this
id name status date
---------------------------------------
1 aaaa Running '2014-05-03'
2 aaaa Pause '2014-05-04'
3 aaaa new '2014-05-03'
4 aaaa completed '2014-05-02'
5 aaaa completed '2014-05-08'
but is sorting like this
I want status to be sorted like this
id name status date
---------------------------------------
1 aaaa Running '2014-05-03'
2 aaaa Pause '2014-05-04'
3 aaaa new '2014-05-03'
4 aaaa completed '2014-05-02'
5 aaaa completed '2014-05-05'
any suggestion are appreciated...
Thank you
Upvotes: 1
Views: 118
Reputation: 3342
try this ..
In Android
String selectQuery = "SELECT * FROM "
+ TABLE_CONTACTS
+ " where isConvertToInvoice = 0 and loginUser_id ='"
+ logInID
+ "' ORDER BY CASE WHEN status = 'Running' THEN 1 WHEN status = 'Pause' THEN 2 WHEN status = 'new' THEN 3 WHEN status = 'Completed' THEN 4 END ";
In ios
NSString *query=[NSString stringWithFormat:@"SELECT * FROM TABLE_CONTACTS where isConvertToInvoice = 0 and User_id ='%@' ORDER BY CASE WHEN status = 'Running' THEN 1 WHEN status = 'Pause' THEN 2 WHEN status = 'new' THEN 3 WHEN status = 'Completed' THEN 4 END",logInID];
Upvotes: 2
Reputation: 324
try this....
SELECT * FROM table
ORDER BY
(CASE WHEN status = 'Running' THEN 1
WHEN status = 'Pause' THEN 2
WHEN status = 'New' THEN 3
WHEN status = 'Completed' THEN 4
WHEN status = 'Deleted' THEN 5 END), Date ASC
Upvotes: 2