Patrick Donnelly wrote:
> I'm trying to find a way to select a random row from a group (rather
> than "arbitrary"). Something like:
>
> SELECT attr1, attr2
> FROM foo
> GROUP BY attr1
> ORDER BY attr1, random()
>
> but of course ORDER BY is done after GROUP BY has selected an
> arbitrary row.

To do the random() before the GROUP BY, use a subquery:

    SELECT attr1,
           attr2,
           min(r)
    FROM (SELECT attr1,
                 attr2,
                 random() AS r
          FROM foo)
    GROUP BY attr1


Regards,
Clemens
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to