2014-09-04 18 views
0

我使用Postgresql 9.2要改變一個類型的表達式在SELECT子句中

假設我有表:

id  date   click_count  registration_count 
serial timestamp   bigint    bigint 

我已經寫了下面的查詢:

select date, sum(click_count), 
    sum(registration_count), 
    (CASE 
      WHEN (not sum(click_count) = 0) 
      THEN sum(registration_count)/sum(click_count)*100 
      ELSE 0 
    END) as convert 
from myTbl 

它有效,但不正確。事情是我需要得到算術運算的雙重結果,但在結果中它有bigint類型。有沒有辦法解決這個問題?

回答

1

如何使用cast()

select date, sum(click_count), 
     sum(registration_count), 
     (CASE WHEN (not sum(click_count) = 0) 
      THEN cast(sum(registration_count) as double)/sum(click_count)*100 
      ELSE 0.0 
     END) as convert 
from myTbl 
+0

爲什麼顯式'cast()'?你可以簡單地把'100'改成'100.0',不是嗎? – canon 2014-09-04 14:36:18

+0

完美,謝謝。 – 2014-09-04 14:38:25

+0

@ canon看來,沒有。我執行了該查詢,但結果顯然不正確。 – 2014-09-04 14:39:34

相關問題