Welcome to WebmasterWorld Guest from 54.146.201.80

Forum Moderators: open

Message Too Old, No Replies

column sorting, grouping, ordering and randomising all at once

Split a column into two groups then order and randomise them

     
10:04 pm on Jul 20, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 2, 2004
posts: 81
votes: 0


My SQL 4.1.25
I am totally stumped with this one.

I have a table in which one of the columns is an integer from 0~10.

I am trying to split that it into two groups. 1-5 and 0 + 6-10 and order them firstly group 0 + 6-10 then group 1-5 with each group randomised.
So basically I need to see something like this

10
7
8
0
7
6
9
6
0
10

2
4
5
2
5
3
1

Any ideas please
12:31 am on July 21, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 2, 2004
posts: 81
votes: 0


I came up with the following

ORDER BY (my_number = 0 OR my_number > 5) DESC, RAND(),my_number ASC, RAND()

Do you know a better/alternate way?
12:36 am on July 21, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


Doesn't that work? It looks like it should, and is exactly how I would do it . . .
12:58 am on July 21, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 2, 2004
posts: 81
votes: 0


Yes, in fact it works better than I thought it would.

I was just asking to find out if anyone would do it a different way. More than one way to skin a cat and all that.

I feel reasured that you said that's the way you would do it :)

Thanks