sql - syntax error in Data Explorer -
the stack exchange data explorer allows sql queries against stack exchange database. tried one —
select month(creationdate) month, year(creationdate) year, sum(lower(left(title,2))='wh')/count(*) wh, (select sum(score)/count(*) posts u month(creationdate)=month(t.creationdate) , year(creationdate)=year(t.creationdate) , lower(left(title,2))='wh' , posttypeid=1 -- question ) wh_score, sum(score)/count(*) score, (select sum(answercount)/count(*) posts u month(creationdate)=month(t.creationdate) , year(creationdate)=year(t.creationdate) , lower(left(title,2))='wh' , posttypeid=1 -- question ) wh_answers, sum(answercount)/count(*) answers posts t posttypeid=1 -- question group month,year; — site told me
incorrect syntax near ')'. incorrect syntax near 'wh_score'. incorrect syntax near 'wh_answers'.
and cannot figure out why. can help, please?
things i've tried, no avail:
datepart(month,creationdate)instead ofmonth(creationdate)(and likewiseyear)- explicit
asaliases (then latter 2 of 3 errors complained 'at' rather aliases) - aliases aren't built-in function names
left(title,2)instead oflower(left(title,2))- putting parentheses around first two, , around last two, of 4 things joined
ands - explicit
u.column names in subqueries
- you can't group alias, need specify computed column
- this not allowed :
sum(lower(left(title,2))='wh'). have convertcase whenoperator.
here corrected query (that gives timeout):
select month(creationdate) month , year(creationdate) year , sum(case when lower(left(title,2))='wh' 1 else 0 end)/count(*) wh , (select sum(score)/count(*) posts u month(creationdate)=month(t.creationdate) , year(creationdate)=year(t.creationdate) , lower(left(title,2))='wh' , posttypeid=1 -- question ) wh_score, sum(score)/count(*) score, (select sum(answercount)/count(*) posts u month(creationdate)=month(t.creationdate) , year(creationdate)=year(t.creationdate) , lower(left(title,2))='wh' , posttypeid=1 -- question ) wh_answers, sum(answercount)/count(*) answers posts t posttypeid=1 -- question group month(creationdate), year(creationdate); what trying query?
Comments
Post a Comment