【问题标题】:Decimal place in select statement sql选择语句sql中的小数位
【发布时间】:2017-09-11 13:21:52
【问题描述】:

我一直在使用一个完美的选择语句。但是我注意到它只返回零个小数位。我认为 Excel 在复制时已将其排除在外,但事实并非如此。

我已尝试更改格式,但仍然得到零位小数。我已经查看了此处的所有帖子,但由于我使用的是案例陈述,因此包含起来并不简单。

Round(AVG(case when [Duration] > 0 then [Duration] end), 3) as AVGLOS,

一如既往地欢迎任何帮助。

【问题讨论】:

  • Duration的数据类型是什么?
  • 您的方法似乎是正确的,但请尝试检查数据类型
  • 你可以将([Duration] as numeric(36,3))
  • 数据类型是int
  • 那是无效的标准 SQL。您使用的是哪个 DBMS?

标签: sql select case average


【解决方案1】:

数据类型是 int

那是你的问题。

整数值的平均值总是整数:

   declare @t table (col int);
   insert into @t values (2), (3);

   select avg(col)
   from @t;
----
2

所以你应该操作小数,而不是像这样的整数:

   declare @t table (col int);
   insert into @t values (2), (3);

   select avg(col * 1.)
   from @t;
---
2.500000

所以在你的情况下,只需使用这个:

Round(AVG(case when [Duration] > 0 then [Duration]* 1. end), 3) as AVGLOS

【讨论】:

    猜你喜欢
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2015-07-25
    • 1970-01-01
    相关资源
    最近更新 更多