In a table I have a column with numbers from 1-1000. I need to sort them by
odds and evens. For example 1,3,5,7,9,11.....999 and after 999 the next
number should be 2,4,6,8,10....1000. Is it possible? and how?
Thanks
Dimitris
> In a table I have a column with numbers from 1-1000. I need to sort them by
> odds and evens. For example 1,3,5,7,9,11.....999 and after 999 the next
> number should be 2,4,6,8,10....1000. Is it possible? and how?
> Thanks
> Dimitris
SELECT * FROM some_table ORDER BY some_column Mod 2 DESC, some_column
Dimitris - 29 Sep 2005 09:28 GMT
Thanks Baz.
You saved my day!
>> In a table I have a column with numbers from 1-1000. I need to sort them
> by
[quoted text clipped - 4 lines]
>
> SELECT * FROM some_table ORDER BY some_column Mod 2 DESC, some_column
Keith - 29 Sep 2005 10:06 GMT
>> In a table I have a column with numbers from 1-1000. I need to sort them
> by
[quoted text clipped - 4 lines]
>
> SELECT * FROM some_table ORDER BY some_column Mod 2 DESC, some_column
Hi Baz,
Could you explain how that works for the benefit of my small brain?
Thanks.
Keith.
Baz - 29 Sep 2005 10:58 GMT
> >> In a table I have a column with numbers from 1-1000. I need to sort them
> > by
[quoted text clipped - 11 lines]
> Thanks.
> Keith.
The Mod (modulus) operator performs a division, but it returns the remainder
instead of the result. So:
1 Mod 2 = 1
2 Mod 2 = 0
3 Mod 2 = 1
4 Mod 2 = 0
and so on.
So, "some_column Mod 2" returns 1 when some_column is odd, and returns 0
when some_column is even.
Therefore, a descending sort by "some_column Mod 2" puts all the odd values
of some_column before the even values.
Finally, the ascending sort of some_column puts the values in ascending
order within odd and even.
Keith - 29 Sep 2005 11:36 GMT
> The Mod (modulus) operator performs a division, but it returns the
> remainder
[quoted text clipped - 16 lines]
> Finally, the ascending sort of some_column puts the values in ascending
> order within odd and even.
Thanks Baz, I was not familiar with the Mod operator and couldn't find
anything about it in the help. That's quite a neat solution.
Regards,
Keith.