{"id":6667,"date":"2021-06-29T12:56:17","date_gmt":"2021-06-29T04:56:17","guid":{"rendered":"https:\/\/www.gbase8.cn\/?p=6667"},"modified":"2023-05-08T16:32:11","modified_gmt":"2023-05-08T08:32:11","slug":"gbase-8a%e5%bc%80%e7%aa%97%e5%87%bd%e6%95%b0%e5%ad%90%e5%8f%a5rows%e3%80%81range%e7%9a%84%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.gbase8.cn\/en\/6667","title":{"rendered":"\u5357\u5927\u901a\u7528GBase 8a\u5f00\u7a97\u51fd\u6570\u5b50\u53e5rows\u3001range\u7684\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p>GBase 8a \u652f\u6301\u5f00\u7a97\u5b50\u53e5rows\u548crange, \u672c\u6587\u4ecb\u7ecd\u5f00\u7a97\u5b50\u53e5\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Directory Navigation<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2ad79961e15\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2ad79961e15\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.gbase8.cn\/en\/6667\/#%E5%8F%82%E8%80%83\" >\u53c2\u8003<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.gbase8.cn\/en\/6667\/#%E8%AF%AD%E6%B3%95\" >\u8bed\u6cd5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.gbase8.cn\/en\/6667\/#%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E\" >\u4f7f\u7528\u8bf4\u660e<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.gbase8.cn\/en\/6667\/#%E6%95%B0%E6%8D%AE\" >\u6570\u636e<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.gbase8.cn\/en\/6667\/#ROWS%E7%9A%84%E4%BE%8B%E5%AD%90\" >ROWS\u7684\u4f8b\u5b50<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.gbase8.cn\/en\/6667\/#%E6%95%B4%E4%BD%93%E6%B1%87%E6%80%BB%E5%80%BC\" >\u6574\u4f53\u6c47\u603b\u503c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.gbase8.cn\/en\/6667\/#%E7%A7%BB%E5%8A%A8%E6%B1%87%E6%80%BB%E5%80%BC\" >\u79fb\u52a8\u6c47\u603b\u503c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.gbase8.cn\/en\/6667\/#%E6%8C%89%E7%B1%BB%E5%9E%8B%E5%88%86%E5%8C%BA%E8%AE%A1%E7%AE%97%E7%A7%BB%E5%8A%A8%E6%B1%87%E6%80%BB%E5%80%BC\" >\u6309\u7c7b\u578b\u5206\u533a\u8ba1\u7b97\u79fb\u52a8\u6c47\u603b\u503c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.gbase8.cn\/en\/6667\/#%E6%8C%87%E5%AE%9Arows%E7%9A%84%E6%95%B4%E4%BD%93%E7%A7%BB%E5%8A%A8%E6%B1%87%E6%80%BB%E5%80%BCcurrent_row\" >\u6307\u5b9arows\u7684\u6574\u4f53\u79fb\u52a8\u6c47\u603b\u503ccurrent row<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.gbase8.cn\/en\/6667\/#%E6%8C%87%E5%AE%9Arows%E7%9A%84%E6%95%B4%E4%BD%93%E7%A7%BB%E5%8A%A8%E6%B1%87%E6%80%BB%E5%80%BCfollowing\" >\u6307\u5b9arows\u7684\u6574\u4f53\u79fb\u52a8\u6c47\u603b\u503cfollowing<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.gbase8.cn\/en\/6667\/#%E6%8C%87%E5%AE%9Arows%E7%9A%84%E5%88%86%E5%8C%BA%E7%A7%BB%E5%8A%A8%E6%B1%87%E6%80%BB%E5%80%BC\" >\u6307\u5b9arows\u7684\u5206\u533a\u79fb\u52a8\u6c47\u603b\u503c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.gbase8.cn\/en\/6667\/#RANGE%E7%9A%84%E4%BE%8B%E5%AD%90\" >RANGE\u7684\u4f8b\u5b50<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.gbase8.cn\/en\/6667\/#%E6%97%B6%E9%97%B4%E7%B1%BB%E5%9E%8B%E7%9A%84RANGE%E4%BE%8B%E5%AD%90\" >\u65f6\u95f4\u7c7b\u578b\u7684RANGE\u4f8b\u5b50<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%8F%82%E8%80%83\"><\/span>\u53c2\u8003<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a href=\"https:\/\/www.gbase8.cn\/en\/6639\/\">GBase 8a\u7a97\u53e3\u51fd\u6570\u4f7f\u7528\u6837\u4f8b\u7d22\u5f15<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%AF%AD%E6%B3%95\"><\/span>\u8bed\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>rows \uff1a\u6307\u884c\uff0c\u5305\u542b\u8fb9\u754c\u884c<\/li><li>range \uff1a\u6570\u503c\u8303\u56f4\uff0c\u5305\u542b\u8fb9\u754c\u503c<\/li><li>Unbounded \uff1a\u662f\u65e0\u8fb9\u754c<\/li><li>Preceding \uff1a\u5411\u4e0a\uff0c\u51cf\u53bb\uff0c\u63d0\u524d<\/li><li>Following \uff1a\u5411\u4e0b\uff0c\u52a0\u4e0a\uff0c\u5ef6\u540e<\/li><li>Current row \uff1a\u5f53\u524d\u884c<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"658\" height=\"208\" src=\"https:\/\/www.gbase8.cn\/wp-content\/uploads\/2022\/06\/16548450651.png\" alt=\"GBase 8a\u5f00\u7a97\u51fd\u6570\u5b50\u53e5rows\u7684\u4f7f\u7528\u65b9\u6cd5\" class=\"wp-image-9630\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E\"><\/span>\u4f7f\u7528\u8bf4\u660e<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>_t_gbase_new_window_function_support \u53c2\u6570\u4e3a\u63a7\u5236\u652f\u6301windowing_clause \u5b50\u53e5\u7684\u53c2\u6570\uff0c\u53c2\u6570\u503c\u9ed8\u8ba4\u4e3a 0\uff0c\u4e0d\u652f\u6301 windowing_clause \u5b50\u53e5\uff0c\u8bbe\u7f6e\u4e3a 1 \u65f6\u652f\u6301 windowing_clause \u5b50\u53e5\u3002set _t_gbase_new_window_function_support=1<\/li><li>\u4e0d\u652f\u6301 windowing_clause \u5b50\u53e5\u60c5\u51b5\u4e0b\uff0c\u5f00\u7a97\u65b9\u5f0f\u662f\u56fa\u5b9a\u7684\uff0c\u8303\u56f4\u662f\uff1aRange between unbounded preceding and current row<\/li><li>Partition \u5b50\u53e5\u4e3a\u7a7a\uff0c\u5219\u4e0d\u5206\u7ec4\uff0c\u5373\u8bf4\u660e\u5168\u90e8\u6570\u636e\u4e3a\u4e00\u7ec4\uff1border by \u5b50\u53e5\u4e3a\u7a7a\uff0c\u5219\u6bcf\u4e2a\u5206\u7ec4\u4e2d\u7684\u6240\u6709\u6570\u636e\u4e0d\u6392\u5e8f\uff0c\u5373\u4e00\u4e2a\u5206\u7ec4\u5c31\u662f\u4e00\u4e2a\u201c\u5b50\u7a97\u53e3\u201d\u3002<\/li><li>between bound1 and bound2\u3002<ul><li>bound1 \u5b9a\u4e49\u7a97\u53e3\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c<\/li><li>bound2 \u5b9a\u4e49\u7a97\u53e3\u7684\u7ed3\u675f\u4f4d\u7f6e\u3002<\/li><li>\u5355\u72ec\u4e00\u4e2a bound \u65f6\uff0c\u4e3a\u8d77\u59cb\u4f4d\u7f6e\u7684\u5b9a\u4e49\uff0c\u7ed3\u675f\u4f4d\u7f6e\u9ed8\u8ba4\u4e3a current row\uff0c\u5982\uff1aselect a,b,c,sum(c)over(partition by a order by b range 2 preceding) as 'sum(c)' from t;<\/li><\/ul><\/li><li>unbounded preceding \u6307\u660e\u8d77\u59cb\u4f4d\u7f6e\uff0c\u5373\u5f53\u524d\u5206\u7ec4\u4e2d\u7684\u884c\u9996\uff0c\u4e0d\u80fd\u51fa\u73b0\u5728 bound2 \u4e2d\u3002<\/li><li>unbounded following \u6307\u660e\u7ed3\u675f\u4f4d\u7f6e\uff0c\u5373\u5f53\u524d\u5206\u7ec4\u4e2d\u7684\u884c\u5c3e\uff0c\u4e0d\u80fd\u51fa\u73b0\u5728 bound1 \u4e2d\u3002<\/li><li>current row \u6307\u660e\u8d77\u59cb\u6216\u7ed3\u675f\u4f4d\u7f6e\u4e3a\u5f53\u524d\u884c\u3002<\/li><li>\u5728\u5982\u4e0b\u60c5\u51b5\u65f6\uff0corder by \u5173\u952e\u5b57\u540e\u53ef\u4ee5\u6709\u591a\u4e2a\u8868\u8fbe\u5f0f\uff1a<ul><li>Range between unbounded preceding and current row<\/li><li>Range between unbounded preceding and unbounded following<\/li><li>Range between current row and current row<\/li><li>Range between current row and unbounded following<\/li><\/ul><\/li><li>value_expr preceding | value_expr following<ul><li>\u8d77\u59cb\u4f4d\u7f6e\u4e3a value_expr following \u65f6\uff0c\u5219\u7ed3\u675f\u4f4d\u7f6e\u5e94\u4e3a value_expr following\uff1b<\/li><li>\u7ed3\u675f\u4f4d\u7f6e\u4e3a value_expr preceding \u65f6\uff0c\u5219\u8d77\u59cb\u4f4d\u7f6e\u5e94\u4e3a value_expr preceding\u3002<\/li><\/ul><\/li><li>rows \u5173\u952e\u5b57\u540e\u63a5 value_expr \u65f6\uff0c\u4e0d\u652f\u6301 interval \u5b50\u53e5<\/li><li>value_expr \u4e3a num \u65f6\uff0c\u6807\u8bc6\u4e86\u884c\u7684\u504f\u79fb\u91cf\uff0c\u4e3a\u6570\u503c\u5e38\u91cf\uff0c\u6b63\u6574\u6570\uff08\u56db\u820d\u4e94\u5165\uff09\u3002\u4e0d\u53ef\u4e3a\u8d1f\u6570\u3001\u6570\u503c\u51fd\u6570\u3001\u8868\u5217\u3002<\/li><li>range \u5173\u952e\u5b57\u540e\u63a5 value_expr \u65f6\u652f\u6301 interval \u5b50\u53e5\uff0c\u4e14 order by \u540e\u53ea\u80fd\u6709\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff1a<ul><li>value_expr \u4e3a num \u65f6\uff0corder by \u540e\u7684\u8868\u8fbe\u5f0f\u53ea\u80fd\u4e3a\u6570\u503c\u6216\u65e5\u671f\uff08\u65f6\u95f4\uff09\u7c7b\u578b\uff1b<\/li><li>value_expr \u4e3a interval \u5b50\u53e5\u65f6\uff0corder by \u540e\u7684\u8868\u8fbe\u5f0f\u53ea\u80fd\u4e3a\u65e5\u671f\uff08\u65f6\u95f4\uff09\u7c7b\u578b;<\/li><li>value_expr \u4e3a num \u65f6\uff0c\u6807\u8bc6\u4e86\u884c\u7684\u504f\u79fb\u91cf\uff0c\u4e3a\u6570\u503c\u5e38\u91cf\uff0c\u6b63\u6574\u6570\uff08\u56db\u820d\u4e94\u5165\uff09\u3002\u4e0d\u53ef\u4e3a\u8d1f\u6570\u3001\u6570\u503c\u51fd\u6570\u3001\u8868\u5217\u3002<\/li><\/ul><\/li><li>range \u5173\u952e\u5b57\uff0corder by \u540e\u63a5\u552f\u4e00\u8868\u8fbe\u5f0f\u7528\u4e8e\u8ba1\u7b97\u7a97\u53e3\u8303\u56f4\u65f6\uff1a<ul><li>\u6392\u5e8f\u5b58\u5728 null \u503c\u4e14\u5728\u884c\u9996\u65f6\uff0c\u9664\u975e unbounded preceding\uff0c\u8be5\u884c\u4e0d\u8ba1\u5165\u7a97\u53e3\u8303\u56f4\uff1b<\/li><li>\u6392\u5e8f\u5b58\u5728 null \u503c\u4e14\u5728\u884c\u5c3e\u65f6\uff0c\u9664\u975e unbounded following\uff0c\u8be5\u884c\u4e0d\u8ba1\u5165\u7a97\u53e3\u8303\u56f4\uff1b<\/li><li>\u5f53\u524d\u884c\u4e3a null \u503c\uff0c\u9664\u975e unbounded preceding\/following\uff0c\u7a97\u53e3\u8303\u56f4\u4ec5\u4e3a\u8be5\u884c\u3002<\/li><\/ul><\/li><li>\u65e5\u671f\uff08\u65f6\u95f4\uff09\u7c7b\u578b\u7684\u652f\u6301\u8303\u56f4\uff1adate\u3001datetime \u548c timestamp\u3002<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%95%B0%E6%8D%AE\"><\/span>\u6570\u636e<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase&gt; create table t2(id int, type int, val int);\nQuery OK, 0 rows affected (Elapsed: 00:00:00.14)\n\ngbase&gt; insert into t2 values(1,1,111),(2,1,122),(3,1,133),(4,2,222),(5,2,233),(6,2,244),(7,3,333);\nQuery OK, 7 rows affected (Elapsed: 00:00:00.11)\nRecords: 7  Duplicates: 0  Warnings: 0\n\ngbase&gt; desc t2;\n+-------+---------+------+-----+---------+-------+\n| Field | Type    | Null | Key | Default | Extra |\n+-------+---------+------+-----+---------+-------+\n| id    | int(11) | YES  |     | NULL    |       |\n| type  | int(11) | YES  |     | NULL    |       |\n| val   | int(11) | YES  |     | NULL    |       |\n+-------+---------+------+-----+---------+-------+\n3 rows in set (Elapsed: 00:00:00.00)\n\ngbase&gt; select * from t2 order by type,val;\n+------+------+------+\n| id   | type | val  |\n+------+------+------+\n|    1 |    1 |  111 |\n|    2 |    1 |  122 |\n|    3 |    1 |  133 |\n|    4 |    2 |  222 |\n|    5 |    2 |  233 |\n|    6 |    2 |  244 |\n|    7 |    3 |  333 |\n+------+------+------+\n7 rows in set (Elapsed: 00:00:00.02)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"ROWS%E7%9A%84%E4%BE%8B%E5%AD%90\"><\/span>ROWS\u7684\u4f8b\u5b50<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5982\u4e0b\u4ee5SUM OVER\u4e3a\u4f8b\u5b50\u4ecb\u7ecd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%95%B4%E4%BD%93%E6%B1%87%E6%80%BB%E5%80%BC\"><\/span>\u6574\u4f53\u6c47\u603b\u503c<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase&gt; select sum(val) v from t2;\n+------+\n| v    |\n+------+\n| 1398 |\n+------+\n1 row in set (Elapsed: 00:00:00.03)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%A7%BB%E5%8A%A8%E6%B1%87%E6%80%BB%E5%80%BC\"><\/span>\u79fb\u52a8\u6c47\u603b\u503c<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u6570\u636e\u6309\u7167\u7c7b\u578b\uff0c\u6570\u5b57\u6392\u5e8f\uff0c\u8ba1\u7b97\u4e4b\u524d\u7684\u6c47\u603b\u503c\u3002 <\/p>\n\n\n\n<p>\u79fb\u52a8\u6c47\u603b\u53ef\u4ee5\u53c2\u8003\uff1a <a href=\"https:\/\/www.gbase8.cn\/wp-admin\/post.php?post=4765&amp;action=edit\">GBase 8a OLAP<\/a><a href=\"https:\/\/www.gbase8.cn\/en\/4765\/\">\u51fd\u6570\u79fb\u52a8\u5e73\u5747\u503cAVG\uff0c\u79fb\u52a8\u6c47\u603bSUM\uff0c\u79fb\u52a8\u8ba1\u6570count over\u7b49\u4f7f\u7528\u6837\u4f8b<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase&gt; select t2.*,sum(val) over(order by type,val) v from t2;\n+------+------+------+------+\n| id   | type | val  | v    |\n+------+------+------+------+\n|    1 |    1 |  111 |  111 |\n|    2 |    1 |  122 |  233 |\n|    3 |    1 |  133 |  366 |\n|    4 |    2 |  222 |  588 |\n|    5 |    2 |  233 |  821 |\n|    6 |    2 |  244 | 1065 |\n|    7 |    3 |  333 | 1398 |\n+------+------+------+------+\n7 rows in set (Elapsed: 00:00:00.03)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%8C%89%E7%B1%BB%E5%9E%8B%E5%88%86%E5%8C%BA%E8%AE%A1%E7%AE%97%E7%A7%BB%E5%8A%A8%E6%B1%87%E6%80%BB%E5%80%BC\"><\/span>\u6309\u7c7b\u578b\u5206\u533a\u8ba1\u7b97\u79fb\u52a8\u6c47\u603b\u503c<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u6309\u7167\u7c7b\u578b\u4e0d\u540c\uff0c\u5404\u81ea\u8ba1\u7b97\u79fb\u52a8\u6c47\u603b\u503c\u3002\u6bcf\u4e2a\u7c7b\u578b\u5404\u81ea\u8ba1\u7b97\u3002<\/p>\n\n\n\n<p>\u79fb\u52a8\u6c47\u603b\u53ef\u4ee5\u53c2\u8003\uff1a <a href=\"https:\/\/www.gbase8.cn\/wp-admin\/post.php?post=4765&amp;action=edit\">GBase 8a OLAP<\/a><a href=\"https:\/\/www.gbase8.cn\/en\/4765\/\">\u51fd\u6570\u79fb\u52a8\u5e73\u5747\u503cAVG\uff0c\u79fb\u52a8\u6c47\u603bSUM\uff0c\u79fb\u52a8\u8ba1\u6570count over\u7b49\u4f7f\u7528\u6837\u4f8b<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase&gt; select t2.*,sum(val) over(partition by type order by type,val) v from t2;\n+------+------+------+------+\n| id   | type | val  | v    |\n+------+------+------+------+\n|    4 |    2 |  222 |  222 |\n|    5 |    2 |  233 |  455 |\n|    6 |    2 |  244 |  699 |\n<span class=\"has-inline-color has-luminous-vivid-orange-color\">|    1 |    1 |  111 |  111 |\n|    2 |    1 |  122 |  233 |\n|    3 |    1 |  133 |  366 |<\/span>\n|    7 |    3 |  333 |  333 |\n+------+------+------+------+\n7 rows in set (Elapsed: 00:00:00.05)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%8C%87%E5%AE%9Arows%E7%9A%84%E6%95%B4%E4%BD%93%E7%A7%BB%E5%8A%A8%E6%B1%87%E6%80%BB%E5%80%BCcurrent_row\"><\/span>\u6307\u5b9arows\u7684\u6574\u4f53\u79fb\u52a8\u6c47\u603b\u503ccurrent row<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u5148\u770b\u770b\u4e0d\u5206\u533a\u7684\u6574\u4f53\u79fb\u52a8\u6c47\u603b\u503c\u7684\u60c5\u51b5\u3002\u6211\u4eec\u4f8b\u5b50\u91cc\u6307\u5b9a\u6c47\u603b\u524d1\u4e2a\u6570\u503c\u548c\u5f53\u524d\u503c\uff0c\u800c\u4e0d\u662f\u9ed8\u8ba4\u7684\u79fb\u52a8\u6c47\u603b\u503c\u7684\uff0c\u6240\u6709\u524d\u9762\u7684\u3002\u5982\u4e0b\u8d34\u4e862\u4e2a\u7ed3\u679c\uff0c\u65b9\u4fbf\u5bf9\u6bd4\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u666e\u901a\u79fb\u52a8\u6c47\u603b\uff0c\u6570\u503c\u4e00\u76f4\u5728\u7d2f\u52a0\u4e4b\u524d\u7684\uff0c\u800c\u6307\u5b9a\u4e86rows\u7684\uff0c\u53ea\u6c47\u603b\u4e86\u6307\u5b9a\u8303\u56f4\u5185\u7684\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table class=\"has-fixed-layout\"><tbody><tr><td>select t2.*,sum(val) over(order by type,val) v from t2;<\/td><td>select t2.*,sum(val) over(order by type,val rows between 1 preceding and current row) v from t2;<\/td><\/tr><tr><td><code>+------+------+------+------+<br>| id | type | val | v |<br>+------+------+------+------+<br>| 1 | 1 | 111 | 111 |<br>| 2 | 1 | 122 | 233 |<br>| 3 | 1 | 133 | 366 |<br>| 4 | 2 | 222 | 588 |<br>| 5 | 2 | 233 | 821 |<br>| 6 | 2 | 244 | 1065 |<br>| 7 | 3 | 333 | 1398 |<br>+------+------+------+------+<br>7 rows in set (Elapsed: 00:00:00.02)<\/code><\/td><td><code>+------+------+------+------+<br>| id | type | val | v |<br>+------+------+------+------+<br>| 1 | 1 | 111 | 111 |<br>| 2 | 1 | 122 | 233 |<br>| 3 | 1 | 133 | 255 |<br>| 4 | 2 | 222 | 355 |<br>| 5 | 2 | 233 | 455 |<br>| 6 | 2 | 244 | 477 |<br>| 7 | 3 | 333 | 577 |<br>+------+------+------+------+<br>7 rows in set (Elapsed: 00:00:00.02)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%8C%87%E5%AE%9Arows%E7%9A%84%E6%95%B4%E4%BD%93%E7%A7%BB%E5%8A%A8%E6%B1%87%E6%80%BB%E5%80%BCfollowing\"><\/span>\u6307\u5b9arows\u7684\u6574\u4f53\u79fb\u52a8\u6c47\u603b\u503cfollowing<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u672c\u4f8b\u6307\u5b9a\u524d1\u4e2a\u548c\u540e1\u4e2a\uff0c\u51713\u4e2a(\u5982\u679c\u6709\u7684\u8bdd)\u7684\u79fb\u52a8\u6c47\u603b\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase&gt; select t2.*,sum(val) over(order by type,val rows between 1 preceding and 1 following) v from t2;\n+------+------+------+------+\n| id   | type | val  | v    |\n+------+------+------+------+\n|    1 |    1 |  111 |  233 |\n|    2 |    1 |  122 |  366 |\n|    3 |    1 |  133 |  477 |\n|    4 |    2 |  222 |  588 |\n|    5 |    2 |  233 |  699 |\n|    6 |    2 |  244 |  810 |\n|    7 |    3 |  333 |  577 |\n+------+------+------+------+\n7 rows in set (Elapsed: 00:00:00.02)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%8C%87%E5%AE%9Arows%E7%9A%84%E5%88%86%E5%8C%BA%E7%A7%BB%E5%8A%A8%E6%B1%87%E6%80%BB%E5%80%BC\"><\/span>\u6307\u5b9arows\u7684\u5206\u533a\u79fb\u52a8\u6c47\u603b\u503c<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table class=\"has-fixed-layout\"><tbody><tr><td>select t2.*,sum(val) over(partition by type order by type,val) v from t2;<\/td><td>select t2.*,sum(val) over(partition by type order by type,val rows between 1 preceding and current row) v from t2;<\/td><\/tr><tr><td><code>+------+------+------+------+<br>| id | type | val | v |<br>+------+------+------+------+<br>| 1 | 1 | 111 | 111 |<br>| 2 | 1 | 122 | 233 |<br>| 3 | 1 | 133 | 366 |<br>| 7 | 3 | 333 | 333 |<br>| 4 | 2 | 222 | 222 |<br>| 5 | 2 | 233 | 455 |<br>| 6 | 2 | 244 | 699 |<br>+------+------+------+------+<br>7 rows in set (Elapsed: 00:00:00.05)<\/code><\/td><td><code>+------+------+------+------+<br>| id | type | val | v |<br>+------+------+------+------+<br>| 1 | 1 | 111 | 111 |<br>| 2 | 1 | 122 | 233 |<br>| 3 | 1 | 133 | 255 |<br>| 7 | 3 | 333 | 333 |<br>| 4 | 2 | 222 | 222 |<br>| 5 | 2 | 233 | 455 |<br>| 6 | 2 | 244 | 477 |<br>+------+------+------+------+<br>7 rows in set (Elapsed: 00:00:00.05)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"RANGE%E7%9A%84%E4%BE%8B%E5%AD%90\"><\/span>RANGE\u7684\u4f8b\u5b50<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u540c\u6837\u4ee5SUM OVER\u4e3a\u4f8b\u5b50\uff0c\u5206\u522b\u8ba1\u7b97\u524d\u9762\u5dee\u8ddd\u5c0f\u4e8e100\u7684\uff1b\u524d\u9762\u5dee\u8ddd\u5c0f\u4e8e100\u548c\u540e\u9762\u5dee\u8ddd\u5c0f\u4e8e100\u7684\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table class=\"has-fixed-layout\"><tbody><tr><td>gbase&gt; select t2.*,sum(val) over(order by type,val) v from t2;<\/td><td>select t2.*,sum(val) over(order by val range between <code><span class=\"has-inline-color has-luminous-vivid-orange-color\"><strong>100 preceding<\/strong><\/span><\/code> and 1 following) v from t2;<\/td><td>select t2.*,sum(val) over(order by val range between<span class=\"has-inline-color has-luminous-vivid-orange-color\"><strong> 100 preceding and 100 following<\/strong><\/span>) v from t2;<\/td><\/tr><tr><td><code>+------+------+------+------+<br>| id | type | val | v |<br>+------+------+------+------+<br>| 1 | 1 | 111 | 111 |<br>| 2 | 1 | 122 | 233 |<br>| 3 | 1 | 133 | 366 |<br>| 4 | 2 | 222 | 588 |<br>| 5 | 2 | 233 | 821 |<br>| 6 | 2 | 244 | 1065 |<br>| 7 | 3 | 333 | 1398 |<br>+------+------+------+------+<br>7 rows in set (Elapsed: 00:00:00.02)<\/code><\/td><td><code>+------+------+------+------+<br>| id | type | val | v |<br>+------+------+------+------+<br>| 1 | 1 | 111 | 111 |<br>| 2 | 1 | 122 | 233 |<br>| 3 | 1 | 133 | 366 |<br>| 4 | 2 | 222 | 477 |<br>| 5 | 2 | 233 | 588 |<br>| 6 | 2 | 244 | 699 |<br>| 7 | 3 | 333 | 810 |<br>+------+------+------+------+<br>7 rows in set (Elapsed: 00:00:00.03)<\/code><\/td><td><code>+------+------+------+------+<br>| id | type | val | v |<br>+------+------+------+------+<br>| 1 | 1 | 111 | 366 |<br>| 2 | 1 | 122 | 588 |<br>| 3 | 1 | 133 | 821 |<br>| 4 | 2 | 222 | 954 |<br>| 5 | 2 | 233 | 1165 |<br>| 6 | 2 | 244 | 1032 |<br>| 7 | 3 | 333 | 810 |<br>+------+------+------+------+<br>7 rows in set (Elapsed: 00:00:00.02)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%97%B6%E9%97%B4%E7%B1%BB%E5%9E%8B%E7%9A%84RANGE%E4%BE%8B%E5%AD%90\"><\/span>\u65f6\u95f4\u7c7b\u578b\u7684RANGE\u4f8b\u5b50<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6362\u4e2a\u6570\u636e\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase&gt; select * from t3;\n+------+--------+------------+\n| id   | name   | birth      |\n+------+--------+------------+\n|    1 | \u5f20\u4e09   | 1960-01-01 |\n|    2 | \u674e\u56db   | 1970-01-01 |\n|    3 | \u738b\u4e94   | 1980-01-01 |\n|    4 | \u8d75\u516d   | 1990-01-01 |\n+------+--------+------------+\n4 rows in set (Elapsed: 00:00:00.01)\n<\/code><\/pre>\n\n\n\n<p>\u5206\u522b\u8ba1\u7b97\uff1a\u5411\u524d\u65e0\u8fb9\u754c\uff0c\u5411\u540e5\u300110\u300120\u5e74\u751f\u65e5\u5dee\u8ddd\u7684\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase&gt; select t3.*,sum(id)over(order by birth range between unbounded preceding and interval 5 year following) v from t3;\n+------+--------+------------+------+\n| id   | name   | birth      | v    |\n+------+--------+------------+------+\n|    1 | \u5f20\u4e09   | 1960-01-01 |    1 |\n|    2 | \u674e\u56db   | 1970-01-01 |    3 |\n|    3 | \u738b\u4e94   | 1980-01-01 |    6 |\n|    4 | \u8d75\u516d   | 1990-01-01 |   10 |\n+------+--------+------------+------+\n4 rows in set (Elapsed: 00:00:00.02)\n\ngbase&gt; select t3.*,sum(id)over(order by birth range between unbounded preceding and interval 10 year following) v from t3;\n+------+--------+------------+------+\n| id   | name   | birth      | v    |\n+------+--------+------------+------+\n|    1 | \u5f20\u4e09   | 1960-01-01 |    3 |\n|    2 | \u674e\u56db   | 1970-01-01 |    6 |\n|    3 | \u738b\u4e94   | 1980-01-01 |   10 |\n|    4 | \u8d75\u516d   | 1990-01-01 |   10 |\n+------+--------+------------+------+\n4 rows in set (Elapsed: 00:00:00.02)\n\ngbase&gt; select t3.*,sum(id)over(order by birth range between unbounded preceding and interval 20 year following) v from t3;\n+------+--------+------------+------+\n| id   | name   | birth      | v    |\n+------+--------+------------+------+\n|    1 | \u5f20\u4e09   | 1960-01-01 |    6 |\n|    2 | \u674e\u56db   | 1970-01-01 |   10 |\n|    3 | \u738b\u4e94   | 1980-01-01 |   10 |\n|    4 | \u8d75\u516d   | 1990-01-01 |   10 |\n+------+--------+------------+------+\n4 rows in set (Elapsed: 00:00:00.05)\n<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>\u5357\u5927\u901a\u7528GBase 8a \u652f\u6301\u5f00\u7a97\u5b50\u53e5rows\u548crange, \u672c\u6587\u4ecb\u7ecd\u5f00\u7a97\u5b50\u53e5\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[36,77],"class_list":["post-6667","post","type-post","status-publish","format-standard","hentry","category-gbase8a","tag-36","tag-olap"],"_links":{"self":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/6667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/comments?post=6667"}],"version-history":[{"count":50,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/6667\/revisions"}],"predecessor-version":[{"id":11802,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/6667\/revisions\/11802"}],"wp:attachment":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/media?parent=6667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/categories?post=6667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/tags?post=6667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}