Sub-select in from clause
peewee's APIs are geared towards selecting from a table. `Model.select()` implies that you are selecting from the table represented by `Model` and want to be returned `Model` instances (excepting special cases like `.scalar()`, `.aggregate()`, `.tuples()`, `.dicts()`).

I haven't really put much effort into making it work with a sub-select in the from clause, partially because it doesn't fit very well into the rubric of returning model instances.  I see the value of sub-selects, just haven't built it out yet.

I think it'd be a good feature so I'll look into how to handle this.
 I believe the commit I just pushed 22ce07c will make it possible to achieve the queries you've listed.  Take a look at the included testcase.  I'm still not sure this is going to be a flexible enough solution to handle some of the other cases that come to mind, though. Particularly to select from multiple tables and join in the where clause.

I'm using peewee2.1 with python3.3 and an sqlite3.7 database.

I want to perform certain SELECT queries in which:

I first select some aggregate (count, sum), grouping by some id column; then
I then select from the results of (1), aggregating over its aggregate. For example, I want in some cases to count the number of rows in (1) that have each aggregated value.
My database has an 'Event' table with 1 record per event, and a 'Ticket' table with 1..N tickets per event. Each ticket record contains the event's id as a foreign key. Each ticket also contains a 'seats' column that specifies the number of seats purchased. (A "ticket" is really best thought of as a purchase transaction for 1 or more seats at the event.)

Below are two examples of working SQLite queries of this sort that give me the desired results:

``` sql
SELECT ev_tix, count(1) AS ev_tix_n FROM 
(SELECT count(1) AS ev_tix FROM ticket GROUP BY event_id)
GROUP BY ev_tix
```

``` sql
SELECT seat_tot, count(1) AS seat_tot_n FROM
(SELECT sum(seats) AS seat_tot FROM ticket GROUP BY event_id)
GROUP BY seat_tot
```

I can execute these through peewee as raw queries, but I don't know how to do them using peewee directly. Specifically, I don't know how to select on the inner query's aggregate (count or sum) when specifying the outer query. I can of course specify an alias for that aggregate, but it seems I can't use that alias in the outer query.

Is it in fact possible to do these selects without relying on raw queries? If so, how?

Note that I ran into a separate problem conducting these through peewee as raw queries when I updgraded from peewee 2.1.5 to 2.1.7. You can see my separate post about that: https://groups.google.com/forum/#!topic/peewee-orm/ZIXEJzTFxxY.

Thanks for your help!
