koopjs / winnow

Deprecated
Apache License 2.0
90 stars 18 forks source link

Fix group by with where clause. #126

Closed ahamilt closed 4 years ago

ahamilt commented 4 years ago

The SQL query created when using a GROUP BY with a WHERE clause was invalid, the GROUP BY was before the WHERE clause. This resulted in alasql failing to parse the query.

Fixed to separate adding the GROUP BY fields in the SELECT clause, and adding the actual GROUP BY clause later after the WHERE clause.

Added unit test to cover the changes.

Before the change, the generate SQL query looked like this:

SELECT AVG(properties->Trunk_Diameter) as avg_Trunk_Diameter, properties->Genus as Genus FROM ? GROUP BY properties->Genus WHERE properties->Trunk_Diameter > 10

After it looks like this:

SELECT AVG(properties->Trunk_Diameter) as avg_Trunk_Diameter, properties->Genus as Genus FROM ? WHERE properties->Trunk_Diameter > 10 GROUP BY properties->Genus