我正在尝试使用SUM()来计算百分比来收集总数。
CONVERT(decimal(10,2), SUM(cola)/(SUM(cola)+SUM(colb)))
我检查过,SUM(可乐)= 3,SUM(colb)= 2;所以我期待它像......那样计算。
3/5 = .6;所以我希望得到回归,0.60,
以下是完整查询:
SELECT CASE WHEN this < 0 THEN '-'
ELSE '+' END AS 'Col1',
SUM(cola) as 'this', SUM(colb) as 'that',
CONVERT(decimal(10,2), SUM(cola)/(SUM(cola)+SUM(colb)) as 'PCT'
FROM Table1
WHERE colc = 'Condition'
GROUP BY CASE WHEN this < 0 THEN '-'
ELSE '+' END
答案 0 :(得分:2)
你正在做Integer division。
如果你想要一个浮动结果,你至少需要一个操作数是浮点类型。
答案 1 :(得分:0)
谢谢@Oded,(忘记将整数转换为十进制)所以,这就是我改变的......
SELECT CASE WHEN this < 0 THEN '-'
ELSE '+' END AS 'Col1',
SUM(cola) as 'this', SUM(colb) as 'that',
CONVERT(decimal(10,2), SUM(cola)/(CONVERT(decimal(10,2),
SUM(cola))+CONVERT(decimal(10,2), SUM(colb))) as 'PCT'
FROM Table1
WHERE colc = 'Condition'
GROUP BY CASE WHEN this < 0 THEN '-'
ELSE '+' END