| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- {
- "9d0581025d6e4717fc60b90c6f63a6a9c766b6c69c2bbdc985e47be6c95fcf1b": {
- "original": "<!-- example group7 -->\n##### GROUPBY()\nThere is a function `GROUPBY()` which returns the key of the current group. It's useful in many cases, especially when you [GROUP BY an MVA](../Searching/Grouping.md#Grouping-by-MVA-%28multi-value-attributes%29) or a [JSON value](../Searching/Grouping.md#Grouping-by-a-JSON-node).\n\nIt can also be used in `HAVING`, for example, to keep only years 2000 and 2002.\n\nNote that `GROUPBY()`is not recommended for use when you GROUP BY multiple fields at once. It will still work, but since the group key in this case is a compound of field values, it may not appear the way you expect.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_44\n<!-- response SQL -->\nCODE_BLOCK_45\n<!-- end -->\n<!-- example mva -->\n##### Grouping by MVA (multi-value attributes)\nManticore supports grouping by [MVA](../Creating_a_table/Data_types.md#Multi-value-integer-%28MVA%29). To demonstrate how it works, let's create a table \"shoes\" with MVA \"sizes\" and insert a few documents into it:\nCODE_BLOCK_46\nso we have:\nCODE_BLOCK_47\nIf we now GROUP BY \"sizes\", it will process all our multi-value attributes and return an aggregation for each, in this case just the count:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_48\n<!-- response SQL -->\nCODE_BLOCK_49\n\n<!-- request JSON -->\nCODE_BLOCK_50\n<!-- response JSON -->\nCODE_BLOCK_51\n<!-- request PHP -->\nCODE_BLOCK_52\n<!-- response PHP -->\nCODE_BLOCK_53\n<!-- request Python -->\nCODE_BLOCK_54\n<!-- response Python -->\nCODE_BLOCK_55\n<!-- request Javascript -->\nCODE_BLOCK_56\n\n<!-- request Python-asyncio -->\nCODE_BLOCK_57\n<!-- response Python-asyncio -->\nCODE_BLOCK_58\n\n<!-- request Javascript -->\nCODE_BLOCK_59\n\n<!-- response Javascript -->\nCODE_BLOCK_60\n<!-- request Java -->\nCODE_BLOCK_61\n<!-- response Java -->\nCODE_BLOCK_62\n\n<!-- request C# -->\nCODE_BLOCK_63\n<!-- response C# -->\nCODE_BLOCK_64\n\n<!-- request Rust -->\nCODE_BLOCK_65\n<!-- response Rust -->\nCODE_BLOCK_66\nres = await searchApi.search({\n index: 'test',\n aggs: {\n mva_agg: {\n terms: { field: \"mva_field\", size: 2 }\n }\n }\n});\nCODE_BLOCK_67\n{\n\t\"took\":0,\n\t\"timed_out\":false,\n\t\"aggregations\":\n\t{\n\t\t\"mva_agg\":\n\t\t{\n\t\t\t\"buckets\":\n\t\t\t[{\n\t\t\t\t\"key\":1,\n\t\t\t\t\"doc_count\":4\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"key\":2,\n\t\t\t\t\"doc_count\":2\n\t\t\t}]\n\t\t}\n\t},\n\t\"hits\":\n\t{\n\t\t\"total\":4,\n\t\t\"hits\":[]\n\t}\n}\nCODE_BLOCK_68\nquery := map[string]interface{} {};\nsearchRequest.SetQuery(query);\naggTerms := manticoreclient.NewAggregationTerms()\naggTerms.SetField(\"mva_field\")\naggTerms.SetSize(2)\naggregation := manticoreclient.NewAggregation()\naggregation.setTerms(aggTerms)\nsearchRequest.SetAggregation(aggregation)\nres, _, _ := apiClient.SearchAPI.Search(context.Background()).SearchRequest(*searchRequest).Execute()\nCODE_BLOCK_69\n{\n\t\"took\":0,\n\t\"timed_out\":false,\n\t\"aggregations\":\n\t{\n\t\t\"mva_agg\":\n\t\t{\n\t\t\t\"buckets\":\n\t\t\t[{\n\t\t\t\t\"key\":1,\n\t\t\t\t\"doc_count\":4\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"key\":2,\n\t\t\t\t\"doc_count\":2\n\t\t\t}]\n\t\t}\n\t},\n\t\"hits\":\n\t{\n\t\t\"total\":5,\n\t\t\"hits\":[]\n\t}\n}\nCODE_BLOCK_70\ncreate table products(title text, meta json);\ninsert into products values(0,'nike','{\"color\":\"red\"}'),(0,'adidas','{\"color\":\"red\"}'),(0,'puma','{\"color\":\"green\"}');\nCODE_BLOCK_71\nSELECT * FROM products;\n+---------------------+-------------------+--------+\n| id | meta | title |\n+---------------------+-------------------+--------+\n| 1657851069130080268 | {\"color\":\"red\"} | nike |\n| 1657851069130080269 | {\"color\":\"red\"} | adidas |\n| 1657851069130080270 | {\"color\":\"green\"} | puma |\n+---------------------+-------------------+--------+\nCODE_BLOCK_72\nSELECT groupby() color, count(*) from products GROUP BY meta.color;\nCODE_BLOCK_73\n+-------+----------+\n| color | count(*) |\n+-------+----------+\n| red | 2 |\n| green | 1 |\n+-------+----------+\nCODE_BLOCK_74\nPOST /search -d '\n {\n \"table\" : \"products\",\n \"limit\": 0,\n \"aggs\" :\n {\n \"color\" :\n {\n \"terms\" :\n {\n \"field\":\"meta.color\",\n \"size\":100\n }\n }\n }\n }\n'\nCODE_BLOCK_75\n{\n \"took\": 0,\n \"timed_out\": false,\n \"hits\": {\n \"total\": 3,\n \"hits\": [\n\n ]\n },\n \"aggregations\": {\n \"color\": {\n \"buckets\": [\n {\n \"key\": \"green\",\n \"doc_count\": 1\n },\n {\n \"key\": \"red\",\n \"doc_count\": 2\n }\n ]\n }\n }\n}\nCODE_BLOCK_76\n$index->setName('products');\n$search = $index->search('');\n$search->limit(0);\n$search->facet('meta.color','color',100);\n$results = $search->get();\nprint_r($results->getFacets());\nCODE_BLOCK_77\nArray\n(\n [color] => Array\n (\n [buckets] => Array\n (\n [0] => Array\n (\n [key] => green\n [doc_count] => 1\n )\n [1] => Array\n (\n [key] => red\n [doc_count] => 2\n )\n )\n )\n)\n\nCODE_BLOCK_78\nres =searchApi.search({\"table\":\"products\",\"limit\":0,\"aggs\":{\"color\":{\"terms\":{\"field\":\"meta.color\",\"size\":100}}}})\nCODE_BLOCK_79\n{'aggregations': {u'color': {u'buckets': [{u'doc_count': 1,\n u'key': u'green'},\n {u'doc_count': 2, u'key': u'red'}]}},\n 'hits': {'hits': [], 'max_score': None, 'total': 3},\n 'profile': None,\n 'timed_out': False,\n 'took': 0}\nCODE_BLOCK_80\n<!-- request Python-asyncio -->\nCODE_BLOCK_81\n<!-- response Python-asyncio -->\nCODE_BLOCK_82\n\n<!-- request Javascript -->\nCODE_BLOCK_83\n<!-- response Javascript -->\nCODE_BLOCK_84\n<!-- request Java -->\nCODE_BLOCK_85\n<!-- response Java -->\nCODE_BLOCK_86\n\n<!-- request C# -->\nCODE_BLOCK_87\n<!-- response C# -->\nCODE_BLOCK_88\n\n<!-- request Rust -->\nCODE_BLOCK_89\n<!-- response Rust -->\nCODE_BLOCK_90\n\n<!-- request TypeScript -->\nCODE_BLOCK_91\n\n<!-- response TypeScript -->\nCODE_BLOCK_92\n\n<!-- request Go -->\nCODE_BLOCK_93\n\n<!-- response Go -->\nCODE_BLOCK_94\n\n<!-- end -->",
- "translations": {
- "chinese": "<!-- example group7 -->\n##### GROUPBY()\n\u6709\u4e00\u4e2a\u51fd\u6570 `GROUPBY()` \u8fd4\u56de\u5f53\u524d\u5206\u7ec4\u7684\u952e\u3002\u5b83\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\u90fd\u5f88\u6709\u7528\uff0c\u7279\u522b\u662f\u5f53\u4f60[\u5bf9 MVA \u8fdb\u884c\u5206\u7ec4](../Searching/Grouping.md#Grouping-by-MVA-%28multi-value-attributes%29) \u6216\u8005[\u5bf9 JSON \u503c\u8fdb\u884c\u5206\u7ec4](../Searching/Grouping.md#Grouping-by-a-JSON-node)\u3002\n\n\u5b83\u4e5f\u53ef\u4ee5\u7528\u5728 `HAVING` \u4e2d\uff0c\u4f8b\u5982\uff0c\u4ec5\u4fdd\u75592000\u5e74\u548c2002\u5e74\u7684\u6570\u636e\u3002\n\n\u6ce8\u610f\uff0c\u5f53\u4f60\u4e00\u6b21\u5bf9\u591a\u4e2a\u5b57\u6bb5\u8fdb\u884c GROUP BY \u65f6\uff0c\u4e0d\u63a8\u8350\u4f7f\u7528 `GROUPBY()`\u3002\u5b83\u4ecd\u7136\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u6b64\u65f6\u5206\u7ec4\u952e\u662f\u5b57\u6bb5\u503c\u7684\u590d\u5408\u4f53\uff0c\u53ef\u80fd\u4e0d\u4f1a\u4ee5\u4f60\u671f\u671b\u7684\u65b9\u5f0f\u51fa\u73b0\u3002\n\n<!-- intro -->\n##### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_44\n<!-- response SQL -->\nCODE_BLOCK_45\n<!-- end -->\n<!-- example mva -->\n##### \u6309 MVA\uff08\u591a\u503c\u5c5e\u6027\uff09\u5206\u7ec4\nManticore \u652f\u6301\u6309[MVA](../Creating_a_table/Data_types.md#Multi-value-integer-%28MVA%29)\u8fdb\u884c\u5206\u7ec4\u3002\u4e3a\u4e86\u6f14\u793a\u5176\u5de5\u4f5c\u539f\u7406\uff0c\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a \"shoes\"\u3001\u5305\u542b MVA \u5b57\u6bb5 \"sizes\" \u7684\u8868\uff0c\u5e76\u63d2\u5165\u4e00\u4e9b\u6587\u6863\uff1a\nCODE_BLOCK_46\n\u6240\u4ee5\u6211\u4eec\u6709\uff1a\nCODE_BLOCK_47\n\u5982\u679c\u73b0\u5728\u5bf9 \"sizes\" \u8fdb\u884c GROUP BY\uff0c\u5b83\u5c06\u5904\u7406\u6211\u4eec\u6240\u6709\u7684\u591a\u503c\u5c5e\u6027\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u5927\u5c0f\u8fd4\u56de\u4e00\u6b21\u805a\u5408\uff0c\u8fd9\u91cc\u4ec5\u6709\u8ba1\u6570\uff1a\n\n<!-- intro -->\n##### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_48\n<!-- response SQL -->\nCODE_BLOCK_49\n\n<!-- request JSON -->\nCODE_BLOCK_50\n<!-- response JSON -->\nCODE_BLOCK_51\n<!-- request PHP -->\nCODE_BLOCK_52\n<!-- response PHP -->\nCODE_BLOCK_53\n<!-- request Python -->\nCODE_BLOCK_54\n<!-- response Python -->\nCODE_BLOCK_55\n<!-- request Javascript -->\nCODE_BLOCK_56\n\n<!-- request Python-asyncio -->\nCODE_BLOCK_57\n<!-- response Python-asyncio -->\nCODE_BLOCK_58\n\n<!-- request Javascript -->\nCODE_BLOCK_59\n\n<!-- response Javascript -->\nCODE_BLOCK_60\n<!-- request Java -->\nCODE_BLOCK_61\n<!-- response Java -->\nCODE_BLOCK_62\n\n<!-- request C# -->\nCODE_BLOCK_63\n<!-- response C# -->\nCODE_BLOCK_64\n\n<!-- request Rust -->\nCODE_BLOCK_65\n<!-- response Rust -->\nCODE_BLOCK_66\nres = await searchApi.search({\n index: 'test',\n aggs: {\n mva_agg: {\n terms: { field: \"mva_field\", size: 2 }\n }\n }\n});\nCODE_BLOCK_67\n{\n\t\"took\":0,\n\t\"timed_out\":false,\n\t\"aggregations\":\n\t{\n\t\t\"mva_agg\":\n\t\t{\n\t\t\t\"buckets\":\n\t\t\t[{\n\t\t\t\t\"key\":1,\n\t\t\t\t\"doc_count\":4\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"key\":2,\n\t\t\t\t\"doc_count\":2\n\t\t\t}]\n\t\t}\n\t},\n\t\"hits\":\n\t{\n\t\t\"total\":4,\n\t\t\"hits\":[]\n\t}\n}\nCODE_BLOCK_68\nquery := map[string]interface{} {};\nsearchRequest.SetQuery(query);\naggTerms := manticoreclient.NewAggregationTerms()\naggTerms.SetField(\"mva_field\")\naggTerms.SetSize(2)\naggregation := manticoreclient.NewAggregation()\naggregation.setTerms(aggTerms)\nsearchRequest.SetAggregation(aggregation)\nres, _, _ := apiClient.SearchAPI.Search(context.Background()).SearchRequest(*searchRequest).Execute()\nCODE_BLOCK_69\n{\n\t\"took\":0,\n\t\"timed_out\":false,\n\t\"aggregations\":\n\t{\n\t\t\"mva_agg\":\n\t\t{\n\t\t\t\"buckets\":\n\t\t\t[{\n\t\t\t\t\"key\":1,\n\t\t\t\t\"doc_count\":4\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"key\":2,\n\t\t\t\t\"doc_count\":2\n\t\t\t}]\n\t\t}\n\t},\n\t\"hits\":\n\t{\n\t\t\"total\":5,\n\t\t\"hits\":[]\n\t}\n}\nCODE_BLOCK_70\ncreate table products(title text, meta json);\ninsert into products values(0,'nike','{\"color\":\"red\"}'),(0,'adidas','{\"color\":\"red\"}'),(0,'puma','{\"color\":\"green\"}');\nCODE_BLOCK_71\nSELECT * FROM products;\n+---------------------+-------------------+--------+\n| id | meta | title |\n+---------------------+-------------------+--------+\n| 1657851069130080268 | {\"color\":\"red\"} | nike |\n| 1657851069130080269 | {\"color\":\"red\"} | adidas |\n| 1657851069130080270 | {\"color\":\"green\"} | puma |\n+---------------------+-------------------+--------+\nCODE_BLOCK_72\nSELECT groupby() color, count(*) from products GROUP BY meta.color;\nCODE_BLOCK_73\n+-------+----------+\n| color | count(*) |\n+-------+----------+\n| red | 2 |\n| green | 1 |\n+-------+----------+\nCODE_BLOCK_74\nPOST /search -d '\n {\n \"table\" : \"products\",\n \"limit\": 0,\n \"aggs\" :\n {\n \"color\" :\n {\n \"terms\" :\n {\n \"field\":\"meta.color\",\n \"size\":100\n }\n }\n }\n }\n'\nCODE_BLOCK_75\n{\n \"took\": 0,\n \"timed_out\": false,\n \"hits\": {\n \"total\": 3,\n \"hits\": [\n\n ]\n },\n \"aggregations\": {\n \"color\": {\n \"buckets\": [\n {\n \"key\": \"green\",\n \"doc_count\": 1\n },\n {\n \"key\": \"red\",\n \"doc_count\": 2\n }\n ]\n }\n }\n}\nCODE_BLOCK_76\n$index->setName('products');\n$search = $index->search('');\n$search->limit(0);\n$search->facet('meta.color','color',100);\n$results = $search->get();\nprint_r($results->getFacets());\nCODE_BLOCK_77\nArray\n(\n [color] => Array\n (\n [buckets] => Array\n (\n [0] => Array\n (\n [key] => green\n [doc_count] => 1\n )\n [1] => Array\n (\n [key] => red\n [doc_count] => 2\n )\n )\n )\n)\n\nCODE_BLOCK_78\nres =searchApi.search({\"table\":\"products\",\"limit\":0,\"aggs\":{\"color\":{\"terms\":{\"field\":\"meta.color\",\"size\":100}}}})\nCODE_BLOCK_79\n{'aggregations': {u'color': {u'buckets': [{u'doc_count': 1,\n u'key': u'green'},\n {u'doc_count': 2, u'key': u'red'}]}},\n 'hits': {'hits': [], 'max_score': None, 'total': 3},\n 'profile': None,\n 'timed_out': False,\n 'took': 0}\nCODE_BLOCK_80\n<!-- request Python-asyncio -->\nCODE_BLOCK_81\n<!-- response Python-asyncio -->\nCODE_BLOCK_82\n\n<!-- request Javascript -->\nCODE_BLOCK_83\n<!-- response Javascript -->\nCODE_BLOCK_84\n<!-- request Java -->\nCODE_BLOCK_85\n<!-- response Java -->\nCODE_BLOCK_86\n\n<!-- request C# -->\nCODE_BLOCK_87\n<!-- response C# -->\nCODE_BLOCK_88\n\n<!-- request Rust -->\nCODE_BLOCK_89\n<!-- response Rust -->\nCODE_BLOCK_90\n\n<!-- request TypeScript -->\nCODE_BLOCK_91\n\n<!-- response TypeScript -->\nCODE_BLOCK_92\n\n<!-- request Go -->\nCODE_BLOCK_93\n\n<!-- response Go -->\nCODE_BLOCK_94\n\n<!-- end -->",
- "russian": "<!-- example group7 -->\n##### GROUPBY()\n\u0415\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f `GROUPBY()`, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043b\u044e\u0447 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0433\u0440\u0443\u043f\u043f\u044b. \u042d\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043a\u043e\u0433\u0434\u0430 \u0432\u044b [GROUP BY \u043f\u043e MVA](../Searching/Grouping.md#Grouping-by-MVA-%28multi-value-attributes%29) \u0438\u043b\u0438 \u043f\u043e [\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e JSON](../Searching/Grouping.md#Grouping-by-a-JSON-node).\n\n\u0415\u0451 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 `HAVING`, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0433\u043e\u0434\u0430 2000 \u0438 2002.\n\n\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e `GROUPBY()` \u043d\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0435 \u043f\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u043f\u043e\u043b\u044f\u043c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e. \u041e\u043d\u0430 \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u043d\u043e \u0442\u0430\u043a \u043a\u0430\u043a \u043a\u043b\u044e\u0447 \u0433\u0440\u0443\u043f\u043f\u044b \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043e\u0441\u0442\u0430\u0432\u043d\u044b\u043c \u0438\u0437 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e\u043b\u0435\u0439, \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c\u0441\u044f \u043d\u0435 \u0442\u0430\u043a, \u043a\u0430\u043a \u0432\u044b \u043e\u0436\u0438\u0434\u0430\u0435\u0442\u0435.\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_44\n<!-- response SQL -->\nCODE_BLOCK_45\n<!-- end -->\n<!-- example mva -->\n##### \u0413\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e MVA (\u043c\u043d\u043e\u0433\u043e\u0437\u043d\u0430\u0447\u043d\u044b\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c)\nManticore \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e [MVA](../Creating_a_table/Data_types.md#Multi-value-integer-%28MVA%29). \u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \"shoes\" \u0441 MVA \"sizes\" \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u043c \u0442\u0443\u0434\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432:\nCODE_BLOCK_46\n\u0442\u0430\u043a \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c:\nCODE_BLOCK_47\n\u0415\u0441\u043b\u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c GROUP BY \"sizes\", \u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d \u0432\u0435\u0441\u044c \u043d\u0430\u0448 \u043c\u043d\u043e\u0433\u043e\u0437\u043d\u0430\u0447\u043d\u044b\u0439 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0430 \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u044f \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e, \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e:\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_48\n<!-- response SQL -->\nCODE_BLOCK_49\n\n<!-- request JSON -->\nCODE_BLOCK_50\n<!-- response JSON -->\nCODE_BLOCK_51\n<!-- request PHP -->\nCODE_BLOCK_52\n<!-- response PHP -->\nCODE_BLOCK_53\n<!-- request Python -->\nCODE_BLOCK_54\n<!-- response Python -->\nCODE_BLOCK_55\n<!-- request Javascript -->\nCODE_BLOCK_56\n\n<!-- request Python-asyncio -->\nCODE_BLOCK_57\n<!-- response Python-asyncio -->\nCODE_BLOCK_58\n\n<!-- request Javascript -->\nCODE_BLOCK_59\n\n<!-- response Javascript -->\nCODE_BLOCK_60\n<!-- request Java -->\nCODE_BLOCK_61\n<!-- response Java -->\nCODE_BLOCK_62\n\n<!-- request C# -->\nCODE_BLOCK_63\n<!-- response C# -->\nCODE_BLOCK_64\n\n<!-- request Rust -->\nCODE_BLOCK_65\n<!-- response Rust -->\nCODE_BLOCK_66\nres = await searchApi.search({\n index: 'test',\n aggs: {\n mva_agg: {\n terms: { field: \"mva_field\", size: 2 }\n }\n }\n});\nCODE_BLOCK_67\n{\n\t\"took\":0,\n\t\"timed_out\":false,\n\t\"aggregations\":\n\t{\n\t\t\"mva_agg\":\n\t\t{\n\t\t\t\"buckets\":\n\t\t\t[{\n\t\t\t\t\"key\":1,\n\t\t\t\t\"doc_count\":4\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"key\":2,\n\t\t\t\t\"doc_count\":2\n\t\t\t}]\n\t\t}\n\t},\n\t\"hits\":\n\t{\n\t\t\"total\":4,\n\t\t\"hits\":[]\n\t}\n}\nCODE_BLOCK_68\nquery := map[string]interface{} {};\nsearchRequest.SetQuery(query);\naggTerms := manticoreclient.NewAggregationTerms()\naggTerms.SetField(\"mva_field\")\naggTerms.SetSize(2)\naggregation := manticoreclient.NewAggregation()\naggregation.setTerms(aggTerms)\nsearchRequest.SetAggregation(aggregation)\nres, _, _ := apiClient.SearchAPI.Search(context.Background()).SearchRequest(*searchRequest).Execute()\nCODE_BLOCK_69\n{\n\t\"took\":0,\n\t\"timed_out\":false,\n\t\"aggregations\":\n\t{\n\t\t\"mva_agg\":\n\t\t{\n\t\t\t\"buckets\":\n\t\t\t[{\n\t\t\t\t\"key\":1,\n\t\t\t\t\"doc_count\":4\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"key\":2,\n\t\t\t\t\"doc_count\":2\n\t\t\t}]\n\t\t}\n\t},\n\t\"hits\":\n\t{\n\t\t\"total\":5,\n\t\t\"hits\":[]\n\t}\n}\nCODE_BLOCK_70\ncreate table products(title text, meta json);\ninsert into products values(0,'nike','{\"color\":\"red\"}'),(0,'adidas','{\"color\":\"red\"}'),(0,'puma','{\"color\":\"green\"}');\nCODE_BLOCK_71\nSELECT * FROM products;\n+---------------------+-------------------+--------+\n| id | meta | title |\n+---------------------+-------------------+--------+\n| 1657851069130080268 | {\"color\":\"red\"} | nike |\n| 1657851069130080269 | {\"color\":\"red\"} | adidas |\n| 1657851069130080270 | {\"color\":\"green\"} | puma |\n+---------------------+-------------------+--------+\nCODE_BLOCK_72\nSELECT groupby() color, count(*) from products GROUP BY meta.color;\nCODE_BLOCK_73\n+-------+----------+\n| color | count(*) |\n+-------+----------+\n| red | 2 |\n| green | 1 |\n+-------+----------+\nCODE_BLOCK_74\nPOST /search -d '\n {\n \"table\" : \"products\",\n \"limit\": 0,\n \"aggs\" :\n {\n \"color\" :\n {\n \"terms\" :\n {\n \"field\":\"meta.color\",\n \"size\":100\n }\n }\n }\n }\n'\nCODE_BLOCK_75\n{\n \"took\": 0,\n \"timed_out\": false,\n \"hits\": {\n \"total\": 3,\n \"hits\": [\n\n ]\n },\n \"aggregations\": {\n \"color\": {\n \"buckets\": [\n {\n \"key\": \"green\",\n \"doc_count\": 1\n },\n {\n \"key\": \"red\",\n \"doc_count\": 2\n }\n ]\n }\n }\n}\nCODE_BLOCK_76\n$index->setName('products');\n$search = $index->search('');\n$search->limit(0);\n$search->facet('meta.color','color',100);\n$results = $search->get();\nprint_r($results->getFacets());\nCODE_BLOCK_77\nArray\n(\n [color] => Array\n (\n [buckets] => Array\n (\n [0] => Array\n (\n [key] => green\n [doc_count] => 1\n )\n [1] => Array\n (\n [key] => red\n [doc_count] => 2\n )\n )\n )\n)\n\nCODE_BLOCK_78\nres =searchApi.search({\"table\":\"products\",\"limit\":0,\"aggs\":{\"color\":{\"terms\":{\"field\":\"meta.color\",\"size\":100}}}})\nCODE_BLOCK_79\n{'aggregations': {u'color': {u'buckets': [{u'doc_count': 1,\n u'key': u'green'},\n {u'doc_count': 2, u'key': u'red'}]}},\n 'hits': {'hits': [], 'max_score': None, 'total': 3},\n 'profile': None,\n 'timed_out': False,\n 'took': 0}\nCODE_BLOCK_80\n<!-- request Python-asyncio -->\nCODE_BLOCK_81\n<!-- response Python-asyncio -->\nCODE_BLOCK_82\n\n<!-- request Javascript -->\nCODE_BLOCK_83\n<!-- response Javascript -->\nCODE_BLOCK_84\n<!-- request Java -->\nCODE_BLOCK_85\n<!-- response Java -->\nCODE_BLOCK_86\n\n<!-- request C# -->\nCODE_BLOCK_87\n<!-- response C# -->\nCODE_BLOCK_88\n\n<!-- request Rust -->\nCODE_BLOCK_89\n<!-- response Rust -->\nCODE_BLOCK_90\n\n<!-- request TypeScript -->\nCODE_BLOCK_91\n\n<!-- response TypeScript -->\nCODE_BLOCK_92\n\n<!-- request Go -->\nCODE_BLOCK_93\n\n<!-- response Go -->\nCODE_BLOCK_94\n\n<!-- end -->"
- },
- "is_code_or_comment": false,
- "model": "openai:gpt-4.1-mini",
- "updated_at": 1766339824
- },
- "20a8c29b0e9d0ced6771be775faf593c52172999f63cc341c197d532f4241f38": {
- "original": "## Aggregation functions\nBesides `COUNT(*)`, which returns the number of elements in each group, you can use various other aggregation functions:\n<!-- example distinct -->\n##### COUNT(DISTINCT field)\nWhile `COUNT(*)` returns the number of all elements in the group, `COUNT(DISTINCT field)` returns the number of unique values of the field in the group, which may be completely different from the total count. For instance, you can have 100 elements in the group, but all with the same value for a certain field. `COUNT(DISTINCT field)` helps to determine that. To demonstrate this, let's create a table \"students\" with the student's name, age, and major:\nCODE_BLOCK_95\n\nso we have:\n\nCODE_BLOCK_96\n\nIn the example, you can see that if we GROUP BY major and display both `COUNT(*)` and `COUNT(DISTINCT age)`, it becomes clear that there are two students who chose the major \"cs\" with two unique ages, but for the major \"arts\", there are also two students, yet only one unique age.\n\nThere can be at most one `COUNT(DISTINCT)` per query.\n\n** By default, counts are approximate **\n\nActually, some of them are exact, while others are approximate. More on that below.\n\nManticore supports two algorithms for computing counts of distinct values. One is a legacy algorithm that uses a lot of memory and is usually slow. It collects `{group; value}` pairs, sorts them, and periodically discards duplicates. The benefit of this approach is that it guarantees exact counts within a plain table. You can enable it by setting the [distinct_precision_threshold](../Searching/Options.md#distinct_precision_threshold) option to `0`.\n\nThe other algorithm (enabled by default) loads counts into a hash table and returns its size. If the hash table becomes too large, its contents are moved into a `HyperLogLog`. This is where the counts become approximate since `HyperLogLog` is a probabilistic algorithm. The advantage is that the maximum memory usage per group is fixed and depends on the accuracy of the `HyperLogLog`. The overall memory usage also depends on the [max_matches](../Searching/Options.md#max_matches) setting, which reflects the number of groups.\n\nThe [distinct_precision_threshold](../Searching/Options.md#distinct_precision_threshold) option sets the threshold below which counts are guaranteed to be exact. The `HyperLogLog` accuracy setting and the threshold for the \"hash table to HyperLogLog\" conversion are derived from this setting. It's important to use this option with caution because doubling it will double the maximum memory required for count calculations. The maximum memory usage can be roughly estimated using this formula: `64 * max_matches * distinct_precision_threshold`. Note that this is the worst-case scenario, and in most cases, count calculations will use significantly less RAM.\n\n**`COUNT(DISTINCT)` against a distributed table or a real-time table consisting of multiple disk chunks may return inaccurate results**, but the result should be accurate for a distributed table consisting of local plain or real-time tables with the same schema (identical set/order of fields, but may have different tokenization settings).\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_97\n<!-- response SQL -->\nCODE_BLOCK_98\n<!-- end -->\n\n<!-- example concat -->\n##### GROUP_CONCAT(field)\n\nOften, you want to better understand the contents of each group. You can use [GROUP N BY](../Searching/Grouping.md#Give-me-N-rows) for that, but it would return additional rows you might not want in the output. `GROUP_CONCAT()` enriches your grouping by concatenating values of a specific field in the group. Let's take the previous example and improve it by displaying all the ages in each group.\n\n`GROUP_CONCAT(field)` returns the list as comma-separated values.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_99\n<!-- response SQL -->\nCODE_BLOCK_100\n<!-- end -->\n<!-- example sum -->\n##### SUM(), MIN(), MAX(), AVG()\nOf course, you can also obtain the sum, average, minimum, and maximum values within a group.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_101\n<!-- response SQL -->\nCODE_BLOCK_102\n<!-- end -->\n\n<!-- example accuracy -->\n## Grouping accuracy\n\nGrouping is done in fixed memory, which depends on the [max_matches](../Searching/Options.md#max_matches) setting. If `max_matches` allows for storage of all found groups, the results will be 100% accurate. However, if the value of `max_matches` is lower, the results will be less accurate.\n\nWhen parallel processing is involved, it can become more complicated. When `pseudo_sharding` is enabled and/or when using an RT table with several disk chunks, each chunk or pseudo shard gets a result set that is no larger than `max_matches`. This can lead to inaccuracies in aggregates and group counts when the result sets from different threads are merged. To fix this, either a larger `max_matches` value or disabling parallel processing can be used.\n\nManticore will try to increase `max_matches` up to [max_matches_increase_threshold](../Searching/Options.md#max_matches_increase_threshold) if it detects that groupby may return inaccurate results. Detection is based on the number of unique values of the groupby attribute, which is retrieved from secondary indexes (if present).\n\nTo ensure accurate aggregates and/or group counts when using RT tables or `pseudo_sharding`, `accurate_aggregation` can be enabled. This will try to increase `max_matches` up to the threshold, and if the threshold is not high enough, Manticore will disable parallel processing for the query.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_103\n<!-- end -->\n<!-- proofread -->\n\n",
- "translations": {
- "chinese": "## \u805a\u5408\u51fd\u6570\n\u9664\u4e86\u8fd4\u56de\u6bcf\u7ec4\u5143\u7d20\u6570\u91cf\u7684 `COUNT(*)`\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u5176\u4ed6\u805a\u5408\u51fd\u6570\uff1a\n<!-- example distinct -->\n##### COUNT(DISTINCT field)\n\u867d\u7136 `COUNT(*)` \u8fd4\u56de\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u7684\u6570\u91cf\uff0c`COUNT(DISTINCT field)` \u8fd4\u56de\u7ec4\u4e2d\u7279\u5b9a\u5b57\u6bb5\u7684\u552f\u4e00\u503c\u6570\u91cf\uff0c\u8fd9\u53ef\u80fd\u4e0e\u603b\u6570\u5b8c\u5168\u4e0d\u540c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5728\u7ec4\u4e2d\u6709100\u4e2a\u5143\u7d20\uff0c\u4f46\u67d0\u5b57\u6bb5\u7684\u503c\u5168\u90fd\u76f8\u540c\u3002`COUNT(DISTINCT field)` \u80fd\u5e2e\u52a9\u786e\u5b9a\u8fd9\u4e00\u70b9\u3002\u4e3a\u6f14\u793a\u6b64\u529f\u80fd\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a \"students\" \u7684\u8868\uff0c\u5305\u542b\u5b66\u751f\u59d3\u540d\u3001\u5e74\u9f84\u548c\u4e13\u4e1a\uff1a\nCODE_BLOCK_95\n\n\u6240\u4ee5\u6211\u4eec\u6709\uff1a\n\nCODE_BLOCK_96\n\n\u5728\u793a\u4f8b\u4e2d\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u5982\u679c\u6309 major \u5206\u7ec4\u5e76\u540c\u65f6\u663e\u793a `COUNT(*)` \u548c `COUNT(DISTINCT age)`\uff0c\u5c31\u4f1a\u6e05\u695a\u5730\u77e5\u9053\u9009\u62e9\u4e13\u4e1a\u4e3a \"cs\" \u7684\u6709\u4e24\u540d\u5b66\u751f\u4e14\u5e74\u9f84\u5404\u4e0d\u76f8\u540c\uff0c\u4f46\u5bf9\u4e8e\u4e13\u4e1a\u4e3a \"arts\" \u7684\u4e5f\u6709\u4e24\u540d\u5b66\u751f\uff0c\u5374\u53ea\u6709\u4e00\u4e2a\u552f\u4e00\u5e74\u9f84\u3002\n\n\u6bcf\u4e2a\u67e5\u8be2\u4e2d\u6700\u591a\u53ea\u80fd\u6709\u4e00\u4e2a `COUNT(DISTINCT)`\u3002\n\n** \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8ba1\u6570\u662f\u8fd1\u4f3c\u7684 **\n\n\u5b9e\u9645\u4e0a\uff0c\u6709\u4e9b\u8ba1\u6570\u662f\u7cbe\u786e\u7684\uff0c\u6709\u4e9b\u5219\u662f\u8fd1\u4f3c\u7684\u3002\u4e0b\u9762\u4f1a\u8be6\u7ec6\u8bf4\u660e\u3002\n\nManticore \u652f\u6301\u4e24\u79cd\u8ba1\u7b97\u552f\u4e00\u503c\u8ba1\u6570\u7684\u7b97\u6cd5\u3002\u4e00\u79cd\u662f\u4f7f\u7528\u5927\u91cf\u5185\u5b58\u901a\u5e38\u8f83\u6162\u7684\u4f20\u7edf\u7b97\u6cd5\uff0c\u5b83\u6536\u96c6 `{group; value}` \u5bf9\uff0c\u6392\u5e8f\u540e\u5468\u671f\u6027\u53bb\u91cd\u3002\u8be5\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4fdd\u8bc1\u5728\u666e\u901a\u8868\u4e2d\u7684\u8ba1\u6570\u7cbe\u786e\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5c06 [distinct_precision_threshold](../Searching/Options.md#distinct_precision_threshold) \u9009\u9879\u8bbe\u7f6e\u4e3a `0` \u6765\u542f\u7528\u5b83\u3002\n\n\u53e6\u4e00\u79cd\u7b97\u6cd5\uff08\u9ed8\u8ba4\u542f\u7528\uff09\u5c06\u8ba1\u6570\u52a0\u8f7d\u5230\u54c8\u5e0c\u8868\u4e2d\u5e76\u8fd4\u56de\u5176\u5927\u5c0f\u3002\u5982\u679c\u54c8\u5e0c\u8868\u8fc7\u5927\uff0c\u5176\u5185\u5bb9\u4f1a\u88ab\u8f6c\u79fb\u5230 `HyperLogLog`\u3002\u6b64\u65f6\u8ba1\u6570\u53d8\u4e3a\u8fd1\u4f3c\uff0c\u56e0\u4e3a `HyperLogLog` \u662f\u4e00\u79cd\u6982\u7387\u7b97\u6cd5\u3002\u5176\u4f18\u70b9\u662f\u6bcf\u7ec4\u6700\u5927\u5185\u5b58\u4f7f\u7528\u56fa\u5b9a\uff0c\u4f9d\u8d56\u4e8e `HyperLogLog` \u7684\u7cbe\u5ea6\u3002\u603b\u4f53\u5185\u5b58\u4f7f\u7528\u8fd8\u53d7 [max_matches](../Searching/Options.md#max_matches) \u8bbe\u7f6e\u5f71\u54cd\uff0c\u8be5\u503c\u53cd\u6620\u7ec4\u6570\u3002\n\n[distinct_precision_threshold](../Searching/Options.md#distinct_precision_threshold) \u9009\u9879\u8bbe\u5b9a\u8ba1\u6570\u4fdd\u8bc1\u51c6\u786e\u7684\u9608\u503c\u3002`HyperLogLog` \u7684\u7cbe\u5ea6\u8bbe\u7f6e\u4ee5\u53ca\u201c\u4ece\u54c8\u5e0c\u8868\u5230 HyperLogLog\u201d\u7684\u8f6c\u6362\u9608\u503c\u5747\u57fa\u4e8e\u8be5\u53c2\u6570\u3002\u4f7f\u7528\u6b64\u9009\u9879\u9700\u8c28\u614e\uff0c\u56e0\u4e3a\u5c06\u5176\u7ffb\u500d\u4f1a\u4f7f\u8ba1\u6570\u8ba1\u7b97\u6240\u9700\u6700\u5927\u5185\u5b58\u7ffb\u500d\u3002\u6700\u5927\u5185\u5b58\u4f7f\u7528\u5927\u81f4\u53ef\u7528\u516c\u5f0f\u4f30\u7b97\uff1a`64 * max_matches * distinct_precision_threshold`\u3002\u6ce8\u610f\uff0c\u8fd9\u662f\u6700\u574f\u60c5\u51b5\uff0c\u5b9e\u9645\u8ba1\u6570\u8ba1\u7b97\u901a\u5e38\u5360\u7528\u660e\u663e\u66f4\u5c11\u7684\u5185\u5b58\u3002\n\n** \u5bf9\u4e8e\u5305\u542b\u591a\u4e2a\u78c1\u76d8\u5757\u7684\u5206\u5e03\u5f0f\u8868\u6216\u5b9e\u65f6\u8868\uff0c`COUNT(DISTINCT)` \u53ef\u80fd\u8fd4\u56de\u4e0d\u51c6\u786e\u7ed3\u679c\uff0c\u4f46\u5bf9\u4e8e\u7531\u76f8\u540c\u6a21\u5f0f\uff08\u76f8\u540c\u5b57\u6bb5\u96c6/\u987a\u5e8f\u4f46\u5206\u8bcd\u8bbe\u7f6e\u53ef\u4e0d\u540c\uff09\u7684\u672c\u5730\u666e\u901a\u8868\u6216\u5b9e\u65f6\u8868\u7ec4\u6210\u7684\u5206\u5e03\u5f0f\u8868\uff0c\u7ed3\u679c\u5e94\u51c6\u786e\u3002**\n\n<!-- intro -->\n##### \u4f8b\u5b50\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_97\n<!-- response SQL -->\nCODE_BLOCK_98\n<!-- end -->\n\n<!-- example concat -->\n##### GROUP_CONCAT(field)\n\n\u4f60\u901a\u5e38\u60f3\u66f4\u597d\u5730\u4e86\u89e3\u6bcf\u4e2a\u5206\u7ec4\u7684\u5185\u5bb9\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 [GROUP N BY](../Searching/Grouping.md#Give-me-N-rows) \u5b9e\u73b0\uff0c\u4f46\u5b83\u4f1a\u8fd4\u56de\u989d\u5916\u7684\u884c\uff0c\u53ef\u80fd\u4e0d\u60f3\u51fa\u73b0\u5728\u7ed3\u679c\u4e2d\u3002`GROUP_CONCAT()` \u901a\u8fc7\u8fde\u63a5\u7ec4\u4e2d\u7279\u5b9a\u5b57\u6bb5\u7684\u6570\u503c\u6765\u4e30\u5bcc\u5206\u7ec4\u5185\u5bb9\u3002\u7ee7\u7eed\u4f7f\u7528\u524d\u9762\u7684\u4f8b\u5b50\uff0c\u8fd9\u91cc\u6539\u8fdb\u4e3a\u663e\u793a\u6bcf\u7ec4\u4e2d\u7684\u6240\u6709\u5e74\u9f84\u3002\n\n`GROUP_CONCAT(field)` \u8fd4\u56de\u9017\u53f7\u5206\u9694\u7684\u503c\u5217\u8868\u3002\n\n<!-- intro -->\n##### \u4f8b\u5b50\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_99\n<!-- response SQL -->\nCODE_BLOCK_100\n<!-- end -->\n<!-- example sum -->\n##### SUM(), MIN(), MAX(), AVG()\n\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u83b7\u53d6\u7ec4\u5185\u7684\u6c42\u548c\u3001\u5e73\u5747\u503c\u3001\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002\n\n<!-- intro -->\n##### \u4f8b\u5b50\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_101\n<!-- response SQL -->\nCODE_BLOCK_102\n<!-- end -->\n\n<!-- example accuracy -->\n## \u5206\u7ec4\u7cbe\u5ea6\n\n\u5206\u7ec4\u4f7f\u7528\u56fa\u5b9a\u5185\u5b58\uff0c\u8be5\u5185\u5b58\u5927\u5c0f\u4f9d\u8d56\u4e8e [max_matches](../Searching/Options.md#max_matches) \u8bbe\u7f6e\u3002\u5982\u679c `max_matches` \u8db3\u4ee5\u5b58\u50a8\u6240\u6709\u627e\u5230\u7684\u7ec4\uff0c\u7ed3\u679c\u5c06\u662f 100% \u7cbe\u786e\u7684\u3002\u4f46\u82e5 `max_matches` \u8f83\u5c0f\uff0c\u7ed3\u679c\u7684\u51c6\u786e\u6027\u4f1a\u964d\u4f4e\u3002\n\n\u6d89\u53ca\u5e76\u884c\u5904\u7406\u65f6\u60c5\u51b5\u4f1a\u66f4\u590d\u6742\u3002\u542f\u7528 `pseudo_sharding` \u548c/\u6216\u4f7f\u7528\u5305\u542b\u591a\u4e2a\u78c1\u76d8\u5757\u7684 RT \u8868\u65f6\uff0c\u6bcf\u4e2a\u5757\u6216\u4f2a\u5206\u7247\u5f97\u5230\u7684\u7ed3\u679c\u96c6\u5927\u5c0f\u90fd\u4e0d\u8d85\u8fc7 `max_matches`\u3002\u5f53\u4e0d\u540c\u7ebf\u7a0b\u7ed3\u679c\u96c6\u5408\u5e76\u65f6\uff0c\u805a\u5408\u548c\u5206\u7ec4\u8ba1\u6570\u53ef\u80fd\u4e0d\u51c6\u786e\u3002\u4e3a\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u66f4\u5927\u7684 `max_matches` \u503c\u6216\u7981\u7528\u5e76\u884c\u5904\u7406\u3002\n\n\u5982\u679c\u68c0\u6d4b\u5230 groupby \u53ef\u80fd\u8fd4\u56de\u4e0d\u51c6\u786e\u7ed3\u679c\uff0cManticore \u4f1a\u5c1d\u8bd5\u5c06 `max_matches` \u589e\u52a0\u5230 [max_matches_increase_threshold](../Searching/Options.md#max_matches_increase_threshold)\u3002\u68c0\u6d4b\u57fa\u4e8e\u4ece\u6b21\u7ea7\u7d22\u5f15\uff08\u5982\u6709\uff09\u68c0\u7d22\u5230\u7684 groupby \u5c5e\u6027\u7684\u552f\u4e00\u503c\u6570\u91cf\u3002\n\n\u4e3a\u786e\u4fdd\u5728\u4f7f\u7528 RT \u8868\u6216 `pseudo_sharding` \u65f6\u805a\u5408\u548c\u5206\u7ec4\u8ba1\u6570\u51c6\u786e\uff0c\u53ef\u4ee5\u542f\u7528 `accurate_aggregation`\u3002\u8fd9\u5c06\u5c1d\u8bd5\u63d0\u5347 `max_matches` \u5230\u9608\u503c\uff0c\u5982\u679c\u9608\u503c\u4e0d\u8db3\uff0cManticore \u4f1a\u7981\u7528\u67e5\u8be2\u7684\u5e76\u884c\u5904\u7406\u3002\n\n<!-- intro -->\n##### \u4f8b\u5b50\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_103\n<!-- end -->\n<!-- proofread -->",
- "russian": "## \u0410\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438\n\u041f\u043e\u043c\u0438\u043c\u043e `COUNT(*)`, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u0435, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0434\u0440\u0443\u0433\u0438\u0435 \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:\n<!-- example distinct -->\n##### COUNT(DISTINCT field)\n\u0412 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a `COUNT(*)` \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0441\u0435\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0433\u0440\u0443\u043f\u043f\u0435, `COUNT(DISTINCT field)` \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e\u043b\u044f \u0432 \u0433\u0440\u0443\u043f\u043f\u0435, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u043e\u0431\u0449\u0435\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0433\u0440\u0443\u043f\u043f\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c 100 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043d\u043e \u0443 \u0432\u0441\u0435\u0445 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044f. `COUNT(DISTINCT field)` \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u044d\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c. \u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \"students\" \u0441 \u0438\u043c\u0435\u043d\u0435\u043c \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u0430, \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u043e\u043c \u0438 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e:\nCODE_BLOCK_95\n\n\u0442\u0430\u043a \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c:\n\nCODE_BLOCK_96\n\n\u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0441\u0433\u0440\u0443\u043f\u043f\u0438\u0440\u0443\u0435\u043c \u043f\u043e major \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u043c \u043a\u0430\u043a `COUNT(*)`, \u0442\u0430\u043a \u0438 `COUNT(DISTINCT age)`, \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u044f\u0441\u043d\u043e, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u0430, \u0432\u044b\u0431\u0440\u0430\u0432\u0448\u0438\u0435 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \"cs\" \u0441 \u0434\u0432\u0443\u043c\u044f \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043c\u0438, \u0430 \u0434\u043b\u044f \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \"arts\" \u0442\u0430\u043a\u0436\u0435 \u0434\u0432\u0430 \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u0430, \u043d\u043e \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u043e\u0437\u0440\u0430\u0441\u0442.\n\n\u0412 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0439 `COUNT(DISTINCT)`.\n\n** \u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u044b \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0435\u043d\u043d\u044b\u043c\u0438 **\n\n\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437 \u043d\u0438\u0445 \u0442\u043e\u0447\u043d\u044b\u0435, \u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0451\u043d\u043d\u044b\u0435. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e\u0431 \u044d\u0442\u043e\u043c \u043d\u0438\u0436\u0435.\n\nManticore \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0434\u0432\u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0434\u043b\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u041e\u0434\u0438\u043d \u2014 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435. \u041e\u043d \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u043f\u0430\u0440\u044b `{group; value}`, \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0438\u0445 \u0438 \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u044b. \u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u043c \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u043e\u0432 \u0432 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435. \u0415\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0432 \u043e\u043f\u0446\u0438\u044e [distinct_precision_threshold](../Searching/Options.md#distinct_precision_threshold) \u0432 `0`.\n\n\u0414\u0440\u0443\u0433\u043e\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c (\u0432\u043a\u043b\u044e\u0447\u0451\u043d \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e) \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u044b \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0451 \u0440\u0430\u0437\u043c\u0435\u0440. \u0415\u0441\u043b\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u0439, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0432 `HyperLogLog`. \u0417\u0434\u0435\u0441\u044c \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u044b \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0435\u043d\u043d\u044b\u043c\u0438, \u0442\u0430\u043a \u043a\u0430\u043a `HyperLogLog` \u2014 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c. \u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u044e\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0430 \u0433\u0440\u0443\u043f\u043f\u0443 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 `HyperLogLog`. \u041e\u0431\u0449\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 [max_matches](../Searching/Options.md#max_matches), \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0449\u0435\u0439 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0433\u0440\u0443\u043f\u043f.\n\n\u041e\u043f\u0446\u0438\u044f [distinct_precision_threshold](../Searching/Options.md#distinct_precision_threshold) \u0437\u0430\u0434\u0430\u0451\u0442 \u043f\u043e\u0440\u043e\u0433, \u043d\u0438\u0436\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u044b \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e \u0442\u043e\u0447\u043d\u044b\u0435. \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 `HyperLogLog` \u0438 \u043f\u043e\u0440\u043e\u0433 \u0434\u043b\u044f \u043a\u043e\u043d\u0432\u0435\u0440\u0441\u0438\u0438 \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 `HyperLogLog` \u0432\u044b\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u0438\u0437 \u044d\u0442\u043e\u0439 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438. \u0412\u0430\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u043e\u043f\u0446\u0438\u044e \u0441 \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e, \u0442\u0430\u043a \u043a\u0430\u043a \u0435\u0451 \u0443\u0434\u0432\u043e\u0435\u043d\u0438\u0435 \u0443\u0434\u0432\u0430\u0438\u0432\u0430\u0435\u0442 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u043c\u044f\u0442\u0438, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0439 \u0434\u043b\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u043e\u0432. \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u043f\u043e \u0444\u043e\u0440\u043c\u0443\u043b\u0435: `64 * max_matches * distinct_precision_threshold`. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u044d\u0442\u043e \u0445\u0443\u0434\u0448\u0438\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439, \u0438 \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.\n\n**`COUNT(DISTINCT)` \u0434\u043b\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438\u043b\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0439 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0434\u0438\u0441\u043a\u043e\u0432\u044b\u0445 \u0447\u0430\u043d\u043a\u043e\u0432, \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043d\u0435\u0442\u043e\u0447\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b**, \u043d\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0442\u043e\u0447\u043d\u044b\u043c \u0434\u043b\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0439 \u0438\u0437 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0438\u043b\u0438 RT \u0442\u0430\u0431\u043b\u0438\u0446 \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0439 \u0441\u0445\u0435\u043c\u043e\u0439 (\u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440/\u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u043e\u043b\u0435\u0439, \u043d\u043e \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c\u0438 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u043c\u0438 \u0442\u043e\u043a\u0435\u043d\u0438\u0437\u0430\u0446\u0438\u0438).\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_97\n<!-- response SQL -->\nCODE_BLOCK_98\n<!-- end -->\n\n<!-- example concat -->\n##### GROUP_CONCAT(field)\n\n\u0427\u0430\u0441\u0442\u043e \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u043a\u0430\u0436\u0434\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u044b. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c [GROUP N BY](../Searching/Grouping.md#Give-me-N-rows), \u043d\u043e \u043e\u043d \u0432\u0435\u0440\u043d\u0451\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0432 \u0432\u044b\u0432\u043e\u0434\u0435. `GROUP_CONCAT()` \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u0442 \u0432\u0430\u0448\u0443 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0443, \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0432 \u0433\u0440\u0443\u043f\u043f\u0435. \u0412\u043e\u0437\u044c\u043c\u0451\u043c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438 \u0443\u043b\u0443\u0447\u0448\u0438\u043c \u0435\u0433\u043e, \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044f \u0432\u0441\u0435 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430 \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u0435.\n\n`GROUP_CONCAT(field)` \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u0440\u0430\u0437\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u044f\u0442\u044b\u043c\u0438.\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_99\n<!-- response SQL -->\nCODE_BLOCK_100\n<!-- end -->\n<!-- example sum -->\n##### SUM(), MIN(), MAX(), AVG()\n\u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u0432\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0443\u043c\u043c\u0443, \u0441\u0440\u0435\u0434\u043d\u0435\u0435, \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0433\u0440\u0443\u043f\u043f\u0435.\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_101\n<!-- response SQL -->\nCODE_BLOCK_102\n<!-- end -->\n\n<!-- example accuracy -->\n## \u0422\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438\n\n\u0413\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 [max_matches](../Searching/Options.md#max_matches). \u0415\u0441\u043b\u0438 `max_matches` \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432\u0441\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0435 \u0433\u0440\u0443\u043f\u043f\u044b, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0443\u0434\u0443\u0442 \u043d\u0430 100% \u0442\u043e\u0447\u043d\u044b\u043c\u0438. \u0415\u0441\u043b\u0438 \u0436\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 `max_matches` \u043c\u0435\u043d\u044c\u0448\u0435, \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0443\u0445\u0443\u0434\u0448\u0430\u0435\u0442\u0441\u044f.\n\n\u041f\u0440\u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0432\u0441\u0451 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u0435\u0435. \u0415\u0441\u043b\u0438 \u0432\u043a\u043b\u044e\u0447\u0451\u043d `pseudo_sharding` \u0438/\u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f RT \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0434\u0438\u0441\u043a\u043e\u0432\u044b\u043c\u0438 \u0447\u0430\u043d\u043a\u0430\u043c\u0438, \u043a\u0430\u0436\u0434\u044b\u0439 \u0447\u0430\u043d\u043a \u0438\u043b\u0438 \u043f\u0441\u0435\u0432\u0434\u043e-\u0448\u0430\u0440\u0434 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043d\u0430\u0431\u043e\u0440 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 `max_matches`. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043d\u0435\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044f\u043c \u0432 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u0430\u0445 \u0438 \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u0430\u0445 \u0433\u0440\u0443\u043f\u043f \u043f\u0440\u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u0427\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e, \u043c\u043e\u0436\u043d\u043e \u043b\u0438\u0431\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 `max_matches`, \u043b\u0438\u0431\u043e \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443.\n\nManticore \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c `max_matches` \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f [max_matches_increase_threshold](../Searching/Options.md#max_matches_increase_threshold), \u0435\u0441\u043b\u0438 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442, \u0447\u0442\u043e \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u0435\u0442\u043e\u0447\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b. \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u043e\u043c \u0438\u0437 \u0432\u0442\u043e\u0440\u0438\u0447\u043d\u044b\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 (\u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0435\u0441\u0442\u044c).\n\n\u0414\u043b\u044f \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u044f \u0442\u043e\u0447\u043d\u044b\u0445 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0432 \u0438/\u0438\u043b\u0438 \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u043e\u0432 \u0433\u0440\u0443\u043f\u043f \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 RT \u0442\u0430\u0431\u043b\u0438\u0446 \u0438\u043b\u0438 `pseudo_sharding` \u043c\u043e\u0436\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c `accurate_aggregation`. \u042d\u0442\u043e \u043f\u043e\u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c `max_matches` \u0434\u043e \u043f\u043e\u0440\u043e\u0433\u0430, \u0430 \u0435\u0441\u043b\u0438 \u043f\u043e\u0440\u043e\u0433 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u044b\u0441\u043e\u043a, Manticore \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0434\u043b\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430.\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_103\n<!-- end -->\n<!-- proofread -->"
- },
- "is_code_or_comment": false,
- "model": "openai:gpt-4.1-mini",
- "updated_at": 1766339820
- },
- "530d7917338ce368b1efc9bfa618e369367d321e30d78af7b79d54f7084abe06": {
- "original": "# Grouping search results\n\n<!-- example general -->\nGrouping search results is often helpful for obtaining per-group match counts or other aggregations. For example, it's useful for creating a graph illustrating the number of matching blog posts per month or grouping web search results by site or forum posts by author, etc.\n\nManticore supports the grouping of search results by single or multiple columns and computed expressions. The results can:\n\n* Be sorted within a group\n* Return more than one row per group\n* Have groups filtered\n* Have groups sorted\n* Be aggregated using the [aggregation functions](../Searching/Grouping.md#Aggregation-functions)\n\n<!-- intro -->\nThe general syntax is:\n\n<!-- request SQL -->\nGeneral syntax\nCODE_BLOCK_0\n\n<!-- request JSON -->\nJSON query format currently supports a basic grouping that can retrieve aggregate values and their count(*).\n\nCODE_BLOCK_1\n\nThe standard query output returns the result set without grouping, which can be hidden using `limit` (or `size`).\nThe aggregation requires setting a `size` for the group's result set size.\n\n<!-- end -->\n\n<!-- example group1 -->\n### Just Grouping\nGrouping is quite simple - just add \"GROUP BY smth\" to the end of your `SELECT` query. The something can be:\n\n* Any non-full-text field from the table: integer, float, string, MVA (multi-value attribute)\n* Or, if you used an alias in the `SELECT` list, you can GROUP BY it too\n\nYou can omit any [aggregation functions](../Searching/Grouping.md#Aggregation-functions) in the `SELECT` list and it will still work:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_2\n<!-- response SQL -->\nCODE_BLOCK_3\n<!-- end -->\n<!-- example group2 -->\nIn most cases, however, you'll want to obtain some aggregated data for each group, such as:\n\n* `COUNT(*)` to simply get the number of elements in each group\n* or `AVG(field)` to calculate the average value of the field within the group\n\nFor HTTP JSON requests, using a single `aggs` bucket with `limit=0` at the main query level works similarly to a SQL query with `GROUP BY` and `COUNT(*)`, providing equivalent behavior and performance.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL1 -->\nCODE_BLOCK_4\n<!-- response SQL1 -->\nCODE_BLOCK_5\n<!-- request SQL2 -->\nCODE_BLOCK_6\n<!-- response SQL2 -->\nCODE_BLOCK_7\n\n<!-- request JSON -->\nCODE_BLOCK_8\n<!-- response JSON -->\nCODE_BLOCK_9\n<!-- request PHP -->\nCODE_BLOCK_10\n<!-- response PHP -->\nCODE_BLOCK_11\n<!-- request Python -->\nCODE_BLOCK_12\n<!-- response Python -->\nCODE_BLOCK_13\n\n<!-- request Python-asyncio -->\nCODE_BLOCK_14\n<!-- response Python-asyncio -->\nCODE_BLOCK_15\n\n<!-- request Javascript -->\nCODE_BLOCK_16\n<!-- response Javascript -->\nCODE_BLOCK_17\n<!-- request Java -->\nCODE_BLOCK_18\n<!-- response Java -->\nCODE_BLOCK_19\n\n<!-- request C# -->\nCODE_BLOCK_20\n<!-- response C# -->\nCODE_BLOCK_21\n\n<!-- request Rust -->\nCODE_BLOCK_22\n<!-- response Rust -->\nCODE_BLOCK_23\n\n<!-- request TypeScript -->\nCODE_BLOCK_24\n\n<!-- response TypeScript -->\nCODE_BLOCK_25\n\n<!-- request Go -->\nCODE_BLOCK_26\n\n<!-- response Go -->\nCODE_BLOCK_27\n\n<!-- end -->\n\n<!-- example sort1 -->\n##### Sorting groups\nBy default, groups are not sorted, and the next thing you typically want to do is order them by something, like the field you're grouping by:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_28\n<!-- response SQL -->\nCODE_BLOCK_29\n<!-- end -->\n<!-- example sort2 -->\nAlternatively, you can sort by the aggregation:\n\n* by `count(*)` to display groups with the most elements first\n* by `avg(rental_rate)` to show the highest-rated movies first. Note that in the example, it's done via an alias: `avg(rental_rate)` is first mapped to `avg` in the `SELECT` list, and then we simply do `ORDER BY avg`\n\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL1 -->\nCODE_BLOCK_30\n<!-- response SQL1 -->\nCODE_BLOCK_31\n\n<!-- request SQL2 -->\nCODE_BLOCK_32\n<!-- response SQL2 -->\nCODE_BLOCK_33\n<!-- end -->\n\n<!-- example group3 -->\n##### GROUP BY multiple fields at once\nIn some cases, you might want to group not just by a single field, but by multiple fields at once, such as a movie's category and year:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_34\n<!-- response SQL -->\nCODE_BLOCK_35\n<!-- request JSON -->\nCODE_BLOCK_36\n<!-- response JSON -->\nCODE_BLOCK_37\n<!-- end -->\n\n<!-- example group4 -->\n##### Give me N rows\nSometimes it's useful to see not just a single element per group, but multiple. This can be easily achieved with the help of `GROUP N BY`. For example, in the following case, we get two movies for each year rather than just one, which a simple `GROUP BY release_year` would have returned.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_38\n<!-- response SQL -->\nCODE_BLOCK_39\n<!-- end -->\n\n<!-- example group5 -->\n##### Sorting inside a group\nAnother crucial analytics requirement is to sort elements within a group. To achieve this, use the `WITHIN GROUP ORDER BY ... {ASC|DESC}` clause. For example, let's get the highest-rated film for each year. Note that it works in parallel with just `ORDER BY`:\n\n* `WITHIN GROUP ORDER BY` sorts results **inside a group**\n* while just `GROUP BY` **sorts the groups themselves**\n\nThese two work entirely independently.\n\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_40\n<!-- response SQL -->\nCODE_BLOCK_41\n<!-- end -->\n\n<!-- example group6 -->\n##### Filter groups\n`HAVING expression` is a helpful clause for filtering groups. While `WHERE` is applied before grouping, `HAVING` works with the groups. For example, let's keep only those years when the average rental rate of the films for that year was higher than 3. We get only four years:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_42\n<!-- response SQL -->\nCODE_BLOCK_43\n<!-- end -->\n\n**Note:** The `total_found` value in [search query meta info](../Node_info_and_management/SHOW_META.md#SHOW-META) reflects the number of groups that match the `HAVING` condition. This enables proper pagination when using `HAVING` clauses with `GROUP BY`.",
- "translations": {
- "chinese": "# \u5206\u7ec4\u641c\u7d22\u7ed3\u679c\n\n<!-- example general -->\n\u5206\u7ec4\u641c\u7d22\u7ed3\u679c\u901a\u5e38\u6709\u52a9\u4e8e\u83b7\u53d6\u6bcf\u4e2a\u7ec4\u7684\u5339\u914d\u8ba1\u6570\u6216\u5176\u4ed6\u805a\u5408\u3002\u4f8b\u5982\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u521b\u5efa\u663e\u793a\u6bcf\u6708\u5339\u914d\u535a\u5ba2\u6587\u7ae0\u6570\u91cf\u7684\u56fe\u8868\uff0c\u6216\u6309\u7f51\u7ad9\u5206\u7ec4\u7f51\u9875\u641c\u7d22\u7ed3\u679c\uff0c\u6309\u4f5c\u8005\u5206\u7ec4\u8bba\u575b\u5e16\u5b50\u7b49\u3002\n\nManticore \u652f\u6301\u901a\u8fc7\u5355\u5217\u6216\u591a\u5217\u4ee5\u53ca\u8ba1\u7b97\u8868\u8fbe\u5f0f\u5bf9\u641c\u7d22\u7ed3\u679c\u8fdb\u884c\u5206\u7ec4\u3002\u7ed3\u679c\u53ef\u4ee5\uff1a\n\n* \u5728\u7ec4\u5185\u6392\u5e8f\n* \u6bcf\u7ec4\u8fd4\u56de\u591a\u884c\n* \u5bf9\u7ec4\u8fdb\u884c\u8fc7\u6ee4\n* \u5bf9\u7ec4\u8fdb\u884c\u6392\u5e8f\n* \u4f7f\u7528[\u805a\u5408\u51fd\u6570](../Searching/Grouping.md#Aggregation-functions)\u8fdb\u884c\u805a\u5408\n\n<!-- intro -->\n\u901a\u7528\u8bed\u6cd5\u4e3a\uff1a\n\n<!-- request SQL -->\nGeneral syntax\nCODE_BLOCK_0\n\n<!-- request JSON -->\nJSON \u67e5\u8be2\u683c\u5f0f\u76ee\u524d\u652f\u6301\u57fa\u672c\u5206\u7ec4\uff0c\u53ef\u4ee5\u68c0\u7d22\u805a\u5408\u503c\u53ca\u5176 count(*)\u3002\n\nCODE_BLOCK_1\n\n\u6807\u51c6\u67e5\u8be2\u8f93\u51fa\u8fd4\u56de\u672a\u5206\u7ec4\u7684\u7ed3\u679c\u96c6\uff0c\u53ef\u4ee5\u4f7f\u7528 `limit`\uff08\u6216 `size`\uff09\u9690\u85cf\u3002\n\u805a\u5408\u9700\u8981\u8bbe\u7f6e\u7ec4\u7ed3\u679c\u96c6\u7684 `size`\u3002\n\n<!-- end -->\n\n<!-- example group1 -->\n### \u4ec5\u5206\u7ec4\n\u5206\u7ec4\u975e\u5e38\u7b80\u5355\u2014\u2014\u53ea\u9700\u5728 `SELECT` \u67e5\u8be2\u672b\u5c3e\u6dfb\u52a0 \"GROUP BY smth\"\u3002\u5176\u4e2d\u67d0\u7269\u53ef\u4ee5\u662f\uff1a\n\n* \u8868\u4e2d\u7684\u4efb\u4f55\u975e\u5168\u6587\u5b57\u6bb5\uff1a\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001\u591a\u503c\u5c5e\u6027\uff08MVA\uff09\n* \u6216\u8005\uff0c\u5982\u679c\u4f60\u5728 `SELECT` \u5217\u8868\u4e2d\u4f7f\u7528\u4e86\u522b\u540d\uff0c\u4e5f\u53ef\u4ee5\u6309\u522b\u540d\u5206\u7ec4\n\n\u4f60\u53ef\u4ee5\u7701\u7565 `SELECT` \u5217\u8868\u4e2d\u7684\u4efb\u4f55[\u805a\u5408\u51fd\u6570](../Searching/Grouping.md#Aggregation-functions)\uff0c\u8be5\u67e5\u8be2\u4ecd\u7136\u6709\u6548\uff1a\n\n<!-- intro -->\n##### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_2\n<!-- response SQL -->\nCODE_BLOCK_3\n<!-- end -->\n<!-- example group2 -->\n\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u4f1a\u60f3\u4e3a\u6bcf\u4e2a\u5206\u7ec4\u83b7\u5f97\u4e00\u4e9b\u805a\u5408\u6570\u636e\uff0c\u5982\uff1a\n\n* `COUNT(*)`\uff0c\u4ec5\u83b7\u53d6\u6bcf\u7ec4\u4e2d\u7684\u5143\u7d20\u6570\u91cf\n* \u6216 `AVG(field)`\uff0c\u8ba1\u7b97\u8be5\u7ec4\u5185\u5b57\u6bb5\u7684\u5e73\u5747\u503c\n\n\u5bf9\u4e8e HTTP JSON \u8bf7\u6c42\uff0c\u4f7f\u7528\u4e00\u4e2a\u4e3b\u67e5\u8be2\u7ea7\u522b\u7684\u5355\u4e2a `aggs` \u6876\u5e76\u8bbe\u7f6e `limit=0`\uff0c\u5176\u884c\u4e3a\u7c7b\u4f3c\u4e8e\u5e26\u6709 `GROUP BY` \u548c `COUNT(*)` \u7684 SQL \u67e5\u8be2\uff0c\u63d0\u4f9b\u7b49\u6548\u7684\u884c\u4e3a\u548c\u6027\u80fd\u3002\n\n<!-- intro -->\n##### \u793a\u4f8b\uff1a\n\n<!-- request SQL1 -->\nCODE_BLOCK_4\n<!-- response SQL1 -->\nCODE_BLOCK_5\n<!-- request SQL2 -->\nCODE_BLOCK_6\n<!-- response SQL2 -->\nCODE_BLOCK_7\n\n<!-- request JSON -->\nCODE_BLOCK_8\n<!-- response JSON -->\nCODE_BLOCK_9\n<!-- request PHP -->\nCODE_BLOCK_10\n<!-- response PHP -->\nCODE_BLOCK_11\n<!-- request Python -->\nCODE_BLOCK_12\n<!-- response Python -->\nCODE_BLOCK_13\n\n<!-- request Python-asyncio -->\nCODE_BLOCK_14\n<!-- response Python-asyncio -->\nCODE_BLOCK_15\n\n<!-- request Javascript -->\nCODE_BLOCK_16\n<!-- response Javascript -->\nCODE_BLOCK_17\n<!-- request Java -->\nCODE_BLOCK_18\n<!-- response Java -->\nCODE_BLOCK_19\n\n<!-- request C# -->\nCODE_BLOCK_20\n<!-- response C# -->\nCODE_BLOCK_21\n\n<!-- request Rust -->\nCODE_BLOCK_22\n<!-- response Rust -->\nCODE_BLOCK_23\n\n<!-- request TypeScript -->\nCODE_BLOCK_24\n\n<!-- response TypeScript -->\nCODE_BLOCK_25\n\n<!-- request Go -->\nCODE_BLOCK_26\n\n<!-- response Go -->\nCODE_BLOCK_27\n\n<!-- end -->\n\n<!-- example sort1 -->\n##### \u5206\u7ec4\u6392\u5e8f\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7ec4\u4e0d\u6392\u5e8f\uff0c\u901a\u5e38\u4e0b\u4e00\u6b65\u4f60\u4f1a\u60f3\u6309\u67d0\u4e9b\u5b57\u6bb5\u6392\u5e8f\uff0c\u6bd4\u5982\u4f60\u5206\u7ec4\u6240\u7528\u7684\u5b57\u6bb5\uff1a\n\n<!-- intro -->\n##### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_28\n<!-- response SQL -->\nCODE_BLOCK_29\n<!-- end -->\n<!-- example sort2 -->\n\u6216\u8005\uff0c\u4f60\u53ef\u4ee5\u6309\u805a\u5408\u7ed3\u679c\u6392\u5e8f\uff1a\n\n* \u6309 `count(*)` \u6392\u5e8f\uff0c\u4ee5\u663e\u793a\u5143\u7d20\u6700\u591a\u7684\u7ec4\u4f18\u5148\n* \u6309 `avg(rental_rate)` \u6392\u5e8f\uff0c\u4ee5\u663e\u793a\u8bc4\u5206\u6700\u9ad8\u7684\u7535\u5f71\u4f18\u5148\u3002\u6ce8\u610f\u793a\u4f8b\u4e2d\uff0c\u8fd9\u901a\u8fc7\u522b\u540d\u5b8c\u6210\uff1a`avg(rental_rate)` \u9996\u5148\u6620\u5c04\u4e3a `avg`\uff0c\u7136\u540e\u7528 `ORDER BY avg`\n\n<!-- intro -->\n##### \u793a\u4f8b\uff1a\n\n<!-- request SQL1 -->\nCODE_BLOCK_30\n<!-- response SQL1 -->\nCODE_BLOCK_31\n\n<!-- request SQL2 -->\nCODE_BLOCK_32\n<!-- response SQL2 -->\nCODE_BLOCK_33\n<!-- end -->\n\n<!-- example group3 -->\n##### \u540c\u65f6\u6309\u591a\u4e2a\u5b57\u6bb5\u5206\u7ec4\n\u6709\u65f6\u4f60\u53ef\u80fd\u5e0c\u671b\u4e0d\u53ea\u6309\u5355\u4e2a\u5b57\u6bb5\u5206\u7ec4\uff0c\u800c\u662f\u540c\u65f6\u6309\u591a\u4e2a\u5b57\u6bb5\u5206\u7ec4\uff0c\u4f8b\u5982\u7535\u5f71\u7684\u7c7b\u522b\u548c\u5e74\u4efd\uff1a\n\n<!-- intro -->\n##### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_34\n<!-- response SQL -->\nCODE_BLOCK_35\n<!-- request JSON -->\nCODE_BLOCK_36\n<!-- response JSON -->\nCODE_BLOCK_37\n<!-- end -->\n\n<!-- example group4 -->\n##### \u83b7\u53d6\u6bcf\u7ec4\u7684 N \u884c\n\u6709\u65f6\u4e0d\u4ec5\u4ec5\u60f3\u770b\u6bcf\u7ec4\u7684\u4e00\u6761\u8bb0\u5f55\uff0c\u800c\u662f\u591a\u6761\u3002\u4f7f\u7528 `GROUP N BY` \u5f88\u5bb9\u6613\u5b9e\u73b0\u3002\u4f8b\u5982\u4e0b\u9762\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u4e3a\u6bcf\u4e2a\u5e74\u4efd\u83b7\u53d6\u4e24\u90e8\u7535\u5f71\uff0c\u800c\u4e0d\u662f\u7b80\u5355\u7684 `GROUP BY release_year` \u53ea\u8fd4\u56de\u4e00\u90e8\u3002\n\n<!-- intro -->\n##### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_38\n<!-- response SQL -->\nCODE_BLOCK_39\n<!-- end -->\n\n<!-- example group5 -->\n##### \u7ec4\u5185\u6392\u5e8f\n\u53e6\u4e00\u4e2a\u5173\u952e\u7684\u5206\u6790\u9700\u6c42\u662f\u5bf9\u7ec4\u5185\u5143\u7d20\u6392\u5e8f\u3002\u8981\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u8bf7\u4f7f\u7528 `WITHIN GROUP ORDER BY ... {ASC|DESC}` \u5b50\u53e5\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u6bcf\u5e74\u8bc4\u5206\u6700\u9ad8\u7684\u7535\u5f71\u3002\u6ce8\u610f\u8fd9\u4e0e\u5355\u7eaf\u7684 `ORDER BY` \u5e76\u884c\u8fd0\u884c\uff1a\n\n* `WITHIN GROUP ORDER BY` \u5bf9**\u7ec4\u5185**\u7684\u7ed3\u679c\u6392\u5e8f\n* \u4ec5\u7528 `GROUP BY` \u5219\u5bf9**\u7ec4\u672c\u8eab**\u6392\u5e8f\n\n\u8fd9\u4e24\u8005\u5b8c\u5168\u72ec\u7acb\u3002\n\n<!-- intro -->\n##### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_40\n<!-- response SQL -->\nCODE_BLOCK_41\n<!-- end -->\n\n<!-- example group6 -->\n##### \u8fc7\u6ee4\u7ec4\n`HAVING expression` \u662f\u8fc7\u6ee4\u7ec4\u7684\u6709\u7528\u5b50\u53e5\u3002`WHERE` \u662f\u5728\u5206\u7ec4\u524d\u4f7f\u7528\uff0c\u800c `HAVING` \u9488\u5bf9\u5206\u7ec4\u540e\u7684\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u4fdd\u7559\u53ea\u6709\u90a3\u4e9b\u5e74\u4efd\u4e2d\uff0c\u5e73\u5747\u79df\u8d41\u7387\u9ad8\u4e8e 3 \u7684\u5e74\u4efd\u3002\u53ea\u5f97\u52304\u4e2a\u5e74\u4efd\uff1a\n\n<!-- intro -->\n##### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_42\n<!-- response SQL -->\nCODE_BLOCK_43\n<!-- end -->\n\n**\u6ce8\u610f\uff1a** [\u641c\u7d22\u67e5\u8be2\u5143\u4fe1\u606f](../Node_info_and_management/SHOW_META.md#SHOW-META)\u4e2d\u7684 `total_found` \u503c\u53cd\u6620\u5339\u914d `HAVING` \u6761\u4ef6\u7684\u7ec4\u6570\u3002\u8fd9\u4f7f\u5f97\u4f7f\u7528\u5e26 `HAVING` \u7684 `GROUP BY` \u8bed\u53e5\u65f6\uff0c\u5206\u9875\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002",
- "russian": "# \u0413\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043f\u043e\u0438\u0441\u043a\u0430\n\n<!-- example general -->\n\u0413\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043f\u043e\u0438\u0441\u043a\u0430 \u0447\u0430\u0441\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439 \u043f\u043e \u0433\u0440\u0443\u043f\u043f\u0430\u043c \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u044d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0438\u043a\u0430, \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0445 \u0431\u043b\u043e\u0433-\u043f\u043e\u0441\u0442\u043e\u0432 \u043f\u043e \u043c\u0435\u0441\u044f\u0446\u0430\u043c, \u043b\u0438\u0431\u043e \u0434\u043b\u044f \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0432\u0435\u0431-\u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e \u0441\u0430\u0439\u0442\u0430\u043c \u0438\u043b\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u043d\u0430 \u0444\u043e\u0440\u0443\u043c\u0435 \u043f\u043e \u0430\u0432\u0442\u043e\u0440\u0430\u043c \u0438 \u0442. \u0434.\n\nManticore \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0443 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0438\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c \u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u043c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043c\u043e\u0433\u0443\u0442:\n\n* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0433\u0440\u0443\u043f\u043f\u044b\n* \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430 \u0433\u0440\u0443\u043f\u043f\u0443\n* \u0418\u043c\u0435\u0442\u044c \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0433\u0440\u0443\u043f\u043f\u044b\n* \u0418\u043c\u0435\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0433\u0440\u0443\u043f\u043f\u044b\n* \u0410\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e [\u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439](../Searching/Grouping.md#Aggregation-functions)\n\n<!-- intro -->\n\u041e\u0431\u0449\u0438\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441:\n\n<!-- request SQL -->\n\u041e\u0431\u0449\u0438\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\nCODE_BLOCK_0\n\n<!-- request JSON -->\n\u0424\u043e\u0440\u043c\u0430\u0442 JSON-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0443, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0443\u044e \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u0438\u0445 count(*).\n\nCODE_BLOCK_1\n\n\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0430\u0431\u043e\u0440 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0431\u0435\u0437 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0440\u044b\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e `limit` (\u0438\u043b\u0438 `size`).\n\u0414\u043b\u044f \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u0434\u0430\u0442\u044c `size` \u2014 \u0440\u0430\u0437\u043c\u0435\u0440 \u043d\u0430\u0431\u043e\u0440\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0433\u0440\u0443\u043f\u043f\u044b.\n\n<!-- end -->\n\n<!-- example group1 -->\n### \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430\n\u0413\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430 \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \"GROUP BY smth\" \u0432 \u043a\u043e\u043d\u0435\u0446 \u0432\u0430\u0448\u0435\u0433\u043e `SELECT` \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0427\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f:\n\n* \u041b\u044e\u0431\u043e\u0435 \u043f\u043e\u043b\u0435, \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0449\u0435\u0435\u0441\u044f \u043f\u043e\u043b\u043d\u043e\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u043c, \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b: integer, float, string, MVA (\u043c\u0443\u043b\u044c\u0442\u0438\u0437\u043d\u0430\u0447\u043d\u044b\u0439 \u0430\u0442\u0440\u0438\u0431\u0443\u0442)\n* \u0418\u043b\u0438, \u0435\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043f\u0441\u0435\u0432\u0434\u043e\u043d\u0438\u043c \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 `SELECT`, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0442\u0430\u043a\u0436\u0435 GROUP BY \u043f\u043e \u043d\u0435\u043c\u0443\n\n\u041c\u043e\u0436\u043d\u043e \u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043b\u044e\u0431\u044b\u0435 [\u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438](../Searching/Grouping.md#Aggregation-functions) \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 `SELECT`, \u0438 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c:\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_2\n<!-- response SQL -->\nCODE_BLOCK_3\n<!-- end -->\n<!-- example group2 -->\n\u041e\u0434\u043d\u0430\u043a\u043e \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:\n\n* `COUNT(*)` \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u0435\n* \u0438\u043b\u0438 `AVG(field)`, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0440\u0435\u0434\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0433\u0440\u0443\u043f\u043f\u044b\n\n\u0414\u043b\u044f HTTP JSON-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0433\u043e `aggs` \u0431\u0430\u043a\u0435\u0442\u0430 \u0441 `limit=0` \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u0441 `GROUP BY` \u0438 `COUNT(*)`, \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u044f \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c.\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL1 -->\nCODE_BLOCK_4\n<!-- response SQL1 -->\nCODE_BLOCK_5\n<!-- request SQL2 -->\nCODE_BLOCK_6\n<!-- response SQL2 -->\nCODE_BLOCK_7\n\n<!-- request JSON -->\nCODE_BLOCK_8\n<!-- response JSON -->\nCODE_BLOCK_9\n<!-- request PHP -->\nCODE_BLOCK_10\n<!-- response PHP -->\nCODE_BLOCK_11\n<!-- request Python -->\nCODE_BLOCK_12\n<!-- response Python -->\nCODE_BLOCK_13\n\n<!-- request Python-asyncio -->\nCODE_BLOCK_14\n<!-- response Python-asyncio -->\nCODE_BLOCK_15\n\n<!-- request Javascript -->\nCODE_BLOCK_16\n<!-- response Javascript -->\nCODE_BLOCK_17\n<!-- request Java -->\nCODE_BLOCK_18\n<!-- response Java -->\nCODE_BLOCK_19\n\n<!-- request C# -->\nCODE_BLOCK_20\n<!-- response C# -->\nCODE_BLOCK_21\n\n<!-- request Rust -->\nCODE_BLOCK_22\n<!-- response Rust -->\nCODE_BLOCK_23\n\n<!-- request TypeScript -->\nCODE_BLOCK_24\n\n<!-- response TypeScript -->\nCODE_BLOCK_25\n\n<!-- request Go -->\nCODE_BLOCK_26\n\n<!-- response Go -->\nCODE_BLOCK_27\n\n<!-- end -->\n\n<!-- example sort1 -->\n##### \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0433\u0440\u0443\u043f\u043f\n\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0433\u0440\u0443\u043f\u043f\u044b \u043d\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f, \u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0448\u0430\u0433\u043e\u043c \u043e\u0431\u044b\u0447\u043d\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u043f\u043e \u0447\u0435\u043c\u0443-\u0442\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e \u043f\u043e\u043b\u044e, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430:\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_28\n<!-- response SQL -->\nCODE_BLOCK_29\n<!-- end -->\n<!-- example sort2 -->\n\u041b\u0438\u0431\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u0443:\n\n* \u043f\u043e `count(*)`, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u0433\u0440\u0443\u043f\u043f\u044b \u0441 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n* \u043f\u043e `avg(rental_rate)`, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0444\u0438\u043b\u044c\u043c\u044b \u0441 \u0441\u0430\u043c\u043e\u0439 \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u043e\u0446\u0435\u043d\u043a\u043e\u0439. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u043f\u0441\u0435\u0432\u0434\u043e\u043d\u0438\u043c: `avg(rental_rate)` \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f \u0432 `avg` \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 `SELECT`, \u0430 \u0437\u0430\u0442\u0435\u043c \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u043c `ORDER BY avg`\n\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL1 -->\nCODE_BLOCK_30\n<!-- response SQL1 -->\nCODE_BLOCK_31\n\n<!-- request SQL2 -->\nCODE_BLOCK_32\n<!-- response SQL2 -->\nCODE_BLOCK_33\n<!-- end -->\n\n<!-- example group3 -->\n##### GROUP BY \u043f\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u043f\u043e\u043b\u044f\u043c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\n\u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0435 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043f\u043e\u043b\u044e, \u0430 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438 \u0444\u0438\u043b\u044c\u043c\u0430 \u0438 \u0433\u043e\u0434\u0443:\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_34\n<!-- response SQL -->\nCODE_BLOCK_35\n<!-- request JSON -->\nCODE_BLOCK_36\n<!-- response JSON -->\nCODE_BLOCK_37\n<!-- end -->\n\n<!-- example group4 -->\n##### \u0414\u0430\u0439\u0442\u0435 \u043c\u043d\u0435 N \u0441\u0442\u0440\u043e\u043a\n\u0418\u043d\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0435 \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0443\u043f\u043f\u0443, \u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e. \u042d\u0442\u043e \u043b\u0435\u0433\u043a\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e `GROUP N BY`. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0432\u0430 \u0444\u0438\u043b\u044c\u043c\u0430 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0433\u043e\u0434 \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0434\u043d\u043e\u0433\u043e, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 `GROUP BY release_year` \u0432\u0435\u0440\u043d\u0443\u043b \u0431\u044b.\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_38\n<!-- response SQL -->\nCODE_BLOCK_39\n<!-- end -->\n\n<!-- example group5 -->\n##### \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u0433\u0440\u0443\u043f\u043f\u044b\n\u0415\u0449\u0435 \u043e\u0434\u043d\u043e \u0432\u0430\u0436\u043d\u043e\u0435 \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435 \u2014 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u043d\u0443\u0442\u0440\u0438 \u0433\u0440\u0443\u043f\u043f\u044b. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f `WITHIN GROUP ORDER BY ... {ASC|DESC}`. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u0430\u043c\u044b\u0439 \u0432\u044b\u0441\u043e\u043a\u043e\u043e\u0446\u0435\u043d\u0435\u043d\u043d\u044b\u0439 \u0444\u0438\u043b\u044c\u043c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0433\u043e\u0434\u0430. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u0441 \u043e\u0431\u044b\u0447\u043d\u044b\u043c `ORDER BY`:\n\n* `WITHIN GROUP ORDER BY` \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b **\u0432\u043d\u0443\u0442\u0440\u0438 \u0433\u0440\u0443\u043f\u043f\u044b**\n* \u0430 \u043f\u0440\u043e\u0441\u0442\u043e `GROUP BY` **\u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0441\u0430\u043c\u0438 \u0433\u0440\u0443\u043f\u043f\u044b**\n\n\u042d\u0442\u0438 \u0434\u0432\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e.\n\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_40\n<!-- response SQL -->\nCODE_BLOCK_41\n<!-- end -->\n\n<!-- example group6 -->\n##### \u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u0433\u0440\u0443\u043f\u043f\n`HAVING expression` \u2014 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u0433\u0440\u0443\u043f\u043f. \u0412 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a `WHERE` \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0434\u043e \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438, `HAVING` \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0443\u0436\u0435 \u0441\u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u0441\u0442\u0430\u0432\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435 \u0433\u043e\u0434\u044b, \u043a\u043e\u0433\u0434\u0430 \u0441\u0440\u0435\u0434\u043d\u0438\u0439 \u0440\u0435\u0439\u0442\u0438\u043d\u0433 \u043f\u0440\u043e\u043a\u0430\u0442\u0430 \u0444\u0438\u043b\u044c\u043c\u043e\u0432 \u0432 \u044d\u0442\u043e\u043c \u0433\u043e\u0434\u0443 \u0431\u044b\u043b \u0432\u044b\u0448\u0435 3. \u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u0441\u0435\u0433\u043e \u0447\u0435\u0442\u044b\u0440\u0435 \u0433\u043e\u0434\u0430:\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_42\n<!-- response SQL -->\nCODE_BLOCK_43\n<!-- end -->\n\n**\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435:** \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 `total_found` \u0432 [\u043c\u0435\u0442\u0430-\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430](../Node_info_and_management/SHOW_META.md#SHOW-META) \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0433\u0440\u0443\u043f\u043f, \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0438\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044e `HAVING`. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044e \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 `HAVING` \u0441 `GROUP BY`."
- },
- "is_code_or_comment": false,
- "model": "openai:gpt-4.1-mini",
- "updated_at": 1766339822
- },
- "ec6a623f472ccaaf3d3b64a0a4c133f055fae62b91b463426b2f1bfc03b52f63": {
- "original": "# Grouping search results\n\n<!-- example general -->\nGrouping search results is often helpful for obtaining per-group match counts or other aggregations. For example, it's useful for creating a graph illustrating the number of matching blog posts per month or grouping web search results by site or forum posts by author, etc.\n\nManticore supports the grouping of search results by single or multiple columns and computed expressions. The results can:\n\n* Be sorted within a group\n* Return more than one row per group\n* Have groups filtered\n* Have groups sorted\n* Be aggregated using the [aggregation functions](../Searching/Grouping.md#Aggregation-functions)\n\n<!-- intro -->\nThe general syntax is:\n\n<!-- request SQL -->\nGeneral syntax\nCODE_BLOCK_0\n\n<!-- request JSON -->\nJSON query format currently supports a basic grouping that can retrieve aggregate values and their count(*).\n\nCODE_BLOCK_1\n\nThe standard query output returns the result set without grouping, which can be hidden using `limit` (or `size`).\nThe aggregation requires setting a `size` for the group's result set size.\n\n<!-- end -->\n\n<!-- example group1 -->\n### Just Grouping\nGrouping is quite simple - just add \"GROUP BY smth\" to the end of your `SELECT` query. The something can be:\n\n* Any non-full-text field from the table: integer, float, string, MVA (multi-value attribute)\n* Or, if you used an alias in the `SELECT` list, you can GROUP BY it too\n\nYou can omit any [aggregation functions](../Searching/Grouping.md#Aggregation-functions) in the `SELECT` list and it will still work:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_2\n<!-- response SQL -->\nCODE_BLOCK_3\n<!-- end -->\n<!-- example group2 -->\nIn most cases, however, you'll want to obtain some aggregated data for each group, such as:\n\n* `COUNT(*)` to simply get the number of elements in each group\n* or `AVG(field)` to calculate the average value of the field within the group\n\nFor HTTP JSON requests, using a single `aggs` bucket with `limit=0` at the main query level works similarly to a SQL query with `GROUP BY` and `COUNT(*)`, providing equivalent behavior and performance.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL1 -->\nCODE_BLOCK_4\n<!-- response SQL1 -->\nCODE_BLOCK_5\n<!-- request SQL2 -->\nCODE_BLOCK_6\n<!-- response SQL2 -->\nCODE_BLOCK_7\n\n<!-- request JSON -->\nCODE_BLOCK_8\n<!-- response JSON -->\nCODE_BLOCK_9\n<!-- request PHP -->\nCODE_BLOCK_10\n<!-- response PHP -->\nCODE_BLOCK_11\n<!-- request Python -->\nCODE_BLOCK_12\n<!-- response Python -->\nCODE_BLOCK_13\n\n<!-- request Python-asyncio -->\nCODE_BLOCK_14\n<!-- response Python-asyncio -->\nCODE_BLOCK_15\n\n<!-- request Javascript -->\nCODE_BLOCK_16\n<!-- response Javascript -->\nCODE_BLOCK_17\n<!-- request Java -->\nCODE_BLOCK_18\n<!-- response Java -->\nCODE_BLOCK_19\n\n<!-- request C# -->\nCODE_BLOCK_20\n<!-- response C# -->\nCODE_BLOCK_21\n\n<!-- request Rust -->\nCODE_BLOCK_22\n<!-- response Rust -->\nCODE_BLOCK_23\n\n<!-- request TypeScript -->\nCODE_BLOCK_24\n\n<!-- response TypeScript -->\nCODE_BLOCK_25\n\n<!-- request Go -->\nCODE_BLOCK_26\n\n<!-- response Go -->\nCODE_BLOCK_27\n\n<!-- end -->\n\n<!-- example sort1 -->\n##### Sorting groups\nBy default, groups are not sorted, and the next thing you typically want to do is order them by something, like the field you're grouping by:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_28\n<!-- response SQL -->\nCODE_BLOCK_29\n<!-- end -->\n<!-- example sort2 -->\nAlternatively, you can sort by the aggregation:\n\n* by `count(*)` to display groups with the most elements first\n* by `avg(rental_rate)` to show the highest-rated movies first. Note that in the example, it's done via an alias: `avg(rental_rate)` is first mapped to `avg` in the `SELECT` list, and then we simply do `ORDER BY avg`\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL1 -->\nCODE_BLOCK_30\n<!-- response SQL1 -->\nCODE_BLOCK_31\n\n<!-- request SQL2 -->\nCODE_BLOCK_32\n<!-- response SQL2 -->\nCODE_BLOCK_33\n<!-- end -->\n\n<!-- example group3 -->\n##### GROUP BY multiple fields at once\nIn some cases, you might want to group not just by a single field, but by multiple fields at once, such as a movie's category and year:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_34\n<!-- response SQL -->\nCODE_BLOCK_35\n<!-- request JSON -->\nCODE_BLOCK_36\n<!-- response JSON -->\nCODE_BLOCK_37\n<!-- end -->\n\n<!-- example group4 -->\n##### Give me N rows\nSometimes it's useful to see not just a single element per group, but multiple. This can be easily achieved with the help of `GROUP N BY`. For example, in the following case, we get two movies for each year rather than just one, which a simple `GROUP BY release_year` would have returned.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_38\n<!-- response SQL -->\nCODE_BLOCK_39\n<!-- end -->\n\n<!-- example group5 -->\n##### Sorting inside a group\nAnother crucial analytics requirement is to sort elements within a group. To achieve this, use the `WITHIN GROUP ORDER BY ... {ASC|DESC}` clause. For example, let's get the highest-rated film for each year. Note that it works in parallel with just `ORDER BY`:\n\n* `WITHIN GROUP ORDER BY` sorts results **inside a group**\n* while just `GROUP BY` **sorts the groups themselves**\n\nThese two work entirely independently.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_40\n<!-- response SQL -->\nCODE_BLOCK_41\n<!-- end -->\n\n<!-- example group6 -->\n##### Filter groups\n`HAVING expression` is a helpful clause for filtering groups. While `WHERE` is applied before grouping, `HAVING` works with the groups. For example, let's keep only those years when the average rental rate of the films for that year was higher than 3. We get only four years:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\nCODE_BLOCK_42\n<!-- response SQL -->\nCODE_BLOCK_43\n<!-- end -->\n\n**Note:** The `total_found` value in [search query meta info](../Node_info_and_management/SHOW_META.md#SHOW-META) reflects the number of groups that match the `HAVING` condition. This enables proper pagination when using `HAVING` clauses with `GROUP BY`.",
- "translations": {
- "chinese": "# \u5206\u7ec4\u641c\u7d22\u7ed3\u679c\n\n<!-- example general -->\n\u5206\u7ec4\u641c\u7d22\u7ed3\u679c\u901a\u5e38\u6709\u52a9\u4e8e\u83b7\u53d6\u6bcf\u4e2a\u7ec4\u7684\u5339\u914d\u8ba1\u6570\u6216\u5176\u4ed6\u805a\u5408\u3002\u4f8b\u5982\uff0c\u5b83\u5bf9\u4e8e\u521b\u5efa\u4e00\u4e2a\u56fe\u8868\u6765\u8bf4\u660e\u6bcf\u4e2a\u6708\u5339\u914d\u7684\u535a\u5ba2\u6587\u7ae0\u6570\u91cf\u6216\u6309\u7ad9\u70b9\u5206\u7ec4\u7f51\u9875\u641c\u7d22\u7ed3\u679c\u6216\u6309\u4f5c\u8005\u5206\u7ec4\u8bba\u575b\u5e16\u5b50\u7b49\u975e\u5e38\u6709\u7528\u3002\n\nManticore \u652f\u6301\u6309\u5355\u4e2a\u6216\u591a\u4e2a\u5217\u548c\u8ba1\u7b97\u8868\u8fbe\u5f0f\u5bf9\u641c\u7d22\u7ed3\u679c\u8fdb\u884c\u5206\u7ec4\u3002\u7ed3\u679c\u53ef\u4ee5\uff1a\n\n* \u5728\u7ec4\u5185\u6392\u5e8f\n* \u6bcf\u4e2a\u7ec4\u8fd4\u56de\u591a\u4e8e\u4e00\u884c\n* \u8fc7\u6ee4\u7ec4\n* \u5bf9\u7ec4\u8fdb\u884c\u6392\u5e8f\n* \u4f7f\u7528[\u805a\u5408\u51fd\u6570](../Searching/Grouping.md#Aggregation-functions)\u8fdb\u884c\u805a\u5408\n\n<!-- intro -->\n\u4e00\u822c\u7684\u8bed\u6cd5\u662f\uff1a\n\n<!-- request SQL -->\n\u901a\u7528\u8bed\u6cd5\nCODE_BLOCK_0\n\n<!-- request JSON -->\nJSON \u67e5\u8be2\u683c\u5f0f\u76ee\u524d\u652f\u6301\u57fa\u672c\u7684\u5206\u7ec4\uff0c\u53ef\u4ee5\u68c0\u7d22\u805a\u5408\u503c\u53ca\u5176 count(*)\u3002\n\nCODE_BLOCK_1\n\n\u6807\u51c6\u67e5\u8be2\u8f93\u51fa\u8fd4\u56de\u672a\u5206\u7ec4\u7684\u7ed3\u679c\u96c6\uff0c\u53ef\u4ee5\u4f7f\u7528 `limit`\uff08\u6216 `size`\uff09\u9690\u85cf\u3002\n\u805a\u5408\u9700\u8981\u4e3a\u7ec4\u7684\u7ed3\u679c\u96c6\u5927\u5c0f\u8bbe\u7f6e `size`\u3002\n\n<!-- end -->\n\n<!-- example group1 -->\n### \u53ea\u5206\u7ec4\n\u5206\u7ec4\u975e\u5e38\u7b80\u5355 - \u53ea\u9700\u5728 `SELECT` \u67e5\u8be2\u7684\u672b\u5c3e\u6dfb\u52a0 \"GROUP BY smth\"\u3002smth \u53ef\u4ee5\u662f\uff1a\n\n* \u8868\u4e2d\u7684\u4efb\u4f55\u975e\u5168\u6587\u5b57\u6bb5\uff1a\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u3001MVA\uff08\u591a\u503c\u5c5e\u6027\uff09\n* \u6216\u8005\u5982\u679c\u4f60\u5728 `SELECT` \u5217\u8868\u4e2d\u4f7f\u7528\u4e86\u522b\u540d\uff0c\u4f60\u4e5f\u53ef\u4ee5\u6309\u5b83\u5206\u7ec4\n\n\u5728 `SELECT` \u5217\u8868\u4e2d\u53ef\u4ee5\u7701\u7565\u4efb\u4f55 [\u805a\u5408\u51fd\u6570](../Searching/Grouping.md#Aggregation-functions)\uff0c\u5b83\u4ecd\u7136\u4f1a\u5de5\u4f5c\uff1a\n\n<!-- intro -->\n#### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_2\n<!-- response SQL -->\nCODE_BLOCK_3\n<!-- end -->\n<!-- example group2 -->\n\u7136\u800c\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u4e3a\u6bcf\u4e2a\u7ec4\u83b7\u53d6\u4e00\u4e9b\u805a\u5408\u6570\u636e\uff0c\u4f8b\u5982\uff1a\n\n* `COUNT(*)` \u4ee5\u7b80\u5355\u5730\u83b7\u53d6\u6bcf\u4e2a\u7ec4\u4e2d\u7684\u5143\u7d20\u6570\u91cf\n* \u6216 `AVG(field)` \u8ba1\u7b97\u7ec4\u5185\u5b57\u6bb5\u7684\u5e73\u5747\u503c\n\n\u5bf9\u4e8e HTTP JSON \u8bf7\u6c42\uff0c\u4f7f\u7528\u4e3b\u67e5\u8be2\u7ea7\u522b\u7684\u5355\u4e2a `aggs` \u6876\u5e76\u8bbe\u7f6e `limit=0` \u4e0e SQL \u67e5\u8be2\u4e2d\u7684 `GROUP BY` \u548c `COUNT(*)` \u5177\u6709\u7c7b\u4f3c\u7684\u884c\u4e3a\u548c\u6027\u80fd\u3002\n\n<!-- intro -->\n#### \u793a\u4f8b\uff1a\n\n<!-- request SQL1 -->\nCODE_BLOCK_4\n<!-- response SQL1 -->\nCODE_BLOCK_5\n<!-- request SQL2 -->\nCODE_BLOCK_6\n<!-- response SQL2 -->\nCODE_BLOCK_7\n\n<!-- request JSON -->\nCODE_BLOCK_8\n<!-- response JSON -->\nCODE_BLOCK_9\n<!-- request PHP -->\nCODE_BLOCK_10\n<!-- response PHP -->\nCODE_BLOCK_11\n<!-- request Python -->\nCODE_BLOCK_12\n<!-- response Python -->\nCODE_BLOCK_13\n\n<!-- request Python-asyncio -->\nCODE_BLOCK_14\n<!-- response Python-asyncio -->\nCODE_BLOCK_15\n\n<!-- request Javascript -->\nCODE_BLOCK_16\n<!-- response Javascript -->\nCODE_BLOCK_17\n<!-- request Java -->\nCODE_BLOCK_18\n<!-- response Java -->\nCODE_BLOCK_19\n\n<!-- request C# -->\nCODE_BLOCK_20\n<!-- response C# -->\nCODE_BLOCK_21\n\n<!-- request Rust -->\nCODE_BLOCK_22\n<!-- response Rust -->\nCODE_BLOCK_23\n\n<!-- request TypeScript -->\nCODE_BLOCK_24\n\n<!-- response TypeScript -->\nCODE_BLOCK_25\n\n<!-- request Go -->\nCODE_BLOCK_26\n\n<!-- response Go -->\nCODE_BLOCK_27\n\n<!-- end -->\n\n<!-- example sort1 -->\n#### \u6309\u7ec4\u6392\u5e8f\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7ec4\u4e0d\u4f1a\u6392\u5e8f\uff0c\u901a\u5e38\u63a5\u4e0b\u6765\u8981\u505a\u7684\u5c31\u662f\u6309\u67d0\u4e9b\u5185\u5bb9\u5bf9\u4ed6\u4eec\u8fdb\u884c\u6392\u5e8f\uff0c\u6bd4\u5982\u4f60\u6b63\u5728\u5206\u7ec4\u7684\u5b57\u6bb5\uff1a\n\n<!-- intro -->\n#### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_28\n<!-- response SQL -->\nCODE_BLOCK_29\n<!-- end -->\n<!-- example sort2 -->\n\u6216\u8005\uff0c\u4f60\u53ef\u4ee5\u6309\u805a\u5408\u8fdb\u884c\u6392\u5e8f\uff1a\n\n* \u6309 `count(*)` \u663e\u793a\u5177\u6709\u6700\u591a\u5143\u7d20\u7684\u7ec4\u9996\u5148\n* \u6309 `avg(rental_rate)` \u5c55\u793a\u8bc4\u5206\u6700\u9ad8\u7684\u7535\u5f71\u9996\u5148\u3002\u6ce8\u610f\uff0c\u5728\u793a\u4f8b\u4e2d\uff0c\u5b83\u662f\u901a\u8fc7\u522b\u540d\u5b8c\u6210\u7684\uff1a`avg(rental_rate)` \u9996\u5148\u6620\u5c04\u5230 `avg` \u5728 `SELECT` \u5217\u8868\u4e2d\uff0c\u7136\u540e\u6211\u4eec\u53ea\u9700\u505a `ORDER BY avg`\n\n<!-- intro -->\n#### \u793a\u4f8b\uff1a\n\n<!-- request SQL1 -->\nCODE_BLOCK_30\n<!-- response SQL1 -->\nCODE_BLOCK_31\n\n<!-- request SQL2 -->\nCODE_BLOCK_32\n<!-- response SQL2 -->\nCODE_BLOCK_33\n<!-- end -->\n\n<!-- example group3 -->\n#### \u540c\u65f6\u6309\u591a\u4e2a\u5b57\u6bb5\u5206\u7ec4\n\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u4e0d\u4ec5\u60f3\u6309\u5355\u4e2a\u5b57\u6bb5\u5206\u7ec4\uff0c\u8fd8\u60f3\u540c\u65f6\u6309\u591a\u4e2a\u5b57\u6bb5\u5206\u7ec4\uff0c\u4f8b\u5982\u7535\u5f71\u7684\u7c7b\u522b\u548c\u5e74\u4efd\uff1a\n\n<!-- intro -->\n#### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_34\n<!-- response SQL -->\nCODE_BLOCK_35\n<!-- request JSON -->\nCODE_BLOCK_36\n<!-- response JSON -->\nCODE_BLOCK_37\n<!-- end -->\n\n<!-- example group4 -->\n#### \u7ed9\u6211 N \u884c\n\u6709\u65f6\u67e5\u770b\u6bcf\u4e2a\u7ec4\u7684\u4e0d\u6b62\u4e00\u884c\u662f\u6709\u7528\u7684\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7 `GROUP N BY` \u8f85\u52a9\u8f7b\u677e\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5c06\u4e3a\u6bcf\u4e00\u5e74\u83b7\u53d6\u4e24\u90e8\u7535\u5f71\u800c\u4e0d\u662f\u53ea\u6709\u4e00\u90e8\uff0c\u800c\u7b80\u5355\u7684 `GROUP BY release_year` \u5c06\u8fd4\u56de\u3002\n\n<!-- intro -->\n#### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_38\n<!-- response SQL -->\nCODE_BLOCK_39\n<!-- end -->\n\n<!-- example group5 -->\n#### \u7ec4\u5185\u7684\u6392\u5e8f\n\u53e6\u4e00\u4e2a\u5173\u952e\u7684\u5206\u6790\u9700\u6c42\u662f\u5728\u7ec4\u5185\u5bf9\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002\u8981\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u8bf7\u4f7f\u7528 `WITHIN GROUP ORDER BY ... {ASC|DESC}` \u5b50\u53e5\u3002\u4f8b\u5982\uff0c\u8ba9\u6211\u4eec\u83b7\u53d6\u6bcf\u4e00\u5e74\u8bc4\u5206\u6700\u9ad8\u7684\u7535\u5f71\u3002\u8bf7\u6ce8\u610f\uff0c\u5b83\u4e0e\u4ec5 `ORDER BY` \u5e76\u884c\u5de5\u4f5c\uff1a\n\n* `WITHIN GROUP ORDER BY` \u5bf9\u7ec4\u5185\u7684\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\n* \u800c\u4ec5 `GROUP BY` \u5bf9\u7ec4\u672c\u8eab\u8fdb\u884c\u6392\u5e8f\n\n\u8fd9\u4e24\u4e2a\u5b8c\u5168\u72ec\u7acb\u5730\u5de5\u4f5c\u3002\n\n<!-- intro -->\n#### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_40\n<!-- response SQL -->\nCODE_BLOCK_41\n<!-- end -->\n\n<!-- example group6 -->\n#### \u8fc7\u6ee4\u7ec4\n`HAVING expression` \u662f\u4e00\u4e2a\u6709\u7528\u7684\u5b50\u53e5\uff0c\u7528\u4e8e\u8fc7\u6ee4\u7ec4\u3002\u867d\u7136 `WHERE` \u5728\u5206\u7ec4\u4e4b\u524d\u5e94\u7528\uff0c\u4f46 `HAVING` \u4f5c\u7528\u4e8e\u7ec4\u3002\u4f8b\u5982\uff0c\u8ba9\u6211\u4eec\u4fdd\u7559\u90a3\u4e9b\u5f53\u5e74\u7535\u5f71\u5e73\u5747\u79df\u8d41\u4ef7\u683c\u9ad8\u4e8e 3 \u7684\u5e74\u4efd\u3002\u6211\u4eec\u53ea\u5f97\u5230\u56db\u4e2a\u5e74\u4efd\uff1a\n\n<!-- intro -->\n#### \u793a\u4f8b\uff1a\n\n<!-- request SQL -->\nCODE_BLOCK_42\n<!-- response SQL -->\nCODE_BLOCK_43\n<!-- end -->\n\n**\u6ce8\u610f\uff1a** \u5728 [\u641c\u7d22\u67e5\u8be2\u5143\u4fe1\u606f](../Node_info_and_management/SHOW_META.md#SHOW-META) \u4e2d\uff0c`total_found` \u503c\u53cd\u6620\u4e86\u6ee1\u8db3 `HAVING` \u6761\u4ef6\u7684\u7ec4\u7684\u6570\u91cf\u3002\u5f53\u4f7f\u7528 `HAVING` \u5b50\u53e5\u4e0e `GROUP BY` \u7ed3\u5408\u65f6\uff0c\u8fd9\u4f7f\u5f97\u5206\u9875\u66f4\u52a0\u51c6\u786e\u3002",
- "russian": "# \u0413\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043f\u043e\u0438\u0441\u043a\u0430\n\n<!-- example general -->\n\u0413\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043f\u043e\u0438\u0441\u043a\u0430 \u0447\u0430\u0441\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439 \u043f\u043e \u0433\u0440\u0443\u043f\u043f\u0430\u043c \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u0439. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u043d\u0430 \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0438\u043a\u0430, \u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043f\u043e\u0441\u0442\u043e\u0432 \u0432 \u0431\u043b\u043e\u0433\u0435 \u0437\u0430 \u043c\u0435\u0441\u044f\u0446, \u0438\u043b\u0438 \u0434\u043b\u044f \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0432\u0435\u0431-\u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e \u0441\u0430\u0439\u0442\u0443 \u0438\u043b\u0438 \u043f\u043e\u0441\u0442\u043e\u0432 \u043d\u0430 \u0444\u043e\u0440\u0443\u043c\u0435 \u043f\u043e \u0430\u0432\u0442\u043e\u0440\u0443 \u0438 \u0442.\u0434.\n\nManticore \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0443 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0438\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c \u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u043c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043c\u043e\u0433\u0443\u0442:\n\n* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0433\u0440\u0443\u043f\u043f\u044b\n* \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430 \u0433\u0440\u0443\u043f\u043f\u0443\n* \u0418\u043c\u0435\u0442\u044c \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e \u0433\u0440\u0443\u043f\u043f\n* \u0418\u043c\u0435\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0433\u0440\u0443\u043f\u043f\n* \u0410\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c [\u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439](../Searching/Grouping.md#Aggregation-functions)\n\n<!-- intro -->\n\u041e\u0431\u0449\u0438\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441:\n\n<!-- request SQL -->\n\u041e\u0431\u0449\u0438\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\nCODE_BLOCK_0\n\n<!-- request JSON -->\n\u0424\u043e\u0440\u043c\u0430\u0442 JSON-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0436\u0435\u0442 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u0438\u0445 count(*).\n\nCODE_BLOCK_1\n\n\u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0430\u0431\u043e\u0440 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0431\u0435\u0437 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0440\u044b\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e `limit` (\u0438\u043b\u0438 `size`).\n\u0414\u043b\u044f \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c `size` \u0434\u043b\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0433\u0440\u0443\u043f\u043f\u044b.\n\n<!-- end -->\n\n<!-- example group1 -->\n### \u041f\u0440\u043e\u0441\u0442\u043e \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430\n\u0413\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u0430 - \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \"GROUP BY \u0447\u0442\u043e-\u0442\u043e\" \u0432 \u043a\u043e\u043d\u0435\u0446 \u0432\u0430\u0448\u0435\u0433\u043e `SELECT` \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u042d\u0442\u0438\u043c \"\u0447\u0442\u043e-\u0442\u043e\" \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c:\n\n* \u041b\u044e\u0431\u043e\u0435 \u043d\u0435 \u043f\u043e\u043b\u043d\u043e\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435 \u0438\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b: \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0435, \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0442\u043e\u0447\u043a\u043e\u0439, \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0435, MVA (\u043c\u043d\u043e\u0433\u043e\u0437\u043d\u0430\u0447\u043d\u044b\u0439 \u0430\u0442\u0440\u0438\u0431\u0443\u0442)\n* \u0418\u043b\u0438, \u0435\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043f\u0441\u0435\u0432\u0434\u043e\u043d\u0438\u043c \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 `SELECT`, \u0432\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u043d\u0435\u043c\u0443\n\n\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043b\u044e\u0431\u044b\u0435 [\u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438](../Searching/Grouping.md#Aggregation-functions) \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 `SELECT`, \u0438 \u044d\u0442\u043e \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c:\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_2\n<!-- response SQL -->\nCODE_BLOCK_3\n<!-- end -->\n<!-- example group2 -->\n\u041e\u0434\u043d\u0430\u043a\u043e \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u044b, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a:\n\n* `COUNT(*)`, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u0435\n* \u0438\u043b\u0438 `AVG(field)`, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0440\u0435\u0434\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f \u0432 \u0433\u0440\u0443\u043f\u043f\u0435\n\n\u0414\u043b\u044f HTTP JSON-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430 `aggs` \u0441 `limit=0` \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u0441 `GROUP BY` \u0438 `COUNT(*)`, \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u044f \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c.\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL1 -->\nCODE_BLOCK_4\n<!-- response SQL1 -->\nCODE_BLOCK_5\n<!-- request SQL2 -->\nCODE_BLOCK_6\n<!-- response SQL2 -->\nCODE_BLOCK_7\n\n<!-- request JSON -->\nCODE_BLOCK_8\n<!-- response JSON -->\nCODE_BLOCK_9\n<!-- request PHP -->\nCODE_BLOCK_10\n<!-- response PHP -->\nCODE_BLOCK_11\n<!-- request Python -->\nCODE_BLOCK_12\n<!-- response Python -->\nCODE_BLOCK_13\n\n<!-- request Python-asyncio -->\nCODE_BLOCK_14\n<!-- response Python-asyncio -->\nCODE_BLOCK_15\n\n<!-- request Javascript -->\nCODE_BLOCK_16\n<!-- response Javascript -->\nCODE_BLOCK_17\n<!-- request Java -->\nCODE_BLOCK_18\n<!-- response Java -->\nCODE_BLOCK_19\n\n<!-- request C# -->\nCODE_BLOCK_20\n<!-- response C# -->\nCODE_BLOCK_21\n\n<!-- request Rust -->\nCODE_BLOCK_22\n<!-- response Rust -->\nCODE_BLOCK_23\n\n<!-- request TypeScript -->\nCODE_BLOCK_24\n\n<!-- response TypeScript -->\nCODE_BLOCK_25\n\n<!-- request Go -->\nCODE_BLOCK_26\n\n<!-- response Go -->\nCODE_BLOCK_27\n\n<!-- end -->\n\n<!-- example sort1 -->\n##### \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0433\u0440\u0443\u043f\u043f\n\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0433\u0440\u0443\u043f\u043f\u044b \u043d\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f, \u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435, \u0447\u0442\u043e \u0432\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u0445\u043e\u0442\u0438\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c, - \u044d\u0442\u043e \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0442\u044c \u0438\u0445 \u043f\u043e \u043a\u0430\u043a\u043e\u043c\u0443-\u043b\u0438\u0431\u043e \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e \u043f\u043e\u043b\u044e, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u0432\u044b \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u0443\u0435\u0442\u0435:\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_28\n<!-- response SQL -->\nCODE_BLOCK_29\n<!-- end -->\n<!-- example sort2 -->\n\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u044b, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u0438:\n\n* \u043f\u043e `count(*)`, \u0447\u0442\u043e\u0431\u044b \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u0433\u0440\u0443\u043f\u043f\u044b \u0441 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u0435\u0440\u0432\u044b\u043c\u0438\n* \u043f\u043e `avg(rental_rate)`, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0444\u0438\u043b\u044c\u043c\u044b \u0441 \u043d\u0430\u0438\u0432\u044b\u0441\u0448\u0438\u043c \u0440\u0435\u0439\u0442\u0438\u043d\u0433\u043e\u043c \u043f\u0435\u0440\u0432\u044b\u043c\u0438. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u043f\u0441\u0435\u0432\u0434\u043e\u043d\u0438\u043c: `avg(rental_rate)` \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f \u043d\u0430 `avg` \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 `SELECT`, \u0430 \u0437\u0430\u0442\u0435\u043c \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u043c `ORDER BY avg`\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL1 -->\nCODE_BLOCK_30\n<!-- response SQL1 -->\nCODE_BLOCK_31\n\n<!-- request SQL2 -->\nCODE_BLOCK_32\n<!-- response SQL2 -->\nCODE_BLOCK_33\n<!-- end -->\n\n<!-- example group3 -->\n##### GROUP BY \u043f\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u043f\u043e\u043b\u044f\u043c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\n\u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u0445\u043e\u0442\u0435\u0442\u044c \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0435 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043f\u043e\u043b\u044e, \u0430 \u043f\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u043f\u043e\u043b\u044f\u043c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438 \u0444\u0438\u043b\u044c\u043c\u0430 \u0438 \u0433\u043e\u0434\u0443:\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_34\n<!-- response SQL -->\nCODE_BLOCK_35\n<!-- request JSON -->\nCODE_BLOCK_36\n<!-- response JSON -->\nCODE_BLOCK_37\n<!-- end -->\n\n<!-- example group4 -->\n##### \u0414\u0430\u0439\u0442\u0435 \u043c\u043d\u0435 N \u0441\u0442\u0440\u043e\u043a\n\u0418\u043d\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0435 \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0443\u043f\u043f\u0443, \u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e. \u042d\u0442\u043e \u043b\u0435\u0433\u043a\u043e \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e `GROUP N BY`. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0432\u0430 \u0444\u0438\u043b\u044c\u043c\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0433\u043e\u0434\u0430, \u0430 \u043d\u0435 \u043e\u0434\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0435\u0440\u043d\u0443\u043b \u0431\u044b \u043f\u0440\u043e\u0441\u0442\u043e\u0439 `GROUP BY release_year`.\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_38\n<!-- response SQL -->\nCODE_BLOCK_39\n<!-- end -->\n\n<!-- example group5 -->\n##### \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u0433\u0440\u0443\u043f\u043f\u044b\n\u0415\u0449\u0435 \u043e\u0434\u043d\u043e \u0432\u0430\u0436\u043d\u043e\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0435 \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0438 - \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043d\u0443\u0442\u0440\u0438 \u0433\u0440\u0443\u043f\u043f\u044b. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435 `WITHIN GROUP ORDER BY ... {ASC|DESC}`. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0444\u0438\u043b\u044c\u043c \u0441 \u043d\u0430\u0438\u0432\u044b\u0441\u0448\u0438\u043c \u0440\u0435\u0439\u0442\u0438\u043d\u0433\u043e\u043c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0433\u043e\u0434\u0430. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c `ORDER BY`:\n\n* `WITHIN GROUP ORDER BY` \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b **\u0432\u043d\u0443\u0442\u0440\u0438 \u0433\u0440\u0443\u043f\u043f\u044b**\n* \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e `GROUP BY` **\u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0441\u0430\u043c\u0438 \u0433\u0440\u0443\u043f\u043f\u044b**\n\n\u042d\u0442\u0438 \u0434\u0432\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e.\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_40\n<!-- response SQL -->\nCODE_BLOCK_41\n<!-- end -->\n\n<!-- example group6 -->\n##### \u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u0433\u0440\u0443\u043f\u043f\n`HAVING expression` - \u044d\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u0438 \u0433\u0440\u0443\u043f\u043f. \u0412 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a `WHERE` \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0434\u043e \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438, `HAVING` \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0433\u0440\u0443\u043f\u043f\u0430\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0441\u0442\u0430\u0432\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435 \u0433\u043e\u0434\u044b, \u043a\u043e\u0433\u0434\u0430 \u0441\u0440\u0435\u0434\u043d\u044f\u044f \u0430\u0440\u0435\u043d\u0434\u043d\u0430\u044f \u0441\u0442\u0430\u0432\u043a\u0430 \u0444\u0438\u043b\u044c\u043c\u043e\u0432 \u0437\u0430 \u044d\u0442\u043e\u0442 \u0433\u043e\u0434 \u0431\u044b\u043b\u0430 \u0432\u044b\u0448\u0435 3. \u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0435\u0442\u044b\u0440\u0435 \u0433\u043e\u0434\u0430:\n\n<!-- intro -->\n##### \u041f\u0440\u0438\u043c\u0435\u0440:\n\n<!-- request SQL -->\nCODE_BLOCK_42\n<!-- response SQL -->\nCODE_BLOCK_43\n<!-- end -->\n\n**\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435:** \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 `total_found` \u0432 [\u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430](../Node_info_and_management/SHOW_META.md#SHOW-META) \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0433\u0440\u0443\u043f\u043f, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044e `HAVING`. \u042d\u0442\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u044e \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0439 `HAVING` \u0441 `GROUP BY`."
- },
- "is_code_or_comment": false,
- "model": "deepseek/deepseek-v3.2",
- "updated_at": 1766374274
- },
- "__meta": {
- "source_text": "# Grouping search results\n\n<!-- example general -->\nGrouping search results is often helpful for obtaining per-group match counts or other aggregations. For example, it's useful for creating a graph illustrating the number of matching blog posts per month or grouping web search results by site or forum posts by author, etc.\n\nManticore supports the grouping of search results by single or multiple columns and computed expressions. The results can:\n\n* Be sorted within a group\n* Return more than one row per group\n* Have groups filtered\n* Have groups sorted\n* Be aggregated using the [aggregation functions](../Searching/Grouping.md#Aggregation-functions)\n\n<!-- intro -->\nThe general syntax is:\n\n<!-- request SQL -->\nGeneral syntax\n```sql\nSELECT {* | SELECT_expr [, SELECT_expr ...]}\n...\nGROUP BY {field_name | alias } [, ...]\n[HAVING where_condition]\n[WITHIN GROUP ORDER BY field_name {ASC | DESC} [, ...]]\n...\n\nSELECT_expr: { field_name | function_name(...) }\nwhere_condition: {aggregation expression alias | COUNT(*)}\n```\n\n<!-- request JSON -->\nJSON query format currently supports a basic grouping that can retrieve aggregate values and their count(*).\n\n```json\n{\n \"table\": \"<table_name>\",\n \"limit\": 0,\n \"aggs\": {\n \"<aggr_name>\": {\n \"terms\": {\n \"field\": \"<attribute>\",\n \"size\": <int value>\n }\n }\n }\n}\n```\n\nThe standard query output returns the result set without grouping, which can be hidden using `limit` (or `size`).\nThe aggregation requires setting a `size` for the group's result set size.\n\n<!-- end -->\n\n<!-- example group1 -->\n### Just Grouping\nGrouping is quite simple - just add \"GROUP BY smth\" to the end of your `SELECT` query. The something can be:\n\n* Any non-full-text field from the table: integer, float, string, MVA (multi-value attribute)\n* Or, if you used an alias in the `SELECT` list, you can GROUP BY it too\n\nYou can omit any [aggregation functions](../Searching/Grouping.md#Aggregation-functions) in the `SELECT` list and it will still work:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\n```sql\nSELECT release_year FROM films GROUP BY release_year LIMIT 5;\n```\n<!-- response SQL -->\n```sql\n+--------------+\n| release_year |\n+--------------+\n| 2004 |\n| 2002 |\n| 2001 |\n| 2005 |\n| 2000 |\n+--------------+\n```\n<!-- end -->\n<!-- example group2 -->\nIn most cases, however, you'll want to obtain some aggregated data for each group, such as:\n\n* `COUNT(*)` to simply get the number of elements in each group\n* or `AVG(field)` to calculate the average value of the field within the group\n\nFor HTTP JSON requests, using a single `aggs` bucket with `limit=0` at the main query level works similarly to a SQL query with `GROUP BY` and `COUNT(*)`, providing equivalent behavior and performance.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL1 -->\n```sql\nSELECT release_year, count(*) FROM films GROUP BY release_year LIMIT 5;\n```\n<!-- response SQL1 -->\n```sql\n+--------------+----------+\n| release_year | count(*) |\n+--------------+----------+\n| 2004 | 108 |\n| 2002 | 108 |\n| 2001 | 91 |\n| 2005 | 93 |\n| 2000 | 97 |\n+--------------+----------+\n```\n<!-- request SQL2 -->\n```sql\nSELECT release_year, AVG(rental_rate) FROM films GROUP BY release_year LIMIT 5;\n```\n<!-- response SQL2 -->\n```sql\n+--------------+------------------+\n| release_year | avg(rental_rate) |\n+--------------+------------------+\n| 2004 | 2.78629661 |\n| 2002 | 3.08259249 |\n| 2001 | 3.09989142 |\n| 2005 | 2.90397978 |\n| 2000 | 3.17556739 |\n+--------------+------------------+\n```\n\n<!-- request JSON -->\n``` json\nPOST /search -d '\n {\n \"table\" : \"films\",\n \"limit\": 0,\n \"aggs\" :\n {\n \"release_year\" :\n {\n \"terms\" :\n {\n \"field\":\"release_year\",\n \"size\":100\n }\n }\n }\n }\n'\n```\n<!-- response JSON -->\n``` json\n{\n \"took\": 2,\n \"timed_out\": false,\n \"hits\": {\n \"total\": 10000,\n \"hits\": [\n\n ]\n },\n \"release_year\": {\n \"group_brand_id\": {\n \"buckets\": [\n {\n \"key\": 2004,\n \"doc_count\": 108\n },\n {\n \"key\": 2002,\n \"doc_count\": 108\n },\n {\n \"key\": 2000,\n \"doc_count\": 97\n },\n {\n \"key\": 2005,\n \"doc_count\": 93\n },\n {\n \"key\": 2001,\n \"doc_count\": 91\n }\n ]\n }\n }\n}\n```\n<!-- request PHP -->\n``` php\n$index->setName('films');\n$search = $index->search('');\n$search->limit(0);\n$search->facet('release_year','release_year',100);\n$results = $search->get();\nprint_r($results->getFacets());\n```\n<!-- response PHP -->\n``` php\nArray\n(\n [release_year] => Array\n (\n [buckets] => Array\n (\n [0] => Array\n (\n [key] => 2009\n [doc_count] => 99\n )\n [1] => Array\n (\n [key] => 2008\n [doc_count] => 102\n )\n [2] => Array\n (\n [key] => 2007\n [doc_count] => 93\n )\n [3] => Array\n (\n [key] => 2006\n [doc_count] => 103\n )\n [4] => Array\n (\n [key] => 2005\n [doc_count] => 93\n )\n [5] => Array\n (\n [key] => 2004\n [doc_count] => 108\n )\n [6] => Array\n (\n [key] => 2003\n [doc_count] => 106\n )\n [7] => Array\n (\n [key] => 2002\n [doc_count] => 108\n )\n [8] => Array\n (\n [key] => 2001\n [doc_count] => 91\n )\n [9] => Array\n (\n [key] => 2000\n [doc_count] => 97\n )\n )\n )\n)\n```\n<!-- request Python -->\n``` python\nres =searchApi.search({\"table\":\"films\",\"limit\":0,\"aggs\":{\"release_year\":{\"terms\":{\"field\":\"release_year\",\"size\":100}}}})\n```\n<!-- response Python -->\n``` python\n{'aggregations': {u'release_year': {u'buckets': [{u'doc_count': 99,\n u'key': 2009},\n {u'doc_count': 102,\n u'key': 2008},\n {u'doc_count': 93,\n u'key': 2007},\n {u'doc_count': 103,\n u'key': 2006},\n {u'doc_count': 93,\n u'key': 2005},\n {u'doc_count': 108,\n u'key': 2004},\n {u'doc_count': 106,\n u'key': 2003},\n {u'doc_count': 108,\n u'key': 2002},\n {u'doc_count': 91,\n u'key': 2001},\n {u'doc_count': 97,\n u'key': 2000}]}},\n 'hits': {'hits': [], 'max_score': None, 'total': 1000},\n 'profile': None,\n 'timed_out': False,\n 'took': 0}\n\n```\n\n<!-- request Python-asyncio -->\n``` python\nres = await searchApi.search({\"table\":\"films\",\"limit\":0,\"aggs\":{\"release_year\":{\"terms\":{\"field\":\"release_year\",\"size\":100}}}})\n```\n<!-- response Python-asyncio -->\n``` python\n{'aggregations': {u'release_year': {u'buckets': [{u'doc_count': 99,\n u'key': 2009},\n {u'doc_count': 102,\n u'key': 2008},\n {u'doc_count': 93,\n u'key': 2007},\n {u'doc_count': 103,\n u'key': 2006},\n {u'doc_count': 93,\n u'key': 2005},\n {u'doc_count': 108,\n u'key': 2004},\n {u'doc_count': 106,\n u'key': 2003},\n {u'doc_count': 108,\n u'key': 2002},\n {u'doc_count': 91,\n u'key': 2001},\n {u'doc_count': 97,\n u'key': 2000}]}},\n 'hits': {'hits': [], 'max_score': None, 'total': 1000},\n 'profile': None,\n 'timed_out': False,\n 'took': 0}\n\n```\n\n<!-- request Javascript -->\n``` javascript\nres = await searchApi.search({\"table\":\"films\",\"limit\":0,\"aggs\":{\"release_year\":{\"terms\":{\"field\":\"release_year\",\"size\":100}}}});\n```\n<!-- response Javascript -->\n``` javascript\n{\"took\":0,\"timed_out\":false,\"aggregations\":{\"release_year\":{\"buckets\":[{\"key\":2009,\"doc_count\":99},{\"key\":2008,\"doc_count\":102},{\"key\":2007,\"doc_count\":93},{\"key\":2006,\"doc_count\":103},{\"key\":2005,\"doc_count\":93},{\"key\":2004,\"doc_count\":108},{\"key\":2003,\"doc_count\":106},{\"key\":2002,\"doc_count\":108},{\"key\":2001,\"doc_count\":91},{\"key\":2000,\"doc_count\":97}]}},\"hits\":{\"total\":1000,\"hits\":[]}}\n```\n<!-- request Java -->\n``` java\nHashMap<String,Object> aggs = new HashMap<String,Object>(){{\n put(\"release_year\", new HashMap<String,Object>(){{\n put(\"terms\", new HashMap<String,Object>(){{\n put(\"field\",\"release_year\");\n put(\"size\",100);\n }});\n }});\n}};\n\nsearchRequest = new SearchRequest();\nsearchRequest.setIndex(\"films\");\nsearchRequest.setLimit(0);\nquery = new HashMap<String,Object>();\nquery.put(\"match_all\",null);\nsearchRequest.setQuery(query);\nsearchRequest.setAggs(aggs);\nsearchResponse = searchApi.search(searchRequest);\n```\n<!-- response Java -->\n``` java\nclass SearchResponse {\n took: 0\n timedOut: false\n aggregations: {release_year={buckets=[{key=2009, doc_count=99}, {key=2008, doc_count=102}, {key=2007, doc_count=93}, {key=2006, doc_count=103}, {key=2005, doc_count=93}, {key=2004, doc_count=108}, {key=2003, doc_count=106}, {key=2002, doc_count=108}, {key=2001, doc_count=91}, {key=2000, doc_count=97}]}}\n hits: class SearchResponseHits {\n maxScore: null\n total: 1000\n hits: []\n }\n profile: null\n}\n```\n\n<!-- request C# -->\n``` clike\nvar agg = new Aggregation(\"release_year\", \"release_year\");\nagg.Size = 100;\nobject query = new { match_all=null };\nvar searchRequest = new SearchRequest(\"films\", query);\nsearchRequest.Aggs = new List<Aggregation> {agg};\nvar searchResponse = searchApi.Search(searchRequest);\n```\n<!-- response C# -->\n``` clike\nclass SearchResponse {\n took: 0\n timedOut: false\n aggregations: {release_year={buckets=[{key=2009, doc_count=99}, {key=2008, doc_count=102}, {key=2007, doc_count=93}, {key=2006, doc_count=103}, {key=2005, doc_count=93}, {key=2004, doc_count=108}, {key=2003, doc_count=106}, {key=2002, doc_count=108}, {key=2001, doc_count=91}, {key=2000, doc_count=97}]}}\n hits: class SearchResponseHits {\n maxScore: null\n total: 1000\n hits: []\n }\n profile: null\n}\n```\n\n<!-- request Rust -->\n``` rust\nlet query = SearchQuery::new();\nlet aggTerms1 = AggTerms::new {\n fields: \"release_year\".to_string(),\n size: Some(100),\n};\nlet agg1 = Aggregation {\n terms: Some(Box::new(aggTerms1)),\n ..Default::default(),\n};\nlet mut aggs = HashMap::new();\naggs.insert(\"release_year\".to_string(), agg1);\n\nlet search_req = SearchRequest {\n table: \"films\".to_string(),\n query: Some(Box::new(query)),\n aggs: serde_json::json!(aggs),\n ..Default::default(),\n};\nlet search_res = search_api.search(search_req).await;\n```\n<!-- response Rust -->\n``` rust\nclass SearchResponse {\n took: 0\n timedOut: false\n aggregations: {release_year={buckets=[{key=2009, doc_count=99}, {key=2008, doc_count=102}, {key=2007, doc_count=93}, {key=2006, doc_count=103}, {key=2005, doc_count=93}, {key=2004, doc_count=108}, {key=2003, doc_count=106}, {key=2002, doc_count=108}, {key=2001, doc_count=91}, {key=2000, doc_count=97}]}}\n hits: class SearchResponseHits {\n maxScore: null\n total: 1000\n hits: []\n }\n profile: null\n}\n```\n\n<!-- request TypeScript -->\n``` typescript\nres = await searchApi.search({\n index: 'test',\n limit: 0,\n aggs: {\n cat_id: {\n terms: { field: \"cat\", size: 1 }\n }\n }\n});\n```\n\n<!-- response TypeScript -->\n``` typescript\n{\n\t\"took\":0,\n\t\"timed_out\":false,\n\t\"aggregations\":\n\t{\n\t\t\"cat_id\":\n\t\t{\n\t\t\t\"buckets\":\n\t\t\t[{\n\t\t\t\t\"key\":1,\n\t\t\t\t\"doc_count\":1\n\t\t\t}]\n\t\t}\n\t},\n\t\"hits\":\n\t{\n\t\t\"total\":5,\n\t\t\"hits\":[]\n\t}\n}\n```\n\n<!-- request Go -->\n``` go\nquery := map[string]interface{} {};\nsearchRequest.SetQuery(query);\naggTerms := manticoreclient.NewAggregationTerms()\naggTerms.SetField(\"cat\")\naggTerms.SetSize(1)\naggregation := manticoreclient.NewAggregation()\naggregation.setTerms(aggTerms)\nsearchRequest.SetAggregation(aggregation)\nres, _, _ := apiClient.SearchAPI.Search(context.Background()).SearchRequest(*searchRequest).Execute()\n```\n\n<!-- response Go -->\n``` go\n{\n\t\"took\":0,\n\t\"timed_out\":false,\n\t\"aggregations\":\n\t{\n\t\t\"cat_id\":\n\t\t{\n\t\t\t\"buckets\":\n\t\t\t[{\n\t\t\t\t\"key\":1,\n\t\t\t\t\"doc_count\":1\n\t\t\t}]\n\t\t}\n\t},\n\t\"hits\":\n\t{\n\t\t\"total\":5,\n\t\t\"hits\":[]\n\t}\n}\n```\n\n<!-- end -->\n\n<!-- example sort1 -->\n##### Sorting groups\nBy default, groups are not sorted, and the next thing you typically want to do is order them by something, like the field you're grouping by:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\n```sql\nSELECT release_year, count(*) from films GROUP BY release_year ORDER BY release_year asc limit 5;\n```\n<!-- response SQL -->\n```sql\n+--------------+----------+\n| release_year | count(*) |\n+--------------+----------+\n| 2000 | 97 |\n| 2001 | 91 |\n| 2002 | 108 |\n| 2003 | 106 |\n| 2004 | 108 |\n+--------------+----------+\n```\n<!-- end -->\n<!-- example sort2 -->\nAlternatively, you can sort by the aggregation:\n\n* by `count(*)` to display groups with the most elements first\n* by `avg(rental_rate)` to show the highest-rated movies first. Note that in the example, it's done via an alias: `avg(rental_rate)` is first mapped to `avg` in the `SELECT` list, and then we simply do `ORDER BY avg`\n\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL1 -->\n```sql\nSELECT release_year, count(*) FROM films GROUP BY release_year ORDER BY count(*) desc LIMIT 5;\n```\n<!-- response SQL1 -->\n```sql\n+--------------+----------+\n| release_year | count(*) |\n+--------------+----------+\n| 2004 | 108 |\n| 2002 | 108 |\n| 2003 | 106 |\n| 2006 | 103 |\n| 2008 | 102 |\n+--------------+----------+\n```\n\n<!-- request SQL2 -->\n```sql\nSELECT release_year, AVG(rental_rate) avg FROM films GROUP BY release_year ORDER BY avg desc LIMIT 5;\n```\n<!-- response SQL2 -->\n```sql\n+--------------+------------+\n| release_year | avg |\n+--------------+------------+\n| 2006 | 3.26184368 |\n| 2000 | 3.17556739 |\n| 2001 | 3.09989142 |\n| 2002 | 3.08259249 |\n| 2008 | 2.99000049 |\n+--------------+------------+\n```\n<!-- end -->\n\n<!-- example group3 -->\n##### GROUP BY multiple fields at once\nIn some cases, you might want to group not just by a single field, but by multiple fields at once, such as a movie's category and year:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\n```sql\nSELECT category_id, release_year, count(*) FROM films GROUP BY category_id, release_year ORDER BY category_id ASC, release_year ASC;\n```\n<!-- response SQL -->\n```sql\n+-------------+--------------+----------+\n| category_id | release_year | count(*) |\n+-------------+--------------+----------+\n| 1 | 2000 | 5 |\n| 1 | 2001 | 2 |\n| 1 | 2002 | 6 |\n| 1 | 2003 | 6 |\n| 1 | 2004 | 5 |\n| 1 | 2005 | 10 |\n| 1 | 2006 | 4 |\n| 1 | 2007 | 5 |\n| 1 | 2008 | 7 |\n| 1 | 2009 | 14 |\n| 2 | 2000 | 10 |\n| 2 | 2001 | 5 |\n| 2 | 2002 | 6 |\n| 2 | 2003 | 6 |\n| 2 | 2004 | 10 |\n| 2 | 2005 | 4 |\n| 2 | 2006 | 5 |\n| 2 | 2007 | 8 |\n| 2 | 2008 | 8 |\n| 2 | 2009 | 4 |\n+-------------+--------------+----------+\n```\n<!-- request JSON -->\n``` json\nPOST /search -d '\n {\n \"size\": 0,\n \"table\": \"films\",\n \"aggs\": {\n \"cat_release\": {\n \"composite\": {\n \"size\":5,\n \"sources\": [\n { \"category\": { \"terms\": { \"field\": \"category_id\" } } },\n { \"release year\": { \"terms\": { \"field\": \"release_year\" } } }\n ]\n }\n }\n }\n }\n'\n```\n<!-- response JSON -->\n``` json\n{\n \"took\": 0,\n \"timed_out\": false,\n \"hits\": {\n \"total\": 1000,\n \"total_relation\": \"eq\",\n \"hits\": []\n },\n \"aggregations\": {\n \"cat_release\": {\n \"after_key\": {\n \"category\": 1,\n \"release year\": 2007\n },\n \"buckets\": [\n {\n \"key\": {\n \"category\": 1,\n \"release year\": 2008\n },\n \"doc_count\": 7\n },\n {\n \"key\": {\n \"category\": 1,\n \"release year\": 2009\n },\n \"doc_count\": 14\n },\n {\n \"key\": {\n \"category\": 1,\n \"release year\": 2005\n },\n \"doc_count\": 10\n },\n {\n \"key\": {\n \"category\": 1,\n \"release year\": 2004\n },\n \"doc_count\": 5\n },\n {\n \"key\": {\n \"category\": 1,\n \"release year\": 2007\n },\n \"doc_count\": 5\n }\n ]\n }\n }\n}\n```\n<!-- end -->\n\n<!-- example group4 -->\n##### Give me N rows\nSometimes it's useful to see not just a single element per group, but multiple. This can be easily achieved with the help of `GROUP N BY`. For example, in the following case, we get two movies for each year rather than just one, which a simple `GROUP BY release_year` would have returned.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\n```sql\nSELECT release_year, title FROM films GROUP 2 BY release_year ORDER BY release_year DESC LIMIT 6;\n```\n<!-- response SQL -->\n```sql\n+--------------+-----------------------------+\n| release_year | title |\n+--------------+-----------------------------+\n| 2009 | ALICE FANTASIA |\n| 2009 | ALIEN CENTER |\n| 2008 | AMADEUS HOLY |\n| 2008 | ANACONDA CONFESSIONS |\n| 2007 | ANGELS LIFE |\n| 2007 | ARACHNOPHOBIA ROLLERCOASTER |\n+--------------+-----------------------------+\n```\n<!-- end -->\n\n<!-- example group5 -->\n##### Sorting inside a group\nAnother crucial analytics requirement is to sort elements within a group. To achieve this, use the `WITHIN GROUP ORDER BY ... {ASC|DESC}` clause. For example, let's get the highest-rated film for each year. Note that it works in parallel with just `ORDER BY`:\n\n* `WITHIN GROUP ORDER BY` sorts results **inside a group**\n* while just `GROUP BY` **sorts the groups themselves**\n\nThese two work entirely independently.\n\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\n```sql\nSELECT release_year, title, rental_rate FROM films GROUP BY release_year WITHIN GROUP ORDER BY rental_rate DESC ORDER BY release_year DESC LIMIT 5;\n```\n<!-- response SQL -->\n```sql\n+--------------+------------------+-------------+\n| release_year | title | rental_rate |\n+--------------+------------------+-------------+\n| 2009 | AMERICAN CIRCUS | 4.990000 |\n| 2008 | ANTHEM LUKE | 4.990000 |\n| 2007 | ATTACKS HATE | 4.990000 |\n| 2006 | ALADDIN CALENDAR | 4.990000 |\n| 2005 | AIRPLANE SIERRA | 4.990000 |\n+--------------+------------------+-------------+\n```\n<!-- end -->\n\n<!-- example group6 -->\n##### Filter groups\n`HAVING expression` is a helpful clause for filtering groups. While `WHERE` is applied before grouping, `HAVING` works with the groups. For example, let's keep only those years when the average rental rate of the films for that year was higher than 3. We get only four years:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\n```sql\nSELECT release_year, avg(rental_rate) avg FROM films GROUP BY release_year HAVING avg > 3;\n```\n<!-- response SQL -->\n```sql\n+--------------+------------+\n| release_year | avg |\n+--------------+------------+\n| 2002 | 3.08259249 |\n| 2001 | 3.09989142 |\n| 2000 | 3.17556739 |\n| 2006 | 3.26184368 |\n+--------------+------------+\n```\n<!-- end -->\n\n**Note:** The `total_found` value in [search query meta info](../Node_info_and_management/SHOW_META.md#SHOW-META) reflects the number of groups that match the `HAVING` condition. This enables proper pagination when using `HAVING` clauses with `GROUP BY`.\n\n<!-- example group7 -->\n##### GROUPBY()\nThere is a function `GROUPBY()` which returns the key of the current group. It's useful in many cases, especially when you [GROUP BY an MVA](../Searching/Grouping.md#Grouping-by-MVA-%28multi-value-attributes%29) or a [JSON value](../Searching/Grouping.md#Grouping-by-a-JSON-node).\n\nIt can also be used in `HAVING`, for example, to keep only years 2000 and 2002.\n\nNote that `GROUPBY()`is not recommended for use when you GROUP BY multiple fields at once. It will still work, but since the group key in this case is a compound of field values, it may not appear the way you expect.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\n```sql\nSELECT release_year, count(*) FROM films GROUP BY release_year HAVING GROUPBY() IN (2000, 2002);\n```\n<!-- response SQL -->\n```sql\n+--------------+----------+\n| release_year | count(*) |\n+--------------+----------+\n| 2002 | 108 |\n| 2000 | 97 |\n+--------------+----------+\n```\n<!-- end -->\n<!-- example mva -->\n##### Grouping by MVA (multi-value attributes)\nManticore supports grouping by [MVA](../Creating_a_table/Data_types.md#Multi-value-integer-%28MVA%29). To demonstrate how it works, let's create a table \"shoes\" with MVA \"sizes\" and insert a few documents into it:\n```sql\ncreate table shoes(title text, sizes multi);\ninsert into shoes values(0,'nike',(40,41,42)),(0,'adidas',(41,43)),(0,'reebook',(42,43));\n```\nso we have:\n```sql\nSELECT * FROM shoes;\n+---------------------+----------+---------+\n| id | sizes | title |\n+---------------------+----------+---------+\n| 1657851069130080265 | 40,41,42 | nike |\n| 1657851069130080266 | 41,43 | adidas |\n| 1657851069130080267 | 42,43 | reebook |\n+---------------------+----------+---------+\n```\nIf we now GROUP BY \"sizes\", it will process all our multi-value attributes and return an aggregation for each, in this case just the count:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\n```sql\nSELECT groupby() gb, count(*) FROM shoes GROUP BY sizes ORDER BY gb asc;\n```\n<!-- response SQL -->\n```sql\n+------+----------+\n| gb | count(*) |\n+------+----------+\n| 40 | 1 |\n| 41 | 2 |\n| 42 | 2 |\n| 43 | 2 |\n+------+----------+\n```\n\n<!-- request JSON -->\n``` json\nPOST /search -d '\n {\n \"table\" : \"shoes\",\n \"limit\": 0,\n \"aggs\" :\n {\n \"sizes\" :\n {\n \"terms\" :\n {\n \"field\":\"sizes\",\n \"size\":100\n }\n }\n }\n }\n'\n```\n<!-- response JSON -->\n``` json\n{\n \"took\": 0,\n \"timed_out\": false,\n \"hits\": {\n \"total\": 3,\n \"hits\": [\n\n ]\n },\n \"aggregations\": {\n \"sizes\": {\n \"buckets\": [\n {\n \"key\": 43,\n \"doc_count\": 2\n },\n {\n \"key\": 42,\n \"doc_count\": 2\n },\n {\n \"key\": 41,\n \"doc_count\": 2\n },\n {\n \"key\": 40,\n \"doc_count\": 1\n }\n ]\n }\n }\n}\n```\n<!-- request PHP -->\n``` php\n$index->setName('shoes');\n$search = $index->search('');\n$search->limit(0);\n$search->facet('sizes','sizes',100);\n$results = $search->get();\nprint_r($results->getFacets());\n```\n<!-- response PHP -->\n``` php\nArray\n(\n [sizes] => Array\n (\n [buckets] => Array\n (\n [0] => Array\n (\n [key] => 43\n [doc_count] => 2\n )\n [1] => Array\n (\n [key] => 42\n [doc_count] => 2\n )\n [2] => Array\n (\n [key] => 41\n [doc_count] => 2\n )\n [3] => Array\n (\n [key] => 40\n [doc_count] => 1\n )\n )\n )\n)\n```\n<!-- request Python -->\n``` python\nres =searchApi.search({\"table\":\"shoes\",\"limit\":0,\"aggs\":{\"sizes\":{\"terms\":{\"field\":\"sizes\",\"size\":100}}}})\n```\n<!-- response Python -->\n``` python\n{'aggregations': {u'sizes': {u'buckets': [{u'doc_count': 2, u'key': 43},\n {u'doc_count': 2, u'key': 42},\n {u'doc_count': 2, u'key': 41},\n {u'doc_count': 1, u'key': 40}]}},\n 'hits': {'hits': [], 'max_score': None, 'total': 3},\n 'profile': None,\n 'timed_out': False,\n 'took': 0}\n```\n<!-- request Javascript -->\n``` javascript\nres = await searchApi.search({\"table\":\"shoes\",\"limit\":0,\"aggs\":{\"sizes\":{\"terms\":{\"field\":\"sizes\",\"size\":100}}}});\n```\n\n<!-- request Python-asyncio -->\n``` python\nres = await searchApi.search({\"table\":\"shoes\",\"limit\":0,\"aggs\":{\"sizes\":{\"terms\":{\"field\":\"sizes\",\"size\":100}}}})\n```\n<!-- response Python-asyncio -->\n``` python\n{'aggregations': {u'sizes': {u'buckets': [{u'doc_count': 2, u'key': 43},\n {u'doc_count': 2, u'key': 42},\n {u'doc_count': 2, u'key': 41},\n {u'doc_count': 1, u'key': 40}]}},\n 'hits': {'hits': [], 'max_score': None, 'total': 3},\n 'profile': None,\n 'timed_out': False,\n 'took': 0}\n```\n\n<!-- request Javascript -->\n``` javascript\nres = await searchApi.search({\"table\":\"shoes\",\"limit\":0,\"aggs\":{\"sizes\":{\"terms\":{\"field\":\"sizes\",\"size\":100}}}});\n```\n\n<!-- response Javascript -->\n``` javascript\n{\"took\":0,\"timed_out\":false,\"aggregations\":{\"sizes\":{\"buckets\":[{\"key\":43,\"doc_count\":2},{\"key\":42,\"doc_count\":2},{\"key\":41,\"doc_count\":2},{\"key\":40,\"doc_count\":1}]}},\"hits\":{\"total\":3,\"hits\":[]}}\n```\n<!-- request Java -->\n``` java\nHashMap<String,Object> aggs = new HashMap<String,Object>(){{\n put(\"release_year\", new HashMap<String,Object>(){{\n put(\"terms\", new HashMap<String,Object>(){{\n put(\"field\",\"release_year\");\n put(\"size\",100);\n }});\n }});\n}};\n\nsearchRequest = new SearchRequest();\nsearchRequest.setIndex(\"films\");\nsearchRequest.setLimit(0);\nquery = new HashMap<String,Object>();\nquery.put(\"match_all\",null);\nsearchRequest.setQuery(query);\nsearchRequest.setAggs(aggs);\nsearchResponse = searchApi.search(searchRequest);\n```\n<!-- response Java -->\n``` java\nclass SearchResponse {\n took: 0\n timedOut: false\n aggregations: {release_year={buckets=[{key=43, doc_count=2}, {key=42, doc_count=2}, {key=41, doc_count=2}, {key=40, doc_count=1}]}}\n hits: class SearchResponseHits {\n maxScore: null\n total: 3\n hits: []\n }\n profile: null\n}\n\n```\n\n<!-- request C# -->\n``` clike\nvar agg = new Aggregation(\"release_year\", \"release_year\");\nagg.Size = 100;\nobject query = new { match_all=null };\nvar searchRequest = new SearchRequest(\"films\", query);\nsearchRequest.Limit = 0;\nsearchRequest.Aggs = new List<Aggregation> {agg};\nvar searchResponse = searchApi.Search(searchRequest);\n```\n<!-- response C# -->\n``` clike\nclass SearchResponse {\n took: 0\n timedOut: false\n aggregations: {release_year={buckets=[{key=43, doc_count=2}, {key=42, doc_count=2}, {key=41, doc_count=2}, {key=40, doc_count=1}]}}\n hits: class SearchResponseHits {\n maxScore: null\n total: 3\n hits: []\n }\n profile: null\n}\n\n```\n\n<!-- request Rust -->\n``` rust\nlet query = SearchQuery::new();\nlet aggTerms1 = AggTerms::new {\n fields: \"release_year\".to_string(),\n size: Some(100),\n};\nlet agg1 = Aggregation {\n terms: Some(Box::new(aggTerms1)),\n ..Default::default(),\n};\nlet mut aggs = HashMap::new();\naggs.insert(\"release_year\".to_string(), agg1);\n\nlet search_req = SearchRequest {\n table: \"films\".to_string(),\n query: Some(Box::new(query)),\n aggs: serde_json::json!(aggs),\n limit: serde_json::json!(0),\n ..Default::default(),\n};\nlet search_res = search_api.search(search_req).await;\n```\n<!-- response Rust -->\n``` rust\nclass SearchResponse {\n took: 0\n timedOut: false\n aggregations: {release_year={buckets=[{key=43, doc_count=2}, {key=42, doc_count=2}, {key=41, doc_count=2}, {key=40, doc_count=1}]}}\n hits: class SearchResponseHits {\n maxScore: null\n total: 3\n hits: []\n }\n profile: null\n}\n\n<!-- request TypeScript -->\n``` typescript\nres = await searchApi.search({\n index: 'test',\n aggs: {\n mva_agg: {\n terms: { field: \"mva_field\", size: 2 }\n }\n }\n});\n```\n\n<!-- response TypeScript -->\n``` typescript\n{\n\t\"took\":0,\n\t\"timed_out\":false,\n\t\"aggregations\":\n\t{\n\t\t\"mva_agg\":\n\t\t{\n\t\t\t\"buckets\":\n\t\t\t[{\n\t\t\t\t\"key\":1,\n\t\t\t\t\"doc_count\":4\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"key\":2,\n\t\t\t\t\"doc_count\":2\n\t\t\t}]\n\t\t}\n\t},\n\t\"hits\":\n\t{\n\t\t\"total\":4,\n\t\t\"hits\":[]\n\t}\n}\n```\n\n<!-- request Go -->\n``` go\nquery := map[string]interface{} {};\nsearchRequest.SetQuery(query);\naggTerms := manticoreclient.NewAggregationTerms()\naggTerms.SetField(\"mva_field\")\naggTerms.SetSize(2)\naggregation := manticoreclient.NewAggregation()\naggregation.setTerms(aggTerms)\nsearchRequest.SetAggregation(aggregation)\nres, _, _ := apiClient.SearchAPI.Search(context.Background()).SearchRequest(*searchRequest).Execute()\n```\n\n<!-- response Go -->\n``` go\n{\n\t\"took\":0,\n\t\"timed_out\":false,\n\t\"aggregations\":\n\t{\n\t\t\"mva_agg\":\n\t\t{\n\t\t\t\"buckets\":\n\t\t\t[{\n\t\t\t\t\"key\":1,\n\t\t\t\t\"doc_count\":4\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"key\":2,\n\t\t\t\t\"doc_count\":2\n\t\t\t}]\n\t\t}\n\t},\n\t\"hits\":\n\t{\n\t\t\"total\":5,\n\t\t\"hits\":[]\n\t}\n}\n```\n\n<!-- end -->\n\n<!-- example json -->\n##### Grouping by a JSON node\nIf you have a field of type [JSON](../Creating_a_table/Data_types.md#JSON), you can GROUP BY any node from it. To demonstrate this, let's create a table \"products\" with a few documents, each having a color in the \"meta\" JSON field:\n```sql\ncreate table products(title text, meta json);\ninsert into products values(0,'nike','{\"color\":\"red\"}'),(0,'adidas','{\"color\":\"red\"}'),(0,'puma','{\"color\":\"green\"}');\n```\nThis gives us:\n```sql\nSELECT * FROM products;\n+---------------------+-------------------+--------+\n| id | meta | title |\n+---------------------+-------------------+--------+\n| 1657851069130080268 | {\"color\":\"red\"} | nike |\n| 1657851069130080269 | {\"color\":\"red\"} | adidas |\n| 1657851069130080270 | {\"color\":\"green\"} | puma |\n+---------------------+-------------------+--------+\n```\nTo group the products by color, we can simply use `GROUP BY meta.color`, and to display the corresponding group key in the `SELECT` list, we can use `GROUPBY()`:\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\n```sql\nSELECT groupby() color, count(*) from products GROUP BY meta.color;\n```\n<!-- response SQL -->\n```sql\n+-------+----------+\n| color | count(*) |\n+-------+----------+\n| red | 2 |\n| green | 1 |\n+-------+----------+\n```\n<!-- request JSON -->\n``` json\nPOST /search -d '\n {\n \"table\" : \"products\",\n \"limit\": 0,\n \"aggs\" :\n {\n \"color\" :\n {\n \"terms\" :\n {\n \"field\":\"meta.color\",\n \"size\":100\n }\n }\n }\n }\n'\n```\n<!-- response JSON -->\n``` json\n{\n \"took\": 0,\n \"timed_out\": false,\n \"hits\": {\n \"total\": 3,\n \"hits\": [\n\n ]\n },\n \"aggregations\": {\n \"color\": {\n \"buckets\": [\n {\n \"key\": \"green\",\n \"doc_count\": 1\n },\n {\n \"key\": \"red\",\n \"doc_count\": 2\n }\n ]\n }\n }\n}\n```\n<!-- request PHP -->\n``` php\n$index->setName('products');\n$search = $index->search('');\n$search->limit(0);\n$search->facet('meta.color','color',100);\n$results = $search->get();\nprint_r($results->getFacets());\n```\n<!-- response PHP -->\n``` php\nArray\n(\n [color] => Array\n (\n [buckets] => Array\n (\n [0] => Array\n (\n [key] => green\n [doc_count] => 1\n )\n [1] => Array\n (\n [key] => red\n [doc_count] => 2\n )\n )\n )\n)\n\n```\n<!-- request Python -->\n``` python\nres =searchApi.search({\"table\":\"products\",\"limit\":0,\"aggs\":{\"color\":{\"terms\":{\"field\":\"meta.color\",\"size\":100}}}})\n```\n<!-- response Python -->\n``` python\n{'aggregations': {u'color': {u'buckets': [{u'doc_count': 1,\n u'key': u'green'},\n {u'doc_count': 2, u'key': u'red'}]}},\n 'hits': {'hits': [], 'max_score': None, 'total': 3},\n 'profile': None,\n 'timed_out': False,\n 'took': 0}\n```\n\n```\n<!-- request Python-asyncio -->\n``` python\nres = await searchApi.search({\"table\":\"products\",\"limit\":0,\"aggs\":{\"color\":{\"terms\":{\"field\":\"meta.color\",\"size\":100}}}})\n```\n<!-- response Python-asyncio -->\n``` python\n{'aggregations': {u'color': {u'buckets': [{u'doc_count': 1,\n u'key': u'green'},\n {u'doc_count': 2, u'key': u'red'}]}},\n 'hits': {'hits': [], 'max_score': None, 'total': 3},\n 'profile': None,\n 'timed_out': False,\n 'took': 0}\n```\n\n<!-- request Javascript -->\n``` javascript\nres = await searchApi.search({\"table\":\"products\",\"limit\":0,\"aggs\":{\"color\":{\"terms\":{\"field\":\"meta.color\",\"size\":100}}}});\n```\n<!-- response Javascript -->\n``` javascript\n{\"took\":0,\"timed_out\":false,\"aggregations\":{\"color\":{\"buckets\":[{\"key\":\"green\",\"doc_count\":1},{\"key\":\"red\",\"doc_count\":2}]}},\"hits\":{\"total\":3,\"hits\":[]}}\n```\n<!-- request Java -->\n``` java\nHashMap<String,Object> aggs = new HashMap<String,Object>(){{\n put(\"color\", new HashMap<String,Object>(){{\n put(\"terms\", new HashMap<String,Object>(){{\n put(\"field\",\"meta.color\");\n put(\"size\",100);\n }});\n }});\n}};\n\nsearchRequest = new SearchRequest();\nsearchRequest.setIndex(\"products\");\nsearchRequest.setLimit(0);\nquery = new HashMap<String,Object>();\nquery.put(\"match_all\",null);\nsearchRequest.setQuery(query);\nsearchRequest.setAggs(aggs);\nsearchResponse = searchApi.search(searchRequest);\n```\n<!-- response Java -->\n``` java\nclass SearchResponse {\n took: 0\n timedOut: false\n aggregations: {color={buckets=[{key=green, doc_count=1}, {key=red, doc_count=2}]}}\n hits: class SearchResponseHits {\n maxScore: null\n total: 3\n hits: []\n }\n profile: null\n}\n\n```\n\n<!-- request C# -->\n``` clike\nvar agg = new Aggregation(\"color\", \"meta.color\");\nagg.Size = 100;\nobject query = new { match_all=null };\nvar searchRequest = new SearchRequest(\"products\", query);\nsearchRequest.Limit = 0;\nsearchRequest.Aggs = new List<Aggregation> {agg};\nvar searchResponse = searchApi.Search(searchRequest);\n```\n<!-- response C# -->\n``` clike\nclass SearchResponse {\n took: 0\n timedOut: false\n aggregations: {color={buckets=[{key=green, doc_count=1}, {key=red, doc_count=2}]}}\n hits: class SearchResponseHits {\n maxScore: null\n total: 3\n hits: []\n }\n profile: null\n}\n\n```\n\n<!-- request Rust -->\n``` rust\nlet query = SearchQuery::new();\nlet aggTerms1 = AggTerms::new {\n fields: \"meta.color\".to_string(),\n size: Some(100),\n};\nlet agg1 = Aggregation {\n terms: Some(Box::new(aggTerms1)),\n ..Default::default(),\n};\nlet mut aggs = HashMap::new();\naggs.insert(\"color\".to_string(), agg1);\n\nlet search_req = SearchRequest {\n table: \"products\".to_string(),\n query: Some(Box::new(query)),\n aggs: serde_json::json!(aggs),\n limit: serde_json::json!(0),\n ..Default::default(),\n};\nlet search_res = search_api.search(search_req).await;\n```\n<!-- response Rust -->\n``` rust\nclass SearchResponse {\n took: 0\n timedOut: false\n aggregations: {color={buckets=[{key=green, doc_count=1}, {key=red, doc_count=2}]}}\n hits: class SearchResponseHits {\n maxScore: null\n total: 3\n hits: []\n }\n profile: null\n}\n\n```\n\n<!-- request TypeScript -->\n``` typescript\nres = await searchApi.search({\n index: 'test',\n aggs: {\n json_agg: {\n terms: { field: \"json_field.year\", size: 1 }\n }\n }\n});\n```\n\n<!-- response TypeScript -->\n``` typescript\n{\n\t\"took\":0,\n\t\"timed_out\":false,\n\t\"aggregations\":\n\t{\n\t\t\"json_agg\":\n\t\t{\n\t\t\t\"buckets\":\n\t\t\t[{\n\t\t\t\t\"key\":2000,\n\t\t\t\t\"doc_count\":2\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"key\":2001,\n\t\t\t\t\"doc_count\":2\n\t\t\t}]\n\t\t}\n\t},\n\t\"hits\":\n\t{\n\t\t\"total\":4,\n\t\t\"hits\":[]\n\t}\n}\n```\n\n<!-- request Go -->\n``` go\nquery := map[string]interface{} {};\nsearchRequest.SetQuery(query);\naggTerms := manticoreclient.NewAggregationTerms()\naggTerms.SetField(\"json_field.year\")\naggTerms.SetSize(2)\naggregation := manticoreclient.NewAggregation()\naggregation.setTerms(aggTerms)\nsearchRequest.SetAggregation(aggregation)\nres, _, _ := apiClient.SearchAPI.Search(context.Background()).SearchRequest(*searchRequest).Execute()\n```\n\n<!-- response Go -->\n``` go\n{\n\t\"took\":0,\n\t\"timed_out\":false,\n\t\"aggregations\":\n\t{\n\t\t\"json_agg\":\n\t\t{\n\t\t\t\"buckets\":\n\t\t\t[{\n\t\t\t\t\"key\":2000,\n\t\t\t\t\"doc_count\":2\n\t\t\t},\n\t\t\t{\n\t\t\t\t\"key\":2001,\n\t\t\t\t\"doc_count\":2\n\t\t\t}]\n\t\t}\n\t},\n\t\"hits\":\n\t{\n\t\t\"total\":4,\n\t\t\"hits\":[]\n\t}\n}\n```\n\n<!-- end -->\n\n## Aggregation functions\nBesides `COUNT(*)`, which returns the number of elements in each group, you can use various other aggregation functions:\n<!-- example distinct -->\n##### COUNT(DISTINCT field)\nWhile `COUNT(*)` returns the number of all elements in the group, `COUNT(DISTINCT field)` returns the number of unique values of the field in the group, which may be completely different from the total count. For instance, you can have 100 elements in the group, but all with the same value for a certain field. `COUNT(DISTINCT field)` helps to determine that. To demonstrate this, let's create a table \"students\" with the student's name, age, and major:\n```sql\nCREATE TABLE students(name text, age int, major string);\nINSERT INTO students values(0,'John',21,'arts'),(0,'William',22,'business'),(0,'Richard',21,'cs'),(0,'Rebecca',22,'cs'),(0,'Monica',21,'arts');\n```\n\nso we have:\n\n```sql\nMySQL [(none)]> SELECT * from students;\n+---------------------+------+----------+---------+\n| id | age | major | name |\n+---------------------+------+----------+---------+\n| 1657851069130080271 | 21 | arts | John |\n| 1657851069130080272 | 22 | business | William |\n| 1657851069130080273 | 21 | cs | Richard |\n| 1657851069130080274 | 22 | cs | Rebecca |\n| 1657851069130080275 | 21 | arts | Monica |\n+---------------------+------+----------+---------+\n```\n\nIn the example, you can see that if we GROUP BY major and display both `COUNT(*)` and `COUNT(DISTINCT age)`, it becomes clear that there are two students who chose the major \"cs\" with two unique ages, but for the major \"arts\", there are also two students, yet only one unique age.\n\nThere can be at most one `COUNT(DISTINCT)` per query.\n\n** By default, counts are approximate **\n\nActually, some of them are exact, while others are approximate. More on that below.\n\nManticore supports two algorithms for computing counts of distinct values. One is a legacy algorithm that uses a lot of memory and is usually slow. It collects `{group; value}` pairs, sorts them, and periodically discards duplicates. The benefit of this approach is that it guarantees exact counts within a plain table. You can enable it by setting the [distinct_precision_threshold](../Searching/Options.md#distinct_precision_threshold) option to `0`.\n\nThe other algorithm (enabled by default) loads counts into a hash table and returns its size. If the hash table becomes too large, its contents are moved into a `HyperLogLog`. This is where the counts become approximate since `HyperLogLog` is a probabilistic algorithm. The advantage is that the maximum memory usage per group is fixed and depends on the accuracy of the `HyperLogLog`. The overall memory usage also depends on the [max_matches](../Searching/Options.md#max_matches) setting, which reflects the number of groups.\n\nThe [distinct_precision_threshold](../Searching/Options.md#distinct_precision_threshold) option sets the threshold below which counts are guaranteed to be exact. The `HyperLogLog` accuracy setting and the threshold for the \"hash table to HyperLogLog\" conversion are derived from this setting. It's important to use this option with caution because doubling it will double the maximum memory required for count calculations. The maximum memory usage can be roughly estimated using this formula: `64 * max_matches * distinct_precision_threshold`. Note that this is the worst-case scenario, and in most cases, count calculations will use significantly less RAM.\n\n**`COUNT(DISTINCT)` against a distributed table or a real-time table consisting of multiple disk chunks may return inaccurate results**, but the result should be accurate for a distributed table consisting of local plain or real-time tables with the same schema (identical set/order of fields, but may have different tokenization settings).\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\n```sql\nSELECT major, count(*), count(distinct age) FROM students GROUP BY major;\n```\n<!-- response SQL -->\n```sql\n+----------+----------+---------------------+\n| major | count(*) | count(distinct age) |\n+----------+----------+---------------------+\n| arts | 2 | 1 |\n| business | 1 | 1 |\n| cs | 2 | 2 |\n+----------+----------+---------------------+\n```\n<!-- end -->\n\n<!-- example concat -->\n##### GROUP_CONCAT(field)\n\nOften, you want to better understand the contents of each group. You can use [GROUP N BY](../Searching/Grouping.md#Give-me-N-rows) for that, but it would return additional rows you might not want in the output. `GROUP_CONCAT()` enriches your grouping by concatenating values of a specific field in the group. Let's take the previous example and improve it by displaying all the ages in each group.\n\n`GROUP_CONCAT(field)` returns the list as comma-separated values.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\n```sql\nSELECT major, count(*), count(distinct age), group_concat(age) FROM students GROUP BY major\n```\n<!-- response SQL -->\n```sql\n+----------+----------+---------------------+-------------------+\n| major | count(*) | count(distinct age) | group_concat(age) |\n+----------+----------+---------------------+-------------------+\n| arts | 2 | 1 | 21,21 |\n| business | 1 | 1 | 22 |\n| cs | 2 | 2 | 21,22 |\n+----------+----------+---------------------+-------------------+\n```\n<!-- end -->\n<!-- example sum -->\n##### SUM(), MIN(), MAX(), AVG()\nOf course, you can also obtain the sum, average, minimum, and maximum values within a group.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\n```sql\nSELECT release_year year, sum(rental_rate) sum, min(rental_rate) min, max(rental_rate) max, avg(rental_rate) avg FROM films GROUP BY release_year ORDER BY year asc LIMIT 5;\n```\n<!-- response SQL -->\n```sql\n+------+------------+----------+----------+------------+\n| year | sum | min | max | avg |\n+------+------------+----------+----------+------------+\n| 2000 | 308.030029 | 0.990000 | 4.990000 | 3.17556739 |\n| 2001 | 282.090118 | 0.990000 | 4.990000 | 3.09989142 |\n| 2002 | 332.919983 | 0.990000 | 4.990000 | 3.08259249 |\n| 2003 | 310.940063 | 0.990000 | 4.990000 | 2.93339682 |\n| 2004 | 300.920044 | 0.990000 | 4.990000 | 2.78629661 |\n+------+------------+----------+----------+------------+\n```\n<!-- end -->\n\n<!-- example accuracy -->\n## Grouping accuracy\n\nGrouping is done in fixed memory, which depends on the [max_matches](../Searching/Options.md#max_matches) setting. If `max_matches` allows for storage of all found groups, the results will be 100% accurate. However, if the value of `max_matches` is lower, the results will be less accurate.\n\nWhen parallel processing is involved, it can become more complicated. When `pseudo_sharding` is enabled and/or when using an RT table with several disk chunks, each chunk or pseudo shard gets a result set that is no larger than `max_matches`. This can lead to inaccuracies in aggregates and group counts when the result sets from different threads are merged. To fix this, either a larger `max_matches` value or disabling parallel processing can be used.\n\nManticore will try to increase `max_matches` up to [max_matches_increase_threshold](../Searching/Options.md#max_matches_increase_threshold) if it detects that groupby may return inaccurate results. Detection is based on the number of unique values of the groupby attribute, which is retrieved from secondary indexes (if present).\n\nTo ensure accurate aggregates and/or group counts when using RT tables or `pseudo_sharding`, `accurate_aggregation` can be enabled. This will try to increase `max_matches` up to the threshold, and if the threshold is not high enough, Manticore will disable parallel processing for the query.\n\n<!-- intro -->\n##### Example:\n\n<!-- request SQL -->\n```sql\nMySQL [(none)]> SELECT release_year year, count(*) FROM films GROUP BY year limit 5;\n+------+----------+\n| year | count(*) |\n+------+----------+\n| 2004 | 108 |\n| 2002 | 108 |\n| 2001 | 91 |\n| 2005 | 93 |\n| 2000 | 97 |\n+------+----------+\n\nMySQL [(none)]> SELECT release_year year, count(*) FROM films GROUP BY year limit 5 option max_matches=1;\n+------+----------+\n| year | count(*) |\n+------+----------+\n| 2004 | 76 |\n+------+----------+\n\nMySQL [(none)]> SELECT release_year year, count(*) FROM films GROUP BY year limit 5 option max_matches=2;\n+------+----------+\n| year | count(*) |\n+------+----------+\n| 2004 | 76 |\n| 2002 | 74 |\n+------+----------+\n\nMySQL [(none)]> SELECT release_year year, count(*) FROM films GROUP BY year limit 5 option max_matches=3;\n+------+----------+\n| year | count(*) |\n+------+----------+\n| 2004 | 108 |\n| 2002 | 108 |\n| 2001 | 91 |\n+------+----------+\n```\n<!-- end -->\n<!-- proofread -->\n\n",
- "updated_at": 1768530797,
- "source_md5": "ec4c03f455f66322e1972d7ea06c4e4d"
- }
- }
|