make zero appear last in a list of ascending numbers

后端 未结 5 1246
悲哀的现实
悲哀的现实 2020-12-19 22:06

I\'m using SQLite in an Android application.
In all of my tables I have a default row with an index of 0 that holds default values for that table.
In most situations

相关标签:
5条回答
  • 2020-12-19 22:34

    Can you add an extra column to act as a filter, then sort by both columns?

    select (...)epoch_date.epoch, case epoch_date.epoch when 0 then 1 else 0 as default
    (...)
    order by default,epoch_date.epoch
    
    0 讨论(0)
  • 2020-12-19 22:38

    Equivalent to the CASE statement suggestion, but shorter:

     ORDER BY epoch_date.epoch == 0, epoch_date.epoch
    
    0 讨论(0)
  • 2020-12-19 22:40

    I'm not completely sure if SQLite allows case statements in "order by", but try something along these lines:

    order by case epoch_date.epoch when 0 then 999999 else epoch_date.epoch end
    
    0 讨论(0)
  • 2020-12-19 22:43

    Try this:

    ORDER BY
        CASE epoch_date.epoch WHEN 0 THEN 1 ELSE 0 END,
        epoch_date.epoch
    

    I haven't tested in SQLite but works in many other databases so hopefully it should work in SQLite too.

    0 讨论(0)
  • 2020-12-19 22:44

    order by decode(epoch_date.epoch,0,null,epoch_date.epoch)

    you can use something like this..since nulls are kept in the end in case of ascending order. provided you have decode function available in sqlite..or else you can just use a case statement instead.

    Ravi Kumar

    0 讨论(0)
提交回复
热议问题