Alasql: Alias on HAVING clause not working properly

Created on 7 Jun 2017  路  3Comments  路  Source: agershun/alasql

Hello!
The following query described on Having page seems to be not working properly with alias (I've tested both on my PC and on the jsFiddle example):

var res = alasql('SELECT *, COUNT(*) AS cnt FROM City \
GROUP BY Country \
HAVING cnt > 2');

Though, without alias, it works normally (e.g. with \"HAVING COUNT(*) > 2\").
Glad to help (:

! Bug Code provided to reproduced Help wanted Was previously working

Most helpful comment

@CeZL according to the order of operation in SQL, the query gets executed in the following order:
FROM Clause, ON, OUTER, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, TOP
Since HAVING gets executed before the SELECT clause, the aliases are not usable in the HAVING clause.

hope it helps.

All 3 comments

To be frank, I think the wrong thing on all of this is the example. I mean, I don't even remember a time in which I could use an alias from 'selected' aggregation on having clause using SQL. So, when I saw it on the examples I was kinda delighted and at the same time surprised haha

@CeZL according to the order of operation in SQL, the query gets executed in the following order:
FROM Clause, ON, OUTER, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, TOP
Since HAVING gets executed before the SELECT clause, the aliases are not usable in the HAVING clause.

hope it helps.

I was working previusly. With the well described response from @ariastaheri we might remove the feature if we can not get it working again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Pasc95 picture Pasc95  路  6Comments

mathiasrw picture mathiasrw  路  5Comments

daffodilistic picture daffodilistic  路  3Comments

mathiasrw picture mathiasrw  路  5Comments

AmyBlankenship picture AmyBlankenship  路  6Comments