- You can't group by an alias, you need to specify the computed column
- This is not allowed :
sum(lower(left(Title,2))='wh')
. You have to convert it into aCASE WHEN
operator.
Here a corrected query (that gives a timeout):
select
month(CreationDate) month
, year(CreationDate) year
, sum(case when lower(left(Title,2))='wh' then 1 else 0 end)/count(*) wh
, (select sum(Score)/count(*)
from Posts u
where month(CreationDate)=month(t.CreationDate)
and year(CreationDate)=year(t.CreationDate)
and lower(left(Title,2))='wh'
and PostTypeId=1 -- question
) wh_score,
sum(Score)/count(*) score,
(select sum(AnswerCount)/count(*)
from Posts u
where month(CreationDate)=month(t.CreationDate)
and year(CreationDate)=year(t.CreationDate)
and lower(left(Title,2))='wh'
and PostTypeId=1 -- question
) wh_answers,
sum(AnswerCount)/count(*) answers
from Posts t
where PostTypeId=1 -- question
group by month(CreationDate), year(CreationDate);
What are you trying to do with this query?