Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ultradiv
Starting Member
41 Posts |
Posted - 2011-01-23 : 07:14:22
|
Hi folksAny way to randomise an ordered select list?CREATE TABLE #TESTSORTING (ID INT ,GroupedCol INT)INSERT INTO #TESTSORTINGSELECT 1,1 UNION ALLSELECT 2,2 UNION ALLSELECT 3,1SELECT 'This is the NON desired output:' commentSELECT * FROM #TESTSORTING ORDER BY ID ASCSELECT 'This is the actual output that CAN include the above:' commentSELECT * FROM #TESTSORTING ORDER BY NEWID()Drop Table #TESTSORTING This produces a fine randomized (enough for my needs) list.But I need the GroupedCol to remain ordered during this process thus NEVER like this :ID GroupedCol3 12 21 1Only like this (ignoring the ID's where GroupedCol=1):ID GroupedCol3 11 12 2or this :ID GroupedCol2 21 13 1I hope i have made senseThnxAndy |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-01-23 : 07:33:25
|
order by GroupedCol, NEWID() KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
|
|
ultradiv
Starting Member
41 Posts |
Posted - 2011-01-23 : 11:16:51
|
| I don't think I have made myself clear, very sorry.What I am trying to do is this;When you use order by [col] you effectively "group" rows when those [col]'s hold the same data in other rows. I want to randomize those "groups" in the returned table, so in my simplistic example the rows returned in GroupedCol would be 112Or211Never 121@khtan thanx but this orders the results without any random element other than within the "groups" in the example. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-01-23 : 22:27:25
|
is this what you want ?ORDER BY case when abs(checksum(newid())) % 2 = 0 then GroupedCol * -1 else GroupedCol end KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ultradiv
Starting Member
41 Posts |
Posted - 2011-01-24 : 03:53:05
|
| @khtan yes! Thankyou that seems to work just fine.Can you explain to me how it works please?Many thanksAndy |
 |
|
|
ultradiv
Starting Member
41 Posts |
Posted - 2011-01-24 : 04:05:31
|
| Although it does not continue to work when the quantity of "groups" increases :CREATE TABLE #TESTSORTING (ID INT ,GroupedCol INT)INSERT INTO #TESTSORTINGSELECT 1,1 UNION ALLSELECT 2,2 UNION ALLSELECT 3,2 UNION ALLSELECT 4,3 UNION ALLSELECT 5,3 UNION ALLSELECT 6,4 UNION ALLSELECT 7,4 UNION ALLSELECT 8,1SELECT * FROM #TESTSORTING ORDER BY case when abs(checksum(newid())) % 2 = 0 then GroupedCol * -1 else GroupedCol endDrop Table #TESTSORTING |
 |
|
|
ultradiv
Starting Member
41 Posts |
Posted - 2011-01-24 : 09:56:09
|
| I found a solution:;WITH RANDOMIZER AS( SELECT GroupedCol, MIN(ABS(CHECKSUM(NEWID()))) NWID FROM #TESTSORTING GROUP BY GroupedCol)SELECT TS.* FROM #TESTSORTING TSINNER JOIN RANDOMIZER RND ON TS.GroupedCol = RND.GroupedCol ORDER BY RND.NWID , TS.GroupedColThanks everyone for your help it is much appreciated |
 |
|
|
|
|
|
|
|