查询结果不返回十进制值

时间:2016-05-26 14:27:12

标签: sql sql-server

为什么以下查询不返回十进制值?我希望CustRating和Rating Difference列以十进制形式返回值。

SELECT 
     a.CustNo AS CustNo,
     a.CustRating AS CustomerRate,
     a.RatingDate AS RatingDate,
     (a.CustRating-b.CustRating) AS [Rating Difference]
FROM 
     MasterTable a
          JOIN MasterTable b ON b.CustNo = a.CustNo
ORDER BY 
     CustNo

这是原始表[MasterTable]

CustNo  CustRating  RatingDate
101AAA  0.000500000 "2016-01-08 00:00:00.000"
101AAA  0.000000750 "2016-01-12 00:00:00.000"
101AAA  0.000000550 "2016-01-22 00:00:00.000"
101AAA  0.000000000 "2016-01-27 00:00:00.000"

这是查询结果:

CustNo  CustRating  RatingDate                  Rating Difference
101AAA  0           "2016-01-08 00:00:00.000"   0
101AAA  0           "2016-01-12 00:00:00.000"   0
101AAA  0           "2016-01-22 00:00:00.000"   0
101AAA  0           "2016-01-27 00:00:00.000"   0

2 个答案:

答案 0 :(得分:2)

我假设你的列是一个整数?您可以轻松地使用转换为此。

CONVERT(decimal(9,2), a.CustRating) AS CustomerRate,
CONVERT(decimal(9,2), a.CustRating - b.CustRating) as RatingDifference

答案 1 :(得分:0)

如果你想在小数点后取10位数和3位数

SELECT a.CustNo AS CustNo ,
       CAST(a.CustRating as decimal(10,3)) AS CustomerRate,
       a.RatingDate AS RatingDate,
       CAST((a.CustRating-b.CustRating) as decimal(10,3)) AS [Rating Difference]
FROM MasterTable a
JOIN MasterTable b
ON b.CustNo = a.CustNo
ORDER BY CustNo