获得非整数结果在数据资源管理器(Getting non-integer results in Dat

2019-11-02 02:05发布

堆栈交换数据资源管理器允许对一个Stack Exchange数据库的SQL查询。 下面的查询 -

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 。)有没有办法来强制十进制或值?

Answer 1:

像很多langage的,当你做的1/2,它由2执行1的区别,并返回商,所以在这里0。

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

让我们务实:只是一个小数乘以,就像这样:

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

你会得到INT小数代替。



文章来源: Getting non-integer results in Data Explorer