如何求平均值精确到小数点后三位
我有以下代码。显示小数点后 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
改为
这样
,你就没有整数除法了......
Change
to
This way, you don't have integer division...
您可以使用此方法,但它也会对值进行四舍五入。
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
看起来问题是你在数学完成后进行转换。因此,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.Total 和total_days 都是整数,因此结果错误。
这应该有效。
Both total and total_days are integer, therefore you have wrong result.
This should works.
在 MySql 中你可以使用 FORMAT
In MySql you could use FORMAT