質問

The Stack Exchange Data Explorer allows SQL queries against a Stack Exchange database. The following query

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

courtesy of Scorpi0 — yields all integer values in the results: everything gets either rounded or truncated (I don't know which). (This is especially annoying in the wh column, where every value is therefore 0.) Is there a way to force decimal or values?

役に立ちましたか?

解決

Like a lot of langage, when you do 1/2, it perform the division of 1 by 2 and return the quotient, so 0 here.

n/m => n = m * q + r
1/2 => 1 = 2 * 0 + 1

Let's be pragmatic : just multiply by a decimal, like this :

(sum(AnswerCount) * 1.0)/count(*)

And you will get decimal instead of int.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top