通过


使用 OData 聚合数据

使用 OData $apply 选项在 Microsoft Dataverse 中聚合和分组数据。 通过此查询选项,可以对最多 50,000 条记录的集合执行总和、计数、平均值和分组操作等计算。

聚合函数处理最多 50,000 条记录的集合。 有关将聚合功能与 Dataverse 配合使用的详细信息,请参阅 使用 FetchXml 聚合数据

有关 OData 数据聚合的详细信息,请参阅 数据聚合版本 4.0 的 OData 扩展。 Dataverse 仅支持这些聚合方法的子集。

注释

  • groupby 不支持日期/时间值。

  • $orderby 不支持包含聚合值的 。 此限制返回错误: The query node SingleValueOpenPropertyAccess is not supported

示例

以下示例演示如何使用聚合函数:

这些示例为了简洁起见,不显示完整的请求和响应。

查询中唯一状态的列表

GET accounts?$apply=groupby((statuscode))
Prefer: odata.include-annotations="OData.Community.Display.V1.FormattedValue"

响应正文

{
    "@odata.context": "[Organization URI]/api/data/v9.2/$metadata#accounts",
    "value": [
        {
            "statuscode@OData.Community.Display.V1.FormattedValue": "Active",
            "statuscode": 1
        },
        {
            "statuscode@OData.Community.Display.V1.FormattedValue": "Inactive",
            "statuscode": 2
        }
    ]
}

按状态值计数

GET accounts?$apply=groupby((statuscode),aggregate($count as count))
Prefer: odata.include-annotations="OData.Community.Display.V1.FormattedValue"

响应正文

{
    "@odata.context": "[Organization URI]/api/data/v9.2/$metadata#accounts",
    "value": [
        {
            "statuscode@OData.Community.Display.V1.FormattedValue": "Active",
            "statuscode": 1,
            "count@OData.Community.Display.V1.FormattedValue": "8",
            "count": 8
        },
        {
            "statuscode@OData.Community.Display.V1.FormattedValue": "Inactive",
            "statuscode": 2,
            "count@OData.Community.Display.V1.FormattedValue": "1",
            "count": 1
        }
    ]
}

总收入总和

GET accounts?$apply=aggregate(revenue with sum as total)
Prefer: odata.include-annotations="OData.Community.Display.V1.FormattedValue"

响应正文

{
    "@odata.context": "[Organization URI]/api/data/v9.2/$metadata#accounts",
    "value": [
        {
            "total@OData.Community.Display.V1.FormattedValue": "$440,000.00",
            "total": 440000.000000000
        }
    ]
}

基于状态的平均收入

GET accounts?$apply=groupby((statuscode),aggregate(revenue with average as averagevalue))
Prefer: odata.include-annotations="OData.Community.Display.V1.FormattedValue"

响应正文

{
    "@odata.context": "[Organization URI]/api/data/v9.2/$metadata#accounts",
    "value": [
        {
            "statuscode@OData.Community.Display.V1.FormattedValue": "Active",
            "statuscode": 1,
            "averagevalue@OData.Community.Display.V1.FormattedValue": "$53,750.00",
            "averagevalue": 53750.000000000
        },
        {
            "statuscode@OData.Community.Display.V1.FormattedValue": "Inactive",
            "statuscode": 2,
            "averagevalue@OData.Community.Display.V1.FormattedValue": "$10,000.00",
            "averagevalue": 10000.000000000
        }
    ]
}

基于状态的收入总和

GET accounts?$apply=groupby((statuscode),aggregate(revenue with sum as total))
Prefer: odata.include-annotations="OData.Community.Display.V1.FormattedValue"

响应正文

{
    "@odata.context": "[Organization URI]/api/data/v9.2/$metadata#accounts",
    "value": [
        {
            "statuscode@OData.Community.Display.V1.FormattedValue": "Active",
            "statuscode": 1,
            "total@OData.Community.Display.V1.FormattedValue": "$430,000.00",
            "total": 430000.000000000
        },
        {
            "statuscode@OData.Community.Display.V1.FormattedValue": "Inactive",
            "statuscode": 2,
            "total@OData.Community.Display.V1.FormattedValue": "$10,000.00",
            "total": 10000.000000000
        }
    ]
}

按主要联系人姓名列出的帐户总收入

GET accounts?$apply=groupby((primarycontactid/fullname),aggregate(revenue with sum as total))
Prefer: odata.include-annotations="OData.Community.Display.V1.FormattedValue"

响应正文

