是否可以从IIF语句中将CAST转换为FLOAT?
SELECT TOP 100 PeopleID, RateCount, IIF (People.RateCount < 100,
CAST(People.RateCount/100 as FLOAT), 1) as RateWeight
FROM People WHERE People.RateCount > 0 ORDER BY People.DateAdded DESC
我得到的结果是
PeopleID RateCount RateWeight
8548 674 1
135698 30 0
426755 2 0
336714 106 1
276739 43 0
536577 7 0
57674 81 0
79670 32 0
44674 901 1
146784 16 0
我想要的是
PeopleID RateCount RateWeight
8548 674 1
135698 30 .30
426755 2 .02
336714 106 1
276739 43 .43
536577 7 .07
57674 81 .81
79670 32 .32
44674 901 1
146784 16 .16
有更好的方法吗?
答案 0 :(得分:2)
进行除法时,被除数的数据类型控制结果数据类型。要获得FLOAT或DECIMAL结果,其中一个数字必须已经是FLOAT或DECIMAL,即:
SELECT TOP 100 PeopleID,
RateCount,
IIF (People.RateCount < 100, People.RateCount/CAST(100 as FLOAT), 1) as RateWeight
FROM People
WHERE People.RateCount > 0
ORDER BY People.DateAdded DESC
或者,摆脱CAST,将值100设为100.0
SELECT TOP 100 PeopleID,
RateCount,
IIF (People.RateCount < 100, People.RateCount/100.0, 1) as RateWeight
FROM People
WHERE People.RateCount > 0
ORDER BY People.DateAdded DESC