【问题标题】:Ms ACCESS and SQL: round to two decimals through queryMs ACCESS 和 SQL:通过查询四舍五入到小数点后两位
【发布时间】:2016-11-11 00:47:51
【问题描述】:

我使用查询来计算各种供应商信息(平均交货时间、该供应商的总支出、平均价格等)。所有输出都显示在 Ms ACCESS 表单的列表框中。

计算数字示例:

如何将这些查询的输出格式化为四舍五入到小数点后两位?我一直在玩列表框设置,但在那里找不到。我相信我必须在查询本身中这样做,但我不确定如何。

以上号码查询码:

SELECT Avg([Item Master].PlannedLeadTime) AS AverageLeadTime
FROM [Item Master]
WHERE ((([Item Master].DateStamp)>=[Forms]![History Supplier Tool]![List2] And ([Item Master].DateStamp)<=[Forms]![History Supplier Tool]![List3]) AND (([Item Master].SupplierName)=[Forms]![History Supplier Tool]![List1]));

注意:List1 是一个列表框,用户可以在其中选择某个供应商(对其执行计算),list2 和 list3 是用户可以选择的日期(以确定计算的日期范围)。

【问题讨论】:

  • 请注意,SELECT Round(17.065, 2) 返回 17.06。你会担心吗?
  • 不,没关系。
  • 此处的示例和链接均未演示如何根据您的标题要求进行四舍五入。
  • @Gustav 固定标题..
  • 好的,谢谢。另外,请注意 Round 有很多错误,因此如果准确性有任何重要性,请研究此链接:Rounding values up, down, by 4/5, or to significant figures 了解可预测将舍入任何数字数据类型的任何值的函数。

标签: sql ms-access


【解决方案1】:

Access SQL 有一个丰富的函数集,round 函数就是其中之一。

例子:

SELECT Round(Avg([Item Master].PlannedLeadTime),2) AS AverageLeadTime
FROM [Item Master]
WHERE (...)

更多信息: http://www.techonthenet.com/access/functions/numeric/round.php

【讨论】:

    【解决方案2】:

    使用 ROUND() 函数:

    ROUND() 函数用于将数值字段四舍五入到指定的小数位数。

    SQL ROUND() 语法:

    SELECT ROUND(column_name,decimals) FROM table_name;
    
    |Parameter    |Description
    ________________________________________________    
    |column_name  |Required. The field to round.
    |decimals     |Required. Specifies the number of decimals to be returned.
    

    因此,您需要的查询将是:

    SELECT ROUND(AVG([Item Master].PlannedLeadTime),2) AS AverageLeadTime
    FROM [Item Master]
    WHERE ((([Item Master].DateStamp)>=[Forms]![History Supplier Tool]![List2]
          And ([Item Master].DateStamp)<=[Forms]![History Supplier Tool]![List3])                   AND (([Item Master].SupplierName)=[Forms]![History Supplier Tool]![List1]));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多