Link to home
Start Free TrialLog in
Avatar of earwig75
earwig75

asked on

Random Sample from MS SQL Table

I have a table with some rows that I want to grab a sample of.  One of the columns in the table is UserID.

I want to grab a sample of 20 records for each UserID. Could someone help me with the SQL statement?

I tried using the below but it returns the same results each time.

WITH CTE AS (
SELECT ROW_NUMBER() OVER (PARTITION BY UserID ORDER BY UserID) as rn, *
FROM TableUserInfo
)
SELECT * FROM CTE 
WHERE rn<=20

Open in new window


Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial