Monday 4 November 2013

Mini Post: Getting distinct random values using 'TOP' and 'NEWID()'

Heya everyone out there, Mini Post time!

...Too many of these don't you think? I really have to buckle down and write a longer post sometime.

Anyway let's focus on something I had to use 5 minutes ago (I have used it before, but 5 minutes ago was the most recent so I figured I'd jot this down quickly while its in my head).

Sometimes we wish to get back random rows from our table. For this we can use 'NEWID()' as follows:

SELECT top 10 Col1
FROM Table1
-- WHERE SomePredicate
ORDER BY NEWID();

The above will pull 10 random Col1 values, regardless if the same value can appear multiple times in Col1.

Now sometimes we wish to pull not only random values, but random distinct values.

It'd be nice if we could do something like:

--Incorrect
SELECT top DISTINCT 10 Col1
FROM Table1
-- WHERE SomePredicate
ORDER BY NEWID();

But alas, we'll get an error. So instead please use (the key point is the 'GROUP BY'):

--Correct
SELECT top 10 Col1
FROM Table1
-- WHERE SomePredicate
GROUP BY Col1
ORDER BY NEWID();

DISCLAIMER: As stated, I’m not an expert so please, PLEASE (by all means!) feel free to politely correct or comment as you see fit. Your feedback is always welcomed. :-)

No comments:

Post a Comment