如何求平均值精确到小数点后三位

发布于 2024-11-06 23:51:16 字数 1352 浏览 2 评论 0原文

我有以下代码。显示小数点后 3 位,但平均值错误。有没有办法可以重构代码以获得正确的结果?

with total_indi_days as
(
    SELECT COUNT(*) AS total, DATENAME(DW, DateLog) AS NameOfDay 
    FROM  Access 
    GROUP BY DATENAME(DW, DateLog) 
    --ORDER BY total DESC 
)
,total_overall_days as
(
    SELECT COUNT(*) as total_days FROM  Access
)
select str(((total * 100)/ total_days ), 7, 3) as average, total, total_days, NameOfDay 
from total_indi_days, total_overall_days

结果...

|average | total | total_days | NameOfDay
| 2.000  |  29   |   1000     | Sun           
| 18.000 | 188   |   1000     | Mon             
| 15.000 | 159   |   1000     | Tues         
| 20.000 | 207   |   1000     | Wed             
| 19.000 | 194   |   1000     | Thur           
| 17.000 | 171   |   1000     | Fri             
| 5.000  |  52   |   1000     | Sat  

应该是...

|average | total | total_days | NameOfDay
| 2.900  |  29   |   1000     | Sun           
| 18.800 | 188   |   1000     | Mon             
| 15.900 | 159   |   1000     | Tues         
| 20.700 | 207   |   1000     | Wed             
| 19.400 | 194   |   1000     | Thur           
| 17.100 | 171   |   1000     | Fri             
| 5.200  |  52   |   1000     | Sat  

我也想知道是否有更简单的方法来获得这个结果。考虑到我有 AVG 内置功能,“with”有点多,这对我来说不起作用,也许是因为我做错了事情。

I have the following code. The 3 decimal places are being displayed but the average is wrong. Is there a way I can restructure the code to achieve the correct result?

with total_indi_days as
(
    SELECT COUNT(*) AS total, DATENAME(DW, DateLog) AS NameOfDay 
    FROM  Access 
    GROUP BY DATENAME(DW, DateLog) 
    --ORDER BY total DESC 
)
,total_overall_days as
(
    SELECT COUNT(*) as total_days FROM  Access
)
select str(((total * 100)/ total_days ), 7, 3) as average, total, total_days, NameOfDay 
from total_indi_days, total_overall_days

result...

|average | total | total_days | NameOfDay
| 2.000  |  29   |   1000     | Sun           
| 18.000 | 188   |   1000     | Mon             
| 15.000 | 159   |   1000     | Tues         
| 20.000 | 207   |   1000     | Wed             
| 19.000 | 194   |   1000     | Thur           
| 17.000 | 171   |   1000     | Fri             
| 5.000  |  52   |   1000     | Sat  

should be...

|average | total | total_days | NameOfDay
| 2.900  |  29   |   1000     | Sun           
| 18.800 | 188   |   1000     | Mon             
| 15.900 | 159   |   1000     | Tues         
| 20.700 | 207   |   1000     | Wed             
| 19.400 | 194   |   1000     | Thur           
| 17.100 | 171   |   1000     | Fri             
| 5.200  |  52   |   1000     | Sat  

I would also like to know if there is a easier way to get this result. The "with" is a bit much, considering I have the AVG built in function, which did not work for me maybe because I was doing the wrong thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

往昔成烟 2024-11-13 23:51:16

改为

select str(((total * 100)/ total_days ), 7, 3) as average  to 

这样

select str(((total * 100.0)/ total_days ), 7, 3) as average

,你就没有整数除法了......

Change

select str(((total * 100)/ total_days ), 7, 3) as average  to 

to

select str(((total * 100.0)/ total_days ), 7, 3) as average

This way, you don't have integer division...

烟柳画桥 2024-11-13 23:51:16

您可以使用此方法,但它也会对值进行四舍五入。
CONVERT(Numeric(10,3), (Count(ID) * 100./@Total)) 作为百分比

You can use this methodology but it will also round the value.
CONVERT(Numeric(10,3), (Count(ID) * 100. / @Total)) as Percentage

温柔女人霸气范 2024-11-13 23:51:16

看起来问题是你在数学完成后进行转换。因此,SQL Server 对整数进行除法并给出整数结果,然后将其转换为小数。尝试将 100 更改为 100.0,这应该可以解决问题。

您还可以将列转换为小数,但只要其中一个值是小数,它就应该将其余值转换为小数形式给出答案,只需将 100 设为 100.0 即可最简单的方法做到这一点。

It looks like the problem is that you're converting AFTER the math has been done. So, SQL Server is doing the division on integers and giving an integer result which is THEN converted to a decimal. Try changing the 100 to 100.0 and that should correct the issue.

You could also CAST the columns to decimals, but as soon as one of the values is a decimal, it should convert the rest over to give the answer as a decimal and simply making the 100 a 100.0 is the easiest way to do that.

画离情绘悲伤 2024-11-13 23:51:16

Total 和total_days 都是整数,因此结果错误。

这应该有效。

select CAST((total * 100.000)/ total_days AS NUMERIC(19,3)) as average, total, total_days, NameOfDay from total_indi_days, total_overall_days 

Both total and total_days are integer, therefore you have wrong result.

This should works.

select CAST((total * 100.000)/ total_days AS NUMERIC(19,3)) as average, total, total_days, NameOfDay from total_indi_days, total_overall_days 
情魔剑神 2024-11-13 23:51:16

在 MySql 中你可以使用 FORMAT

SELECT FORMAT(12332.1,4) => '12,332.1000'

In MySql you could use FORMAT

SELECT FORMAT(12332.1,4) => '12,332.1000'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文