Reputation: 3362
I have a query that looks like this:
SELECT
date(created_at as date) as created_date,
id,
count(distinct file_name) as files_total,
null as files_error
FROM table
GROUP BY created_date, id
I do not have a columns called created_date
anywhere in my table.
Why does this query work?
Created_Date
is created when taking the date of created_at, in the select part of the query.
SQL is processed in this order:
from, where, group by, having, select, order by
Group By
comes before Select
.
In the query above, the alias didn't come until after the group by
, but for some reason, the alias works in the group by
.
Why is this?
Upvotes: 2
Views: 46
Reputation: 93694
You don't have a column named created_date
, but you have created a alias name created_date
here
date(created_at as date) as created_date,
In postgresql, alias names can be referred in Group by
that is why it was working
Logically Group by
clause might be processed after the select
though it is not the case in all the DBMS
Upvotes: 2