irstv/orbisgis — pull request #3 by gourlaysama (2012-06-22 12:33:59+00:00)
Title: Aggregate functions in Having clauses

This adds support for aggregate functions in SQL HAVING clauses.

The usual syntax like this is supported:
```sql
SELECT field FROM myTable GROUP BY field HAVING sum(otherField) < 18;
```

I chose here to stick to PostgreSQL 'standard' behavior when there is no GROUP BY clause, only a HAVING clause. There are two ways to interpret this kind of query:
* in PostgreSQL, the whole table is a group, you can only reference fields inside aggregate functions. Thus the following query returns either one or zero rows depending on the number of lines in the table:

```sql
SELECT 1 FROM myTable HAVING count(*) >= 10;
```
* In some other dialects, like T-SQL, there is a much more powerful behavior: the aggregate is evaluated on the whole table but HAVING still processes individual rows. Thus the following keeps all rows whose value is below the average of the whole table:

```sql
SELECT * FROM myTable HAVING myField < avg(myField);
```

@ebocher, @gpetit: I only implemented the first possibility here, but do you think the second one might be better?

Note: the two options are mutually exclusive.