0

Stack Exchange 数据资源管理器允许对 Stack Exchange 数据库进行 SQL 查询。以下查询——</p>

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

Scorpi0提供 — 在结果中产生所有整数值:所有内容都被舍入或截断(我不知道是哪个)。(这在列中特别烦人,wh因此每个值都是0.)有没有办法强制小数或值?

4

1 回答 1

2

像很多语言一样,当你做 1/2 时,它执行 1 除以 2 并返回商,所以这里是 0。

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

让我们务实一点:只需乘以小数,如下所示:

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

你会得到十进制而不是整数。

于 2013-04-18T15:04:55.600 回答