Getting non-integer results in Data Explorer

[亡魂溺海] 提交于 2020-01-17 08:05:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/16086325/getting-non-integer-results-in-data-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!