Page 1 of 1

Conditional Random Order

Posted: Wed Jul 18, 2007 5:30 pm
by Shendemiar
Can i order the query so that i would get all of certain value at first, and the all the rest in random order?

Example
AAA, BBB, CCC (Column names and the below are the rows)
2,1,1
2,2,2
2,3,3
1,1,1
1,2,2
1,3,3
3,1,1
3,2,2
3,3,3

And the query would give
1,1,1
1,2,2
1,3,3
And then the rest in random order

Posted: Wed Jul 18, 2007 8:32 pm
by feyd
I would probably use a UNION to purposely select the leading ones then randomly select amongst the remaining.

Posted: Wed Jul 18, 2007 10:25 pm
by volka
ORDER BY AAA=1, Rand()

But that doesn't necessarily return
1,1,1
1,2,2
1,3,3
it can also return
1,3,3
1,1,1
1,2,2
It only assures that all records having AAA=1 come before other records.