Union Query and "Order by RND" (1 Viewer)

alain8225

New member
Local time
Today, 07:23
Joined
Jan 21, 2018
Messages
2
Hi,
(sorry for my English, I'm french)

I have a Table divided into several groups (A, B, C, etc.).

In each group, I need to extract some random records
and put them in one form.

For my tests, I make these 3 queries (separately, they working good for me):

==================
SELECT TOP 5 Rnd([ID]) AS Expr1, Table1.ID, Table1.Group
FROM Table1
WHERE (((Table1.Group)="A"))
ORDER BY Rnd([ID]);

SELECT TOP 2 Rnd([ID]) AS Expr1, Table1.ID, Table1.Group
FROM Table1
WHERE (((Table1.Group)="B"))
ORDER BY Rnd([ID]);

SELECT TOP 3 Rnd([ID]) AS Expr1, Table1.ID, Table1.Group
FROM Table1
WHERE (((Table1.Group)="C"))
ORDER BY Rnd([ID]);
==================

But when I try to create a UNION Query, the "Order by" applies only on the first item. I understant it's the normal beviator. I need the random in each group...

Do you know how can I create a form containing the result of these queries?

Thanks!
Alain
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:23
Joined
May 7, 2009
Messages
19,237
Add another calc field in your union

Select top 5 "1" as grp, rnd(id) as expr1,...
Union
Select top 2 "2" as grp, rnd(id) as expr1,...
Union
...
...

You dont need to group it.
 
Last edited:

Users who are viewing this thread

Top Bottom