[英]How do not to round the number in SQL
我有這樣的查詢:
Select Customer,JobType,sum(SThours),
sum(OThours),SortMonth,
str((sum(OThours)/sum(SThours)),5,2)AS Ratios
from #data
group by Customer,JobType,SortMonth
Sum (SThours): 688
sum(OThours): 618
該比率應為618/688 = 0.90,但我的結果是1.00
我是SQL新手,需要幫助。 謝謝。
嘗試將除法的一側或另一側強制轉換為浮點數-強制浮點而不是整數除法。
SELECT
Customer,
JobType,
sum(SThours),
sum(OThours),
SortMonth,
str((CAST(sum(OThours) AS FLOAT)/sum(SThours)),5,2) AS Ratios
FROM
#data
GROUP BY
Customer,
JobType,
SortMonth
您的問題是您正在執行整數除法,這將始終導致返回整數值。 在將它們相除之前,應將它們轉換為浮點數值(十進制應起作用)。
嘗試將數字轉換為十進制
SELECT
Customer,
JobType,
sum(SThours),
sum(OThours),
SortMonth,
str((CAST(sum(OThours) AS DECIMAL)/sum(SThours) ),5,2) AS Ratios
FROM
#data
GROUP BY
Customer, JobType, SortMonth
試試這個小提琴
這是顯示獲取十進制值的簡單示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.