2013-04-18 70 views
-1

The Stack Exchange Data Explorer允許針對堆棧Exchange數據庫執行SQL查詢。下面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) 
; 

—的Scorpi0courtesy產生的所有整數值的結果:一切變得圓形的或者截斷(我不知道是哪個)。 (這在wh列中特別討厭,因此每個值都是0。)有沒有辦法強制使用小數或值?

回答

1

像許多語言一樣,當你做1/2時,它執行1的除以2並返回商,所以在這裏爲0。

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

讓我們務實:只需乘以一個小數,如:

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

,您將獲得爲int的小數代替。

+0

謝謝!這樣可行。 – msh210

相關問題