Using NEWID() with CTE to produce random subset of rows produces odd results
I'm writing some SQL in a stored procedure to reduce a dataset to a limited random number of rows that I want to report on. The report starts with a Group of Users and a filter is applied to specify the total number of random rows required ( @SampleLimit ). To achieve the desired result, I start by creating a CTE (temp table) with: The top(@SampleLimit) applied group by UserId (as the UserID appears multiple times) order by NEWID() to put the results in a random order SQL: ; with cte_temp as (select top(@SampleLimit) UserId from QueryResults where (GroupId = @GroupId) group by UserId order by