Referring alias name in subselect returns "Unknown column in 'where clause'" I have following table:

+ ---------- + --------- + --------- + -------- + ------------ + ---------- +
| Field      | Type      | Null      | Key      | Default      | Extra      |
+ ---------- + --------- + --------- + -------- + ------------ + ---------- +
| id         | int(11)   | NO        | PRI      |              | auto_increment |
| date       | datetime  | NO        |          |              |            |
| a          | int(11)   | NO        |          |              |            |
| b          | int(11)   | NO        |          |              |            |
| c          | int(11)   | NO        |          |              |            |
| d          | int(11)   | NO        |          |              |            |
| e          | int(11)   | NO        |          |              |            |
+ ---------- + --------- + --------- + -------- + ------------ + ---------- +
7 rows


I need to get number of rows, number of rows where a is '1', number of rows where a is '0' and frequency of '1's as percentage, in each hour of day. So, I ran this query:

select day,hour,c,100.0*(a1/c) as percentage,a1 from 
(
    select day(date) as day,hour(date) as hour,count(*) as c, (select count(*) from t where a=1 and hour(date)=hour) as a1 from t group by hour(date)
) as T;


Which I have Debian 8.0 - MySQL server version (5.5.?), and it returned correct results. Though it fails at home on Windows 7 MySQL 5.7.9 with following error:

Error Code: 1054. Unknown column 'hour' in 'where clause'


My question is if SQL is evaluated from inside out, so how the first query even worked? And what is the standard query to get this results?

Note
Just installed 5.5.13 and above query is working.
For the alternative way to count these:

select
    day(date) as day,
    hour(date) as hour,
    count(1) as c,
    sum(a=1) as a1,
    sum(a=0) as a0,
    (sum(a=1) / count(1)) as pct
from t
group by hour, date(date)


I added the date to the group by so the same hour of different days do not get counted together - change that the way you need.