Fixed bug where column as name would be used in where condition

Fixes bug in aggregate function that created where condition query error. 

Example:

Was:
```
WHERE catgegory.name as category_name LIKE '%test%'
```

Now: 
```
WHERE category.name LIKE '%test%'
```