{"id":12473,"date":"2023-12-29T14:04:31","date_gmt":"2023-12-29T06:04:31","guid":{"rendered":"https:\/\/www.gbase8.cn\/?p=12473"},"modified":"2023-12-29T14:53:19","modified_gmt":"2023-12-29T06:53:19","slug":"gbase-8a-olap%e5%88%86%e6%9e%90%e5%87%bd%e6%95%b0first_value%e7%9a%84%e4%bd%bf%e7%94%a8%e6%a0%b7%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.gbase8.cn\/en\/12473","title":{"rendered":"\u5357\u5927\u901a\u7528GBase 8a OLAP\u5206\u6790\u51fd\u6570first_value\u7684\u4f7f\u7528\u6837\u4f8b"},"content":{"rendered":"<p>GBase 8a\u7684First_value\u51fd\u6570\uff0c\u8fd4\u56de\u6bcf\u7ec4\u7a97\u53e3\u91cc\u7684\u7b2c\u4e00\u4e2a\u503c\uff0c\u7c7b\u4f3crow_number\u662f\u7b49\u4e8e1\u7684\u3002\u672c\u6587\u4ecb\u7ecdfirst_value\u7684\u51fd\u6570\u7528\u6cd5\u5e76\u7ed9\u51fa\u4f7f\u7528\u4f8b\u5b50\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-6a2acbf072bbf\" 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-6a2acbf072bbf\"  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\/12473\/#%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-2\" href=\"https:\/\/www.gbase8.cn\/en\/12473\/#%E8%AF%B4%E6%98%8E\" >\u8bf4\u660e<\/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\/12473\/#%E6%A0%B7%E4%BE%8B\" >\u6837\u4f8b<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.gbase8.cn\/en\/12473\/#%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE\" >\u6d4b\u8bd5\u6570\u636e<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.gbase8.cn\/en\/12473\/#%E5%AE%8C%E6%95%B4first_value%E7%AA%97%E5%8F%A3%E7%94%A8%E6%B3%95\" >\u5b8c\u6574first_value\u7a97\u53e3\u7528\u6cd5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.gbase8.cn\/en\/12473\/#%E6%97%A0partition_%E7%9A%84first_value%E5%87%BD%E6%95%B0%E7%94%A8%E6%B3%95\" >\u65e0partition \u7684first_value\u51fd\u6570\u7528\u6cd5<\/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\/12473\/#%E6%9C%89partition%E6%97%A0order%E7%9A%84first_value%E7%94%A8%E6%B3%95\" >\u6709partition\u65e0order\u7684first_value\u7528\u6cd5<\/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\/12473\/#%E6%B2%A1%E6%9C%89partion%E5%92%8Corder%E7%9A%84first_value%E7%94%A8%E6%B3%95\" >\u6ca1\u6709partion\u548corder\u7684first_value\u7528\u6cd5<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\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<pre class=\"wp-block-code has-small-font-size\"><code>First_value(XXX) OVER(&#91;PARTITION BY col_name1,col_name2,\u2026] ORDER BY col_name1 &#91;asc\/desc], col_name2 &#91;asc\/desc],\u2026)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%AF%B4%E6%98%8E\"><\/span>\u8bf4\u660e<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5bf9\u6bcf\u4e2a\u7a97\u53e3(\u4e5f\u53ef\u4ee5\u4e0d\u6307\u5b9apartion,\u6240\u6709\u6570\u636e\u4e00\u4e2a\u7a97\u53e3\uff09\uff0c\u8fdb\u6307\u5b9a\u5217\u7684\u6392\u5e8f\uff08\u5982\u4e0d\u6392\u5e8f\uff0c\u5219\u9ed8\u8ba4\u987a\u5e8f)\uff0c\u7136\u540e\u8fd4\u56de\u3010\u622a\u6b62\u5230\u5f53\u524d\u884c\u7684\u3011\u7b2c\u4e00\u884c\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u53c2\u6570\u53ef\u4ee5\u662f\u5b57\u6bb5\uff0c\u8868\u8fbe\u5f0f\u7b49\u3002<\/p>\n\n\n\n<p>\u6ce8\u610fnull\u503c\u5728\u6392\u5e8f\u4e2d\u7684\u5f71\u54cd\u3002\u5982\u679c\u4e0d\u786e\u5b9a\uff0c\u5efa\u8bae\u5bf9\u5b57\u6bb5\u505a\u63d0\u524d\u5904\u7406\uff0c\u6bd4\u5982nvl\u51fd\u6570\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%A0%B7%E4%BE%8B\"><\/span>\u6837\u4f8b<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE\"><\/span>\u6d4b\u8bd5\u6570\u636e<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>gbase> create table td(id int, type int,val decimal(20,3));\r\nQuery OK, 0 rows affected (Elapsed: 00:00:00.17)\r\n\r\ngbase> insert into td values(1,1,111),(1,1,222),(2,1,333),(2,2,444),(3,2,555),(3,2,666),(4,2,777);\r\nQuery OK, 7 rows affected (Elapsed: 00:00:00.12)\r\nRecords: 7  Duplicates: 0  Warnings: 0\r\n\r\ngbase> insert into td values(5,1,null),(50,2,null);\r\nQuery OK, 2 rows affected (Elapsed: 00:00:00.09)\r\nRecords: 2  Duplicates: 0  Warnings: 0\r\n\r\ngbase> select * from td;\r\n+------+------+---------+\r\n| id   | type | val     |\r\n+------+------+---------+\r\n|    1 |    1 | 111.000 |\r\n|    1 |    1 | 222.000 |\r\n|    2 |    1 | 333.000 |\r\n|    2 |    2 | 444.000 |\r\n|    3 |    2 | 555.000 |\r\n|    3 |    2 | 666.000 |\r\n|    4 |    2 | 777.000 |\r\n|    5 |    1 |    NULL |\r\n|   50 |    2 |    NULL |\r\n+------+------+---------+\r\n9 rows in set (Elapsed: 00:00:00.03)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AE%8C%E6%95%B4first_value%E7%AA%97%E5%8F%A3%E7%94%A8%E6%B3%95\"><\/span>\u5b8c\u6574first_value\u7a97\u53e3\u7528\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>gbase> select *,first_value(val)over(partition by type order by id) maxOfType from td;\r\n+------+------+---------+-----------+\r\n| id   | type | val     | maxOfType |\r\n+------+------+---------+-----------+\r\n|    1 |    1 | 111.000 |   111.000 |\r\n|    1 |    1 | 222.000 |   111.000 |\r\n|    2 |    1 | 333.000 |   111.000 |\r\n|    5 |    1 |    NULL |   111.000 |\r\n|    2 |    2 | 444.000 |   444.000 |\r\n|    3 |    2 | 555.000 |   444.000 |\r\n|    3 |    2 | 666.000 |   444.000 |\r\n|    4 |    2 | 777.000 |   444.000 |\r\n|   50 |    2 |    NULL |   444.000 |\r\n+------+------+---------+-----------+\r\n9 rows in set (Elapsed: 00:00:00.34)\r\n\r\ngbase> select *,first_value(val)over(partition by type order by id desc) maxOfType from td;\r\n+------+------+---------+-----------+\r\n| id   | type | val     | maxOfType |\r\n+------+------+---------+-----------+\r\n|    5 |    1 |    NULL |      NULL |\r\n|    2 |    1 | 333.000 |      NULL |\r\n|    1 |    1 | 111.000 |      NULL |\r\n|    1 |    1 | 222.000 |      NULL |\r\n|   50 |    2 |    NULL |      NULL |\r\n|    4 |    2 | 777.000 |      NULL |\r\n|    3 |    2 | 555.000 |      NULL |\r\n|    3 |    2 | 666.000 |      NULL |\r\n|    2 |    2 | 444.000 |      NULL |\r\n+------+------+---------+-----------+\r\n9 rows in set (Elapsed: 00:00:00.14)\r\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%97%A0partition_%E7%9A%84first_value%E5%87%BD%E6%95%B0%E7%94%A8%E6%B3%95\"><\/span>\u65e0partition \u7684first_value\u51fd\u6570\u7528\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u6240\u6709\u6570\u636e\u6392\u5e8f\u540e\uff0c\u53d6\u7b2c\u4e00\u4e2a\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>gbase> select *,first_value(val)over(order by id) maxOfType from td;\r\n+------+------+---------+-----------+\r\n| id   | type | val     | maxOfType |\r\n+------+------+---------+-----------+\r\n|    1 |    1 | 111.000 |   111.000 |\r\n|    1 |    1 | 222.000 |   111.000 |\r\n|    2 |    1 | 333.000 |   111.000 |\r\n|    2 |    2 | 444.000 |   111.000 |\r\n|    3 |    2 | 555.000 |   111.000 |\r\n|    3 |    2 | 666.000 |   111.000 |\r\n|    4 |    2 | 777.000 |   111.000 |\r\n|    5 |    1 |    NULL |   111.000 |\r\n|   50 |    2 |    NULL |   111.000 |\r\n+------+------+---------+-----------+\r\n9 rows in set (Elapsed: 00:00:00.10)\r\n\r\ngbase> select *,first_value(val)over(order by id desc) maxOfType from td;\r\n+------+------+---------+-----------+\r\n| id   | type | val     | maxOfType |\r\n+------+------+---------+-----------+\r\n|   50 |    2 |    NULL |      NULL |\r\n|    5 |    1 |    NULL |      NULL |\r\n|    4 |    2 | 777.000 |      NULL |\r\n|    3 |    2 | 555.000 |      NULL |\r\n|    3 |    2 | 666.000 |      NULL |\r\n|    2 |    1 | 333.000 |      NULL |\r\n|    2 |    2 | 444.000 |      NULL |\r\n|    1 |    1 | 111.000 |      NULL |\r\n|    1 |    1 | 222.000 |      NULL |\r\n+------+------+---------+-----------+\r\n9 rows in set (Elapsed: 00:00:00.05)\r\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%9C%89partition%E6%97%A0order%E7%9A%84first_value%E7%94%A8%E6%B3%95\"><\/span>\u6709partition\u65e0order\u7684first_value\u7528\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u6bcf\u4e2a\u7a97\u53e3\u8fd4\u56de\u81ea\u7136\u987a\u5e8f\u7684\u7b2c\u4e00\u4e2a\u3002\u7531\u4e8e\u6570\u636e\u5e93\u5185\u5e76\u884c\uff0c\u65e0\u6cd5\u4fdd\u969c\u7ed3\u679c\u53ef\u91cd\u73b0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>gbase> select id,type,first_value(val)over(partition by type) maxOfType from td;\n+------+------+-----------+\ngbase> select *,first_value(val)over(partition by type) maxOfType from td;\r\n+------+------+---------+-----------+\r\n| id   | type | val     | maxOfType |\r\n+------+------+---------+-----------+\r\n|    1 |    1 | 222.000 |   222.000 |\r\n|    2 |    1 | 333.000 |   222.000 |\r\n|    5 |    1 |    NULL |   222.000 |\r\n|    1 |    1 | 111.000 |   222.000 |\r\n|    3 |    2 | 666.000 |   666.000 |\r\n|    4 |    2 | 777.000 |   666.000 |\r\n|    3 |    2 | 555.000 |   666.000 |\r\n|    2 |    2 | 444.000 |   666.000 |\r\n|   50 |    2 |    NULL |   666.000 |\r\n+------+------+---------+-----------+\r\n9 rows in set (Elapsed: 00:00:00.09)\r\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%B2%A1%E6%9C%89partion%E5%92%8Corder%E7%9A%84first_value%E7%94%A8%E6%B3%95\"><\/span>\u6ca1\u6709partion\u548corder\u7684first_value\u7528\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u6240\u6709\u6570\u636e\u4e00\u4e2a\u7a97\u53e3\uff0c\u9009\u81ea\u7136\u987a\u5e8f\u7684\u7b2c\u4e00\u4e2a\u3002 \u8fd9\u4e2a\u7528\u6cd5\u50cf\u6ca1\u5565\u610f\u4e49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>gbase> select *,first_value(val)over() maxOfType from td;\r\n+------+------+---------+-----------+\r\n| id   | type | val     | maxOfType |\r\n+------+------+---------+-----------+\r\n|    1 |    1 | 111.000 |   111.000 |\r\n|    1 |    1 | 222.000 |   111.000 |\r\n|    2 |    1 | 333.000 |   111.000 |\r\n|    2 |    2 | 444.000 |   111.000 |\r\n|    3 |    2 | 555.000 |   111.000 |\r\n|    3 |    2 | 666.000 |   111.000 |\r\n|    4 |    2 | 777.000 |   111.000 |\r\n|    5 |    1 |    NULL |   111.000 |\r\n|   50 |    2 |    NULL |   111.000 |\r\n+------+------+---------+-----------+\r\n9 rows in set (Elapsed: 00:00:00.08)\r\n\r\n\n<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>\u5357\u5927\u901a\u7528GBase 8a\u7684First_value\u51fd\u6570\uff0c\u8fd4\u56de\u6bcf\u7ec4\u7a97\u53e3\u91cc\u7684\u7b2c\u4e00\u4e2a\u503c\uff0c\u7c7b\u4f3crow_number\u662f\u7b49\u4e8e1\u7684\u3002\u672c\u6587\u4ecb\u7ecdfirst_value\u7684\u51fd\u6570\u7528\u6cd5\u5e76\u7ed9\u51fa\u4f7f\u7528\u4f8b\u5b50\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-12473","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\/12473","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=12473"}],"version-history":[{"count":5,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/12473\/revisions"}],"predecessor-version":[{"id":12488,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/12473\/revisions\/12488"}],"wp:attachment":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/media?parent=12473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/categories?post=12473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/tags?post=12473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}