Randomization

We use several databases at work, at least Oracle and MSSQL. Not sure why we should use both options, but this question is about MSSQL.

There is a table, let’s just name it System..DirectMapping, which has a field account ID. I can not guarantee that they are all numeric, but they seem.

So really my question has two related parts.

First, I need to find a valid account id. This is the one that is in the table. This is straightforward, but I would like to get a random option.

So i did

select distinct accountID from System..DirectMapping 

which works, but they are always in the same order. I read how you could randomize something using newid (), so I tried

 select distinct accountID from System..DirectMapping order by newid() 

, , .

 select accountID from
 (select distinct accountID from System..DirectMapping) j
 order by newid()

, . , , java Collections.shuffle(), , ( , -10..). ?

.

+4
1

ORDER BY , -:

 select TOP 100 PERCENT accountID from
 (select distinct accountID from System..DirectMapping) j
 order by newid()
+3

Source: https://habr.com/ru/post/1677006/


All Articles