简体   繁体   English

带小数百分比细分的SQL计数-如何限制到小数点后4位?

[英]SQL count with decimal percentage breakdown - how do you limit to 4 decimal places?

I would like to alter the decimal places to 4 but can't figure it out, help! 我想将小数位更改为4,但无法弄清楚,请帮助!

--Query -查询

select CASE v2.[ie version]
      When '11' Then 'Internet Explorer 11'
      When '9' Then 'Internet Explorer 9'
      When '8' Then 'Internet Explorer 8'
      Else [IE Version] End [IE Version],
COUNT(distinct v1.guid) 'Total Count', COUNT(*) *100.00 / SUM(COUNT(*)) over()'Total Percentage' from vcomputer v1
inner join vIEVersions v2 on v1.guid = v2.guid where v1.ismanaged = '1'
and v2.[IE Version] is not Null and v2.[IE Version] not in ('Unknown', '7', '10')
group by v2.[IE Version] order by 1 desc

--Output -输出

IE Version               Total Count    Total Percentage
Internet Explorer 9      180            1.7349397590361
Internet Explorer 8      531            5.1180722891566
Internet Explorer 11     9664           93.1469879518072

I think that first you have to first CREATE TABLE VIEW of the decimal percentage breakdown and then call the round function the values of the sum like this example code: 我认为,首先您必须首先CREATE TABLE VIEW小数百分比细分的CREATE TABLE VIEW ,然后再将round的值称为round函数,例如以下示例代码:

SELECT ROUND(column_name,decimals) FROM table_name;

Hope it helps. 希望能帮助到你。

CAST转换为十进制就足够了:

CAST(COUNT(*) *100.00 / SUM(COUNT(*)) over() as decimal(12,4)) 

try this for example, 试试这个,例如

declare @i int=23 声明@i int = 23

select cast(@i*100.00/3 as decimal(10,4)) 选择强制转换(@ i * 100.00 / 3作为十进制(10,4))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM