Random Records with Criteria

Asked

Viewed 258 times

3

I would like some SQL command for a MS-Access database where I have 500 records and only to return 20 records so random being these (10 FEMININE and 10 MALE) and sorted in alphabetical order by name.


The structure of the table tbLista is quite simple...

    ID - Long (500 registros)
    NOME - Texto
    SEXO - Texto (FEMININO/MASCULINO)


This code is what I have, but returns only the records randomly...

SELECT TOP 20 tbNew.ID, tbNew.NOME, tbNew.SEXO
FROM (
    SELECT tbLista.ID, tbLista.NOME, tbLista.SEXO 
    FROM tbLista 
    ORDER BY Rnd(Len(ID))
) AS tbNew;


Thank you for your attention...

  • I’d like to share the code you’ve tried to make?

  • @Statelessdev - I’ve already managed to run random SQL code, but unsuccessfully filtering by genre... SELECT TOP 20 tbNew.ID, tbNew.NOME, tbNew.SEXO
FROM (SELECT tbLista.ID, tbLista.NOME, tbLista.SEXO FROM tbLista ORDER BY Rnd(Len(ID))) AS tbNew;

  • Edit your question and add that code there. It’s easier for people to view and help you ;)

  • @Statelessdev - OK!

1 answer

2


Working:

SELECT tbNew.ID, tbNew.NOME, tbNew.SEXO
FROM tbLista tbNew
WHERE tbNew.ID IN (
    (SELECT TOP 10 tbLista.ID 
        FROM tbLista
        WHERE tbLista.SEXO = 'FEMININO'
        ORDER BY Rnd(Len(ID)))
    UNION ALL
    (SELECT TOP 10 tbLista.ID 
        FROM tbLista
        WHERE tbLista.SEXO = 'MASCULINO'
        ORDER BY Rnd(Len(ID)))
)
ORDER BY tbNew.SEXO, tbNew.NOME

Explaining:

Like you just want 10 FEM and 10 MASC, done 2 Subqueries draw them and limit them individually and then unite them with the UNION ALL.

In the external select, I used the same table to find the results of IDs and order them with the ORDER BY in the end, first by sexo, then by nome.


Auxiliary:

What’s the difference between UNION and UNION ALL?






Ancient answer:

(didn’t work)

Probably the Access does not accept table alias, so it did not work the first way I passed:

SELECT tbNew.ID, tbNew.NOME, tbNew.SEXO
FROM (
    (SELECT TOP 10 tbLista.ID, tbLista.NOME, tbLista.SEXO 
        FROM tbLista
        WHERE tbLista.SEXO = 'FEMININO'
        ORDER BY Rnd(Len(ID)))
    UNION ALL
    (SELECT TOP 10 tbLista.ID, tbLista.NOME, tbLista.SEXO 
        FROM tbLista
        WHERE tbLista.SEXO = 'MASCULINO'
        ORDER BY Rnd(Len(ID)))
) AS tbNew
ORDER BY tbNew.SEXO, tbNew.NOME
  • Didn’t work... "Syntax error in JOIN operation"

  • I got it this way;(SELECT TOP 10 tbLista.ID, tbLista.NOME, tbLista.SEXO FROM tbLista WHERE tbLista.SEXO = 'FEMININO' ORDER BY Rnd(Len(ID)))
UNION ALL
(SELECT TOP 10 tbLista.ID, tbLista.NOME, tbLista.SEXO FROM tbLista WHERE tbLista.SEXO = 'MASCULINO' ORDER BY Rnd(Len(ID)))
&#But this way I can’t order by name...

  • What will sort is external select, with alias. Try SELECT ID, NOME, SEXO and ORDER BY SEXO, NOME, apart from the tbNew of all

  • Dude, I ran tests here, it’s fine. You’re running something together by accident, or whatever.

  • The error persists... "Erro de sintaxe na operação JOIN" The word UNION is selected... I even created a new db to run but it hasn’t worked yet... Anyway, thanks for the help!

  • @Maux It may be that Access does not accept subquery the way I did. I edited and did otherwise. See if it works.

  • Thanks for the help, it worked!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.