Selectiong random records with multiple criterion

  • Hola,

    I'm trying to select random records using ORDER BY NEWID(). That works well with...

    USE database

    SELECT TOP 3 C_division, C_name, C_address

    FROM tableC

    ORDER BY NEWID()

    ...however, there are multiple divisions in C_division. I would like to see the random top 3 for each division. Would this involve SELECT's in sub-queries? Anyone have any ideas?

    Gracias

  • the random Top three for each division implies more than 3 records, if that's what you want maybe try:

    SELECT  t1.C_division, t1.C_name, t1.C_address

    FROM tableC t1

    where pkey in (select top 3 pkey

                   from tableC t2

            where t1.C_division = t2.C_division

            ORDER BY NEWID() )


    * Noel

  • Thank you for the help, Noel.

    We're on the right track because I am getting multiple divisions with random records for each. However, it never seems to be just 3. Some divisions will return 1 while another 4 or 2 or maybe none at all. Any thoughts?

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply