I need Urgent help please

  • Thread starter Thread starter Dimitris
  • Start date Start date
D

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
 
Dimitris said:
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
 
Baz said:
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.
 
Keith said:
Hi Baz,

Could you explain how that works for the benefit of my small brain?

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.
 
Baz said:
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.
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.
 
Back
Top