For this example we are only concerned with the User and the record ID.
CODE
EXAMPLE TABLE
User | ID | +more
Ashley | 1 |
Ashley | 2 |
Ashley | 3 |
Brian | 5 |
Brian | 6 |
Ashley | 7 |
Ashley | 8 |
Brian | 9 |
Brian | 10 |
Brian | 11 |
Brian | 12 |
Ashley | 13 |
Brian | 14 |
User | ID | +more
Ashley | 1 |
Ashley | 2 |
Ashley | 3 |
Brian | 5 |
Brian | 6 |
Ashley | 7 |
Ashley | 8 |
Brian | 9 |
Brian | 10 |
Brian | 11 |
Brian | 12 |
Ashley | 13 |
Brian | 14 |
First I established the TOP 3 records per user using subqueries. This is based off of Allen Browns example from http://allenbrowne.com/subquery-01.html
CODE
EXAMPLE QUERY - SELECT TOP 3 per USER
SELECT a.ID, a.User
FROM tbl AS a
WHERE a.ID In
(SELECT Top 3 b.ID
FROM tbl AS b
WHERE b.User=a.User
ORDER BY b.User DESC, b.ID DESC
)
ORDER BY a.ID DESC, a.User DESC;
SELECT a.ID, a.User
FROM tbl AS a
WHERE a.ID In
(SELECT Top 3 b.ID
FROM tbl AS b
WHERE b.User=a.User
ORDER BY b.User DESC, b.ID DESC
)
ORDER BY a.ID DESC, a.User DESC;
CODE
EXAMPLE QUERY RESULTS - SELECT TOP 3 per USER
User | ID | +more
Ashley | 1 |
Ashley | 2 |
Ashley | 3 |
Brian | 5 |
Brian | 6 |
Brian | 9 |
User | ID | +more
Ashley | 1 |
Ashley | 2 |
Ashley | 3 |
Brian | 5 |
Brian | 6 |
Brian | 9 |
Moving onto select a random sample. There are lots of ways to get a random number. I have choosen to use RND(isnull(ID) * 0 + 1 + now()). Sorting the subquery by User then Random number will in therory select the first (or last) 3 records for that user.
CODE
EXAMPLE QUERY - SELECT RANDOM TOP 3 per USER
SELECT a.ID, a.User
FROM tbl AS a
WHERE a.ID In
(SELECT Top 3 b.ID
FROM tbl AS b
WHERE b.User=a.User
ORDER BY b.User DESC, rnd(isnull(b.ID) * 0 + 1 + now()) DESC, b.ID DESC
)
ORDER BY a.ID DESC, a.User DESC;
SELECT a.ID, a.User
FROM tbl AS a
WHERE a.ID In
(SELECT Top 3 b.ID
FROM tbl AS b
WHERE b.User=a.User
ORDER BY b.User DESC, rnd(isnull(b.ID) * 0 + 1 + now()) DESC, b.ID DESC
)
ORDER BY a.ID DESC, a.User DESC;
CODE
EXAMPLE QUERY RESULTS - SELECT RANDOM TOP 3 per USER
Um the results are random, HOWEVER, the query selects a random number of records per user instead of the TOP 3. An example would be...
User | ID | +more
Ashley | 3 |
Ashley | 7 |
Ashley | 8 |
Ashley | 13 |
Brian | 12 |
There was too many records for Ashley (4, where there should be 3), and too few records for Brian (1, where there should be 3)
Um the results are random, HOWEVER, the query selects a random number of records per user instead of the TOP 3. An example would be...
User | ID | +more
Ashley | 3 |
Ashley | 7 |
Ashley | 8 |
Ashley | 13 |
Brian | 12 |
There was too many records for Ashley (4, where there should be 3), and too few records for Brian (1, where there should be 3)
I did search UtterAccess and found the following link.
http://www.UtterAccess.com/forum/lofiversi...p/t1653519.html
Inside it states that the only way possible is to create a temp table with the random numbers, then run the query TOP N per Group.
So the first question would be, am I barking up the wrong tree (is what I am trying to do possible)?
If possible, what am I doing wrong?
Thank you.