0

Stack Exchange Data Explorerは、Stack Exchange データベースに対する SQL クエリを許可します。私はこれを試しました—</p>

select
  month(CreationDate) month,
  year(CreationDate) year,
  sum(lower(left(Title,2))='wh')/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,year;

—しかし、サイトは私に言った

')' 付近の構文が正しくありません。「wh_score」付近の構文が正しくありません。「wh_answers」付近の構文が正しくありません。

理由がわかりません。誰でも助けてもらえますか?


私が試したこと、無駄に:

  • datepart(month,CreationDate)の代わりにmonth(CreationDate)(および同様にyear)
  • エイリアスに対して明示的as(その後、3 つのエラーのうち後半の 2 つは、エイリアスではなく「at」について不平を言いました)
  • 組み込み関数名ではないエイリアス
  • left(Title,2)それ以外のlower(left(Title,2))
  • andsで結​​合された 4 つのものの最初の 2 つと最後の 2 つを括弧で囲みます。
  • u.サブクエリの列名を明示する
4

1 に答える 1

1
  1. エイリアスでグループ化することはできません。計算列を指定する必要があります
  2. これは許可されていません: sum(lower(left(Title,2))='wh'). 演算子に変換する必要がありCASE WHENます。

ここで修正されたクエリ(タイムアウトを与える):

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);

このクエリで何をしようとしていますか?

于 2013-04-18T07:19:16.587 に答える