{
    "@odata.context": "[Organization URI]/api/data/v9.2/$metadata#accounts",
    "value": [
        {
            "total@OData.Community.Display.V1.FormattedValue": "$10,000.00",
            "total": 10000.000000000,
            "contact_fullname": "Jim Glynn (sample)"
        },
        {
            "total@OData.Community.Display.V1.FormattedValue": "$80,000.00",
            "total": 80000.000000000,
            "contact_fullname": "Maria Campbell (sample)"
        },
      ... <truncated for brevity>
      
    ]
}

“WA”中帐户的主要联系人姓名

GET accounts?$apply=filter(address1_stateorprovince eq 'WA')/groupby((primarycontactid/fullname))

响应正文

{
    "@odata.context": "[Organization URI]/api/data/v9.2/$metadata#accounts",
    "value": [
        {
            "contact_fullname": "Rene Valdes (sample)"
        },
        {
            "contact_fullname": "Robert Lyon (sample)"
        },
        {
            "contact_fullname": "Scott Konersmann (sample)"
        }
    ]
}

上次创建的记录日期和时间

GET accounts?$apply=aggregate(createdon with max as lastCreate)
Prefer: odata.include-annotations="OData.Community.Display.V1.FormattedValue"

响应正文

{
    "@odata.context": "[Organization URI]/api/data/v9.2/$metadata#accounts",
    "value": [
        {
            "lastCreate@OData.Community.Display.V1.FormattedValue": "3/25/2023 10:42 AM",
            "lastCreate": "2023-03-25T17:42:47Z"
        }
    ]
}

首次创建的记录日期和时间

GET accounts?$apply=aggregate(createdon with min as firstCreate)
Prefer: odata.include-annotations="OData.Community.Display.V1.FormattedValue"

响应正文

{
    "@odata.context": "[Organization URI]/api/data/v9.2/$metadata#accounts",
    "value": [
        {
            "firstCreate@OData.Community.Display.V1.FormattedValue": "3/25/2023 10:42 AM",
            "firstCreate": "2023-03-25T17:42:46Z"
        }
    ]
}

唯一列值

OData 没有用于将结果限制为唯一 $distinct 值的查询选项。 请改用$apply转换中的groupby系统查询选项。 此方法为每个属性返回非重复值。

请求

GET [Organization URI]/api/data/v9.2/accounts?$apply=groupby((statecode,statuscode,accountcategorycode))
Accept: application/json  
OData-MaxVersion: 4.0  
OData-Version: 4.0
Prefer: odata.include-annotations="OData.Community.Display.V1.FormattedValue"

响应:

HTTP/1.1 200 OK  
Content-Type: application/json; odata.metadata=minimal  
OData-Version: 4.0  
Preference-Applied: odata.include-annotations="OData.Community.Display.V1.FormattedValue"

{
   "@odata.context": "[Organization URI]/api/data/v9.2/$metadata#accounts",
   "value": [
      {
         "statuscode": 1,
         "statecode": 0
      },
      {
         "statuscode": 1,
         "statecode": 0,
         "accountcategorycode": 1
      },
      {
         "statuscode": 1,
         "statecode": 0,
         "accountcategorycode": 2
      },
      {
         "statuscode": 2,
         "statecode": 1
      }
   ]
}

OData 聚合限制

本部分介绍了使用 FetchXml 聚合所提供的、当前通过 OData 无法实现的功能。

使用 CountColumn 获取非重复数

不能通过 OData 使用 CountColumn 获取不同的值数。 了解如何使用 FetchXml 获取不同的列值

按日期分组时的时区

按日期部分进行分组始终使用 UTC 时间,无法指定应改用用户的时区。 了解如何按 FetchXml 中的日期部分进行分组

行聚合

当表 定义了分层关系时,不能在分层关系的查找列上返回行聚合。 了解使用 FetchXml 的行聚合

单个查询限制

不能指定可配置的聚合限制。 了解使用 FetchXml 的每个查询限制

局限性

返回聚合值的查询限制为 50,000 条记录。 此限制有助于维护系统性能和可靠性。 如果查询中的筛选条件返回超过 50,000 条记录,则会出现以下错误:

编号: -2147164125
代码:8004E023
消息:AggregateQueryRecordLimit exceeded. Cannot perform this operation.
客户端错误消息:超出最大记录限制。 请减少记录数。

若要避免此错误,请将适当的筛选器添加到查询,以确保它不会评估超过 50,000 条记录。 然后多次运行查询并合并结果。 适当的筛选器取决于数据的性质,但它们可能是所选列中的日期范围或值的子集。

后续步骤

了解如何计数行。