2013-03-20 71 views
2

我有以下,但我需要百分比的答案,我一直得到0.我想知道如何四捨五入我的答案。提前致謝!如何四捨五入我的答案

SELECT (COUNT(t1.[ENTRY CODE])/2948) AS 'SecondYearRetention' 

FROM 
     t1 

Inner join 
     t2 ON t1.CLID = t2.CLID 

WHERE t1.[ENTRY CODE] = 'FTF' OR t1.[ENTRY CODE] = 'CFTF' 

回答

2

嘗試2948.0而不是2948

SELECT 
    (COUNT(t1.[ENTRY CODE])/2948.0) AS 'SecondYearRetention' 
FROM t1 
Inner join t2 ON t1.CLID = t2.CLID 
WHERE t1.[ENTRY CODE] = 'FTF' OR t1.[ENTRY CODE] = 'CFTF' 
+0

它的工作,但我得到了7位小數。我怎樣才能使它3小數?非常感謝! – user2134610 2013-03-20 07:24:51

+2

@ user2134610 - 不客氣:)使用['ROUND'](http://msdn.microsoft.com/zh-cn/library/ms175003.aspx)與您想要的進動。像'ROUND((COUNT(t1。[ENTRY CODE])/ 2948.0),3)'[**在這裏看到它在行動**](http://www.sqlfiddle.com/#!3/d41d8/11278 ) – 2013-03-20 07:30:26

0
SELECT ROUND((COUNT(t1.[ENTRY CODE])/2948),3)AS 'SecondYearRetention' 

FROM 
     t1 

Inner join 
     t2 ON t1.CLID = t2.CLID 

WHERE t1.[ENTRY CODE] = 'FTF' OR t1.[ENTRY CODE] = 'CFTF' 

試試這個更多的結果http://msdn.microsoft.com/en-us/library/ms175003.aspx

+0

這個例子會給你0,因爲除以兩個整數給整數,而ROUND()函數什麼也不做 – veljasije 2013-03-21 06:52:00

0

您也可以使用CAST or CONVERT功能與DATA_TYPE這是必要的,你

SELECT 
    (CAST(COUNT(t1.[ENTRY CODE]) AS decimal(9,8))/2948) AS 'SecondYearRetention' 
FROM t1 
Inner join t2 ON t1.CLID = t2.CLID 
WHERE t1.[ENTRY CODE] = 'FTF' OR t1.[ENTRY CODE] = 'CFTF' 

OR

SELECT 
    (CONVERT(decimal(9,8),COUNT(t1.[ENTRY CODE]))/2948) AS 'SecondYearRetention' 
FROM t1 
Inner join t2 ON t1.CLID = t2.CLID 
WHERE t1.[ENTRY CODE] = 'FTF' OR t1.[ENTRY CODE] = 'CFTF' 
0

你可以試試這個:

SELECT ROUND(CAST(COUNT(t1.[ENTRY CODE]) AS FLOAT)/2948),3) AS 'SecondYearRetention' 

FROM 
     t1 

Inner join 
     t2 ON t1.CLID = t2.CLID 

WHERE t1.[ENTRY CODE] = 'FTF' OR t1.[ENTRY CODE] = 'CFTF'