Schema
CREATE TABLE invoice ( Id INTEGER PRIMARY KEY, code TEXT, dropped INTEGER, due_date INTEGER, paid INTEGER, due_date2 TEXT );
Initial data
INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (1, 'IMSS', 0, 1407384129647, 0, '2014-08-07 04:02:09'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (2, 'Deep Discovery family', 0, 1407346684736, 0, '2014-08-06 17:38:04'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (3, 'TMMS', 1, 1407375568242, 0, '2014-08-07 01:39:28'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (4, 'TMS family', 0, 1407374437084, 1, '2014-08-07 01:20:37'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (5, 'SafeSync', 0, 1407358168347, 1, '2014-08-06 20:49:28'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (6, 'TMVMS', 0, 1407350693881, 0, '2014-08-06 18:44:53'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (7, 'TMS family', 0, 1407372590354, 0, '2014-08-07 00:49:50'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (8, 'IMSVA', 0, 1407344495046, 0, '2014-08-06 17:01:35'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (9, 'Other', 0, 1407351210774, 0, '2014-08-06 18:53:30'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (10, 'Deep Discovery family', 0, 1407372667906, 0, '2014-08-07 00:51:07'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (11, 'IMSVA', 1, 1407365934950, 1, '2014-08-06 22:58:54'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (12, 'PSP', 1, 1407382557767, 1, '2014-08-07 03:35:57'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (13, 'SecureCloud', 0, 1407371609679, 0, '2014-08-07 00:33:29'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (14, 'NVWE', 0, 1407383088966, 0, '2014-08-07 03:44:48'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (15, 'ESE', 1, 1407360299869, 1, '2014-08-06 21:24:59'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (16, 'IMSVA', 0, 1407377397832, 0, '2014-08-07 02:09:57'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (17, 'IWSVA', 0, 1407366067348, 1, '2014-08-06 23:01:07'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (18, 'TMS family', 1, 1407351067728, 0, '2014-08-06 18:51:07'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (19, 'OSCE', 0, 1407345046895, 0, '2014-08-06 17:10:46'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (20, 'TIM', 0, 1407363640638, 1, '2014-08-06 22:20:40'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (21, 'CSS', 1, 1407330238874, 0, '2014-08-06 13:03:58'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (22, 'TMPS', 1, 1407321708684, 1, '2014-08-06 10:41:48'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (23, 'TMS family', 1, 1407319881608, 0, '2014-08-06 10:11:21'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (24, 'SafeSync', 1, 1407306547713, 0, '2014-08-06 06:29:07'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (25, 'ESDP', 0, 1407298566126, 0, '2014-08-06 04:16:06'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (26, 'CSS', 0, 1407282477671, 0, '2014-08-05 23:47:57'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (27, 'TMMS', 1, 1407240200509, 1, '2014-08-05 12:03:20'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (28, 'Other', 0, 1407218499996, 0, '2014-08-05 06:01:39'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (29, 'ESE', 0, 1407210588362, 0, '2014-08-05 03:49:48'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (30, 'CSS', 0, 1407207304034, 0, '2014-08-05 02:55:04'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (31, 'TMVMS', 1, 1407188482248, 0, '2014-08-04 21:41:22'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (32, 'SecureCloud', 0, 1407155296508, 0, '2014-08-04 12:28:16'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (33, 'TMS family', 1, 1407136374493, 0, '2014-08-04 07:12:54'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (34, 'ESE', 1, 1407121543307, 1, '2014-08-04 03:05:43'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (35, 'ScanMail', 0, 1407118018107, 0, '2014-08-04 02:06:58'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (36, 'ScanMail', 0, 1407088856291, 0, '2014-08-03 18:00:56'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (37, 'IMSS', 0, 1407059314747, 1, '2014-08-03 09:48:34'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (38, 'OSCE', 1, 1407027453954, 0, '2014-08-03 00:57:33'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (39, 'Deep Discovery family', 0, 1406995304204, 0, '2014-08-02 16:01:44'); INSERT INTO invoice (Id, code, dropped, due_date, paid, due_date2) VALUES (40, 'Worry-Free', 0, 1406968142441, 0, '2014-08-02 08:29:02');
Group select
select Id, code, (dropped|paid) status, (due_date < 1407200000000) expired, due_date2 from invoice order by paid|dropped, (due_date > 1407200000000), due_date desc
result