SQL sql中十进制列的舍入总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21950901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Round sum of decimal column in sql
提问by Raging Bull
I'm using this code to sum decimal values.
我正在使用此代码对十进制值求和。
SUM(Amount) as TotalAmount
Amount column is decimal datatype
金额列是十进制数据类型
Amount decimal (10,4)
After summing up the columns I get values like this
总结列后,我得到这样的值
13500.8765
12005.0000
My expected output should be like this
我的预期输出应该是这样的
13501
12005
回答by Raging Bull
The ROUND() function is used to round a numeric field to the number of decimals specified.
ROUND() 函数用于将数字字段舍入到指定的小数位数。
Syntax:
句法:
SELECT ROUND(column_name,decimals) FROM table_name;
For your problem, try this:
对于你的问题,试试这个:
CONVERT(int,ROUND(SUM(Amount),0)) as TotalAmount --Converting to int for removing the fractional part
Read more about ROUND
here.
阅读更多关于ROUND
这里。
回答by dnoeth
If you want to round and remove the fractional part you can simply CAST it to a DECIMAL with different precision:
如果您想舍入并删除小数部分,您可以简单地将其转换为具有不同精度的 DECIMAL:
select cast(SUM(Amount) as DECIMAL(18,0)) as TotalAmount
回答by Nagaraj S
回答by tHiNk_OuT_oF_bOx
select ROUND(SUM(Amount),0) as TOTALAmount from tableName
回答by sureshhh
DECLARE @A DECIMAL(13,5)
DECLARE @B DECIMAL(13,5)
SET @A = 156.221212
SET @B = 156.821212
SELECT CAST(@A+@B AS INT)
回答by Bernabé Tavarez
The following should be the correct expression to SUM decimal values:
以下应该是 SUM 十进制值的正确表达式:
SELECT CAST(SUM(CONVERT(FLOAT, Field8)) AS NVARCHAR(255))
FROM Session1
WHERE Field1='[*0*]';?
回答by Bernabé Tavarez
select CAST(ROUND(15.09,0)as decimal(18,0)) as RoundNo
Use this, I hope that it is helpful for you.
使用这个,希望对你有帮助。