how  to select a random row with a group by clause in peewee
the question has been asked in stackoverflow : [link](https://stackoverflow.com/questions/28514736/how-to-select-a-random-row-with-a-group-by-clause) ,but i don't know how to translate it to peewee. please help me...
i am stuck in there for a long time.
 It can be done like this:

```python
objects_tbl = Table('objects')
images_tbl = Table('images')
subquery = (objects_tbl
            .select(images_tbl.c.image_path,
                    objects_tbl.c.id.alias('object_id'),
                    objects_tbl.c.name)
            .order_by(SQL('RAND()'))
            .join(images_tbl, join_type=JOIN.LEFT_OUTER, on=images_tbl.c.object_id == objects_tbl.c.id)
            .alias('z'))
query = (Select().select(subquery.c.object_id, subquery.c.name,
                         subquery.c.image_path)
         .from_(subquery)
         .group_by(subquery.c.object_id, subquery.c.name))
```

Output:
```SQL
--- query.sql()
SELECT "z"."object_id", "z"."name", "z"."image_path" 
FROM (
    SELECT "t1"."image_path", "t2"."id" AS "object_id", "t2"."name" 
    FROM "objects" AS "t2" 
    LEFT OUTER JOIN "images" AS "t1" ON ("t1"."object_id" = "t2"."id") 
    ORDER BY RAND()
) AS "z" GROUP BY "z"."object_id", "z"."name"
``` yeah, thank you very much, i solved the problem about 30 mininutes ago。 I found the answer in [charlesleifer.com/blog](http://charlesleifer.com/blog/querying-the-top-n-objects-per-group-with-peewee-orm/)
my code is this:

```
        random_query = (
            Sentence.select(Sentence, Level.difficulty)
            .join(Level, on=(Sentence.level_id == Level.id))
            .order_by(pw.fn.Rand())
            .where(
                Sentence.status == ResourceStatus.PUBLISHED,
                Level.status == ResourceStatus.PUBLISHED,
            )
        )
        sentences = (
            Sentence.select(
                random_query.c.status,
                random_query.c.content,
                random_query.c.id,
                random_query.c.level_id,
            )
            .from_(random_query)
            .group_by(random_query.c.difficulty)
        )
```

thank you very much~


 That's great. Then we should also close the issue. Good stuff, yep that example looks good.
like this: select object_id, name, image_path
from
  (SELECT images.image_path AS image_path, objects.id AS object_id, objects.name
  FROM objects LEFT JOIN images ON images.object_id = objects.id
  ORDER BY RAND()) as z
group by z.object_id, z.name