如何获取最多2位小数的值?

时间:2016-03-11 05:35:13

标签: sql sql-server sql-server-ce

我有以下疑问。

insert into tbl_percount([H1Cycle],[perOffshore],[perOnsite],[perTotal])
(select [H1Cycle],Round(cast([Offshore]*100.0/(select [Offshore] from tbl_FinalLastday where [H1Cycle]='H1 Total')as float),2) as 'perOffshore',
Round(cast([Onsite]*100.0/(select [Onsite] from tbl_FinalLastday where [H1Cycle]='H1 Total')as float),2) as 'perOnsite',
Round(cast([Total]*100.0/(select [Total] from tbl_FinalLastday where [H1Cycle]='H1 Total')as float),2) as 'perTotal'
 from tbl_FinalLastday where [H1Cycle]!='H1 Total')

 insert into tbl_percount([H1Cycle],[perOffshore],[perOnsite],[perTotal])
(select [H1Cycle],Round(cast([Offshore]*100/(select [Total]  from tbl_FinalLastday where [H1Cycle]='H1 Total')as float),2) as 'perOffshore',
Round(cast([Onsite]*100.0/(select [Total] from tbl_FinalLastday where [H1Cycle]='H1 Total')as float),2) as 'perOnsite',
Round(cast([Total]*100.0/(select [Total] from tbl_FinalLastday where [H1Cycle]='H1 Total')as float),2) as 'perTotal'
 from tbl_FinalLastday where [H1Cycle]='H1 Total')

enter image description here

我想将输出插入表格' tbl_percount'。在第一种情况下,它给我正确的输出(前4行)。但在第二种情况下(最后一行),插入查询,通过舍入小数值给出整数值。

我希望输出为71.00,29.00,100.00

我怎么能得到这个?

1 个答案:

答案 0 :(得分:0)

您需要cast作为numeric(3,2),而不是float。 尝试以下方法:

select cast(round([Offshore] * 100 / (
                SELECT [Total]
                FROM tbl_FinalLastday
                WHERE [H1Cycle] = 'H1 Total'
                ), 2) AS NUMERIC(3, 2)) AS 'perOffshore'