共用方式為


AVG - Cosmos DB 中的查詢語言 (在 Azure 和 Fabric 中)

函數 AVG 會計算運算式中值的平均值。

語法

AVG(<numeric_expr>)

Arguments

Description
numeric_expr 要計算平均值的數值運算式。

傳回類型

傳回數值純量值。

範例

本節包含如何使用此查詢語言建構的範例。

請考慮這些範例集合中的 Products 此範例文件集。

[
  {
    "name": "Diannis Watch",
    "price": 98,
    "detailCategory": "apparel-accessories-watches"
  },
  {
    "name": "Confira Watch",
    "price": 105,
    "detailCategory": "apparel-accessories-watches"
  }
]

單一住宿的平均價值

在此範例中,函 AVG 式可用來將屬性的 price 值平均化為單一彙總值。

SELECT
  AVG(p.price) AS averagePrice
FROM
  products p
WHERE
  p.detailCategory = "apparel-accessories-watches"
[
  {
    "averagePrice": 101.5
  }
]

備註

  • 此功能受益於範圍索引的使用。 如需詳細資訊,請參閱 範圍索引
  • 如果中 AVG 的任何引數是字串、布林值或 Null,則整個彙總系統函式會傳回 undefined
  • 如果任何引數有 undefined 值,則計算中 AVG 不會包含該特定值。