{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":476,"path":"api-reference/rest-apis/c_AggregateAPI.md","title":"Aggregate API","bundle":null,"doc_type":null,"release":"australia","body":"\n# Aggregate API\n\nThe Aggregate API provides endpoints that allow you to compute aggregate statistics about existing table and column data.\n\nThis API uses [GlideAggregate](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideAggregateScopedAPI.md) calls and only honors read-only queries if read access is defined for the table being called on based on role ownership.\n\n**Parent Topic:**[REST API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/rest-apis/api-rest.md)\n\n## Aggregate - GET /now/stats/\\{tableName\\}\n\nRetrieves records for the specified table and performs aggregate functions on the returned values.\n\nYou can specify which aggregate functions to perform by using either the **sysparm\\_&lt;aggregate&gt;\\_fields** parameter or **sysparm\\_having=&lt;aggregate&gt;^field^operator^value** parameter, substituting **&lt;aggregate&gt;** for one of these aggregate functions:\n\n-   avg\n-   max\n-   min\n-   sum\n\n### URL format\n\nVersioned URL: `/api/now/{api_version}/stats/{tableName}`\n\nDefault URL: `/api/now/stats/{tableName}`\n\n**Note:** Available versions are specified in the [REST API Explorer](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/rest-api-explorer/use-REST-API-Explorer.md). For scripted REST APIs there is additional version information on the [Scripted REST Service form](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/rest-api-explorer/c_CustomWebServices.md).\n\n### Supported request parameters\n\n<table class=\"rest_api_path_parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\napi\\_version\n\n</td><td id=\"version-entry-RESTAPI\">\n\nOptional. Version of the endpoint to access. For example, `v1` or `v2`. Only specify this value to use an endpoint version other than the latest. Data type: String\n\n</td></tr><tr><td>\n\ntableName\n\n</td><td>\n\nName of the table for which to retrieve records.Data type: String\n\n</td></tr></tbody>\n</table><table class=\"rest_api_query_parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nname-value pairs\n\n</td><td>\n\nAn alternative to using the **sysparm\\_query** parameter. You can filter a query using key-value pairs where the key is the name of a field.For example, instead of using the parameter `&sysparm_query=active=true`, you can use `&active=true`. You can use the display value when the field is a choice or reference type field, such as `&state=closed` instead of `&state=7`. To specify multiple key-value pairs, separate each with an ampersand, such as `&active=true&assigned_to=john.smith`.\n\nData type: String\n\n</td></tr><tr><td>\n\nsysparm\\_&lt;aggregate&gt;\\_fields\n\n</td><td>\n\nList of fields on which to perform each aggregate operation. You can specify multiple fields by separating each with a comma. For example, to get the average values from the duration and priority fields, use `sysparm_avg_fields=duration,priority`.**Note:** Specify this parameter, the **sysparm\\_count** parameter, or both for your query to return meaningful results. If neither parameter is passed, no aggregate operation is performed.\n\nData type: String\n\n</td></tr><tr><td>\n\nsysparm\\_count\n\n</td><td>\n\nFlag that determines whether to return the number of records returned by the query.**Note:** Specify this parameter, the **sysparm\\_&lt;aggregate&gt;\\_fields** parameter, or both for your query to return meaningful results. If neither parameter is passed, no aggregate operation is performed.\n\nData type: String\n\n</td></tr><tr><td>\n\nsysparm\\_display\\_value\n\n</td><td>\n\nData retrieval operation when grouping by reference or choice fields. Based on this value, the query returns either the display value, the actual value in the database, or both.-   true: Returns display values for all fields.\n-   false: Returns actual values from the database. If a value is not specified, this parameter defaults to false.\n-   all: Returns both actual and display values.\n\nThere is no preferred method for setting this parameter. However, specifying the display value may cause performance issues as they are not read from the database and may reference other fields and records. For more information on display values and actual values, see [Table API FAQs \\(KB0534905\\)](https://support.servicenow.com/kb_view.do?sysparm_article=KB0534905).Data type: String\n\n</td></tr><tr><td>\n\nsysparm\\_group\\_by\n\n</td><td>\n\nFields by which to group the returned data. You can specify multiple fields by separating each field with a comma, such as `sysparm_group_by=priority,state`.Data type: String\n\n</td></tr><tr><td>\n\nsysparm\\_having\n\n</td><td>\n\nAdditional query that enables you to filter the data based on an aggregate operation. The value for this parameter must follow the syntax `aggregate^field^operator^value`, such as `count^priority^>^3` to obtain the number of records within the query results with a priority greater than 3. You can specify multiple queries by separating each with a comma, such `ascount^state^=^1,avg^priority^>^3`.Data type: String\n\n</td></tr><tr><td>\n\nsysparm\\_order\\_by\n\n</td><td>\n\nList of values by which to order grouped results. You can specify an order using a field or an aggregate. For example, if you specify `sysparm_order_by=AVG^state`, groups of results with lower average state values are returned first. You can also order by COUNT to arrange groups of records by the number of records in each group.When you specify an order, groups are ordered in ascending order by default. Use `^DESC` to sort in descending order, such as `sysparm_order_by=state^DESC`.\n\nData type: String\n\n</td></tr><tr><td>\n\nsysparm\\_query\n\n</td><td>\n\nAn encoded query.For example: `(sysparm_query=active=true)(sysparm_query=caller_id=javascript:gs.getUserID()^active=true)`\n\nData type: String\n\n</td></tr></tbody>\n</table>|Name|Description|\n|----|-----------|\n|None| |\n\n### Headers\n\nThe following request and response headers apply to this HTTP action only, or apply to this action in a distinct way. For a list of general headers used in the REST API, see [Supported REST API headers](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/rest-api-explorer/c_RESTAPI.md).\n\n<table class=\"rest_api_request_headers\"><thead><tr><th>\n\nHeader\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nAccept\n\n</td><td id=\"accept-entry-RESTAPI\">\n\nData format of the response body. Supported types: **application/json** or **application/xml**. Default: **application/json**\n\n</td></tr></tbody>\n</table>|Header|Description|\n|------|-----------|\n|None| |\n\n### Status codes\n\nThe following status codes apply to this HTTP action. For a list of possible status codes used in the REST API, see [REST API HTTP response codes](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/rest-api-explorer/c_RESTAPI.md).\n\n|Status code|Description|\n|-----------|-----------|\n|200|Successful. The request was successfully processed.|\n|401|Unauthorized. The user credentials are incorrect or have not been passed.|\n|403|Forbidden. The user doesn't have access rights to the specified record.|\n|500|Internal server error. An unexpected error occurred while processing the request. The response contains additional information about the error.|\n\n### Response body parameters \\(JSON or XML\\)\n\n|Name|Description|\n|----|-----------|\n|Depends on specified table and specified request parameters.||\n\n### Sample cURL request\n\n```\ncurl \"https://instance.servicenow.com/api/now/stats/incident?sysparm_avg_fields=reassignment_count%2Cbusiness_stc&sysparm_group_by=assignment_group\" \\\n--request GET \\\n--header \"Accept:application/json\" \\\n--user \"username\":\"password\"\n\n```\n\n```\n{\n  \"result\": [\n    {\n      \"stats\": {\n        \"avg\": {\n          \"business_stc\": \"804162.7143\",\n          \"reassignment_count\": \"1.0000\"\n        }\n      },\n      \"groupby_fields\": [\n        {\n          \"value\": \"\",\n          \"field\": \"assignment_group\"\n        }\n      ]\n    },\n    {\n      \"stats\": {\n        \"avg\": {\n          \"business_stc\": \"2037371.0000\",\n          \"reassignment_count\": \"1.5000\"\n        }\n      },\n      \"groupby_fields\": [\n        {\n          \"value\": \"287ee6fea9fe198100ada7950d0b1b73\",\n          \"field\": \"assignment_group\"\n        }\n      ]\n    },\n    {\n      \"stats\": {\n        \"avg\": {\n          \"business_stc\": \"1821488.2857\",\n          \"reassignment_count\": \"1.1111\"\n        }\n      },\n      \"groupby_fields\": [\n        {\n          \"value\": \"8a5055c9c61122780043563ef53438e3\",\n          \"field\": \"assignment_group\"\n        }\n      ]\n    },\n    {\n      \"stats\": {\n        \"avg\": {\n          \"business_stc\": \"1730322.0000\",\n          \"reassignment_count\": \"1.2500\"\n        }\n      },\n      \"groupby_fields\": [\n        {\n          \"value\": \"287ebd7da9fe198100f92cc8d1d2154e\",\n          \"field\": \"assignment_group\"\n        }\n      ]\n    },\n    {\n      \"stats\": {\n        \"avg\": {\n          \"business_stc\": \"1564478.6250\",\n          \"reassignment_count\": \"1.2500\"\n        }\n      },\n      \"groupby_fields\": [\n        {\n          \"value\": \"d625dccec0a8016700a222a0f7900d06\",\n          \"field\": \"assignment_group\"\n        }\n      ]\n    },\n    {\n      \"stats\": {\n        \"avg\": {\n          \"business_stc\": \"1512202.2500\",\n          \"reassignment_count\": \"1.1111\"\n        }\n      },\n      \"groupby_fields\": [\n        {\n          \"value\": \"8a4dde73c6112278017a6a4baf547aa7\",\n          \"field\": \"assignment_group\"\n        }\n      ]\n    }\n  ]\n}\n```\n\n"}}