{"id":6649,"date":"2021-06-29T10:20:48","date_gmt":"2021-06-29T02:20:48","guid":{"rendered":"https:\/\/www.gbase8.cn\/?p=6649"},"modified":"2021-06-29T10:22:15","modified_gmt":"2021-06-29T02:22:15","slug":"gbase-8a%e7%aa%97%e5%8f%a3%e5%87%bd%e6%95%b0row_number%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\/6649","title":{"rendered":"\u5357\u5927\u901a\u7528GBase 8a\u7a97\u53e3\u51fd\u6570row_number\u7684\u4f7f\u7528\u6837\u4f8b"},"content":{"rendered":"<p>\u521a\u53d1\u73b0\uff0c\u8fd9\u4e2a\u6700\u5e38\u7528\u7684GBase 8a\u7684\u7a97\u53e3\u51fd\u6570\u7adf\u7136\u6ca1\u6709\u5199\u4e2a\u5355\u72ec\u7684\u4f8b\u5b50\uff0c\u53ea\u662f\u5728\u5176\u5b83\u5e16\u5b50\u91cc\u63d0\u4e86\u4e00\u4e0b\uff0c\u8fd9\u91cc\u505a\u4e2a\u5355\u72ec\u7684\u8fd9\u91cc\u5427\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-6a2afd7634753\" 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-6a2afd7634753\"  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\/6649\/#%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\/6649\/#%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\/6649\/#%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-4\" href=\"https:\/\/www.gbase8.cn\/en\/6649\/#%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-5\" href=\"https:\/\/www.gbase8.cn\/en\/6649\/#%E8%A1%A8%E6%95%B0%E6%8D%AE\" >\u8868\u6570\u636e<\/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\/6649\/#%E6%97%A0PARTITION\" >\u65e0PARTITION<\/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\/6649\/#%E5%B8%A6Partition\" >\u5e26Partition<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.gbase8.cn\/en\/6649\/#%E6%9F%A5%E8%AF%A2%E6%AF%8F%E4%B8%AAPartition%E7%9A%84%E5%89%8D2%E5%90%8D\" >\u67e5\u8be2\u6bcf\u4e2aPartition\u7684\u524d2\u540d<\/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><br><a href=\"https:\/\/www.gbase8.cn\/en\/5323\/\">GBase 8a \u6392\u5e8f\u652f\u6301nulls first \/ last\u7684\u4f7f\u7528\u6837\u4f8b\uff0c\u5305\u62ec\u5f00\u7a97\u51fd\u6570<\/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<pre class=\"wp-block-code\"><code>ROW_NUMBER( ) 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>\u8fd4\u56de\u6bcf\u4e00\u884c\u5bf9\u5e94\u7684\u3010\u8fde\u7eed\u3011\u987a\u5e8f\u4f4d\u7f6e\u3002\u548cRank \u7684\u533a\u522b\uff0c\u5c31\u662f\u8fde\u7eed\uff0c\u800c\u5728Rank\u91cc\u76f8\u540c\u7684\u503c\u4f4d\u7f6e\u53f7\u4f1a\u76f8\u540c\uff0c\u7c7b\u4f3c\u5e76\u5217\u7b2c\u51e0\u540d\uff0c\u800crow_number\u5219\u4f1a\u987a\u5e8f\u8fde\u7eed\u6392\u4e0b\u53bb\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>PARTITION BY : \u53ef\u9009\uff0c\u591a\u4e2a\u5b57\u6bb5\u7528\u9017\u53f7\u5206\u5272\u3002\u5982\u679c\u6307\u5b9a\u4e86\uff0c\u5219\u6bcf\u4e2a\u5f00\u7a97\u5206\u533a\u7684\u987a\u5e8f\u53f7\u5404\u81ea\u72ec\u7acb\u3002<\/li><li>ORDER BY \uff1a \u5fc5\u9009\uff0c\u6392\u5e8f\u7684\u5b57\u6bb5\uff0c\u591a\u4e2a\u5b57\u6bb5\u7528\u9017\u53f7\u5206\u5272\u3002<\/li><\/ul>\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=\"%E8%A1%A8%E6%95%B0%E6%8D%AE\"><\/span>\u8868\u6570\u636e<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase> desc t2;\r\n+-------+---------+------+-----+---------+-------+\r\n| Field | Type    | Null | Key | Default | Extra |\r\n+-------+---------+------+-----+---------+-------+\r\n| id    | int(11) | YES  |     | NULL    |       |\r\n| type  | int(11) | YES  |     | NULL    |       |\r\n| val   | int(11) | YES  |     | NULL    |       |\r\n+-------+---------+------+-----+---------+-------+\r\n3 rows in set (Elapsed: 00:00:00.00)\r\n\r\ngbase> select * from t2 order by type,val desc;\r\n+------+------+------+\r\n| id   | type | val  |\r\n+------+------+------+\r\n|    3 |    1 |  133 |\r\n|    2 |    1 |  122 |\r\n|    1 |    1 |  111 |\r\n|    6 |    2 |  244 |\r\n|    5 |    2 |  233 |\r\n|    4 |    2 |  222 |\r\n|    7 |    3 |  333 |\r\n+------+------+------+\r\n7 rows in set (Elapsed: 00:00:00.03)\r\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%97%A0PARTITION\"><\/span>\u65e0PARTITION<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u4e0e\u76f4\u63a5order by\u65e0\u5f02\uff0c\u53ea\u662f\u591a\u4e86\u4e00\u4e2a\u3010\u884c\u53f7\u3011\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase> select t2.*,row_number() over(order by val desc) from t2;\r\n+------+------+------+--------------------------------------+\r\n| id   | type | val  | row_number() over(order by val desc) |\r\n+------+------+------+--------------------------------------+\r\n|    7 |    3 |  333 |                                    1 |\r\n|    6 |    2 |  244 |                                    2 |\r\n|    5 |    2 |  233 |                                    3 |\r\n|    4 |    2 |  222 |                                    4 |\r\n|    3 |    1 |  133 |                                    5 |\r\n|    2 |    1 |  122 |                                    6 |\r\n|    1 |    1 |  111 |                                    7 |\r\n+------+------+------+--------------------------------------+\r\n7 rows in set (Elapsed: 00:00:00.03)\r\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%B8%A6Partition\"><\/span>\u5e26Partition<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u6309\u7167type\u5f00\u7a97\u5206\u7ec4\uff0c\u6bcf\u4e2a\u5206\u7ec4\u5185\uff0c\u5404\u81ea\u505a\u6392\u5e8f\u540e\u7684\u884c\u53f7\u3002\u53ef\u4ee5\u770b\u5230type=1,2,3\u7684\u505a\u4e86\u4e09\u7ec4\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase> select t2.*,row_number() over(partition by type order by val desc) row from t2;\r\n+------+------+------+-----+\r\n| id   | type | val  | row |\r\n+------+------+------+-----+\r\n|    3 |    1 |  133 |   1 |\r\n|    2 |    1 |  122 |   2 |\r\n|    1 |    1 |  111 |   3 |\r\n<span class=\"has-inline-color has-luminous-vivid-orange-color\">|    6 |    2 |  244 |   1 |\r\n|    5 |    2 |  233 |   2 |\r\n|    4 |    2 |  222 |   3 |<\/span>\r\n|    7 |    3 |  333 |   1 |\r\n+------+------+------+-----+\r\n7 rows in set (Elapsed: 00:00:00.05)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%9F%A5%E8%AF%A2%E6%AF%8F%E4%B8%AAPartition%E7%9A%84%E5%89%8D2%E5%90%8D\"><\/span>\u67e5\u8be2\u6bcf\u4e2aPartition\u7684\u524d2\u540d<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u8fd9\u4e2a\u7c7b\u4f3c\u83b7\u53d6\u6bcf\u4e2a\u73ed\u6210\u7ee9\u6700\u597d\u7684\u524d\u51e0\u540d\u7684\u9700\u6c42\u3002\u9700\u8981\u4f7f\u7528\u5d4c\u5957\u67e5\u8be2\uff0c\u5c06row_number\u8fd4\u56de\u7684\u884c\u53f7\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u5217\u505a\u8fc7\u6ee4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase> select * from (select t2.*,row_number() over(partition by type order by val desc) row from t2) t where row&lt;=2 order by type,row;\r\n+------+------+------+-----+\r\n| id   | type | val  | row |\r\n+------+------+------+-----+\r\n|    3 |    1 |  133 |   1 |\r\n|    2 |    1 |  122 |   2 |\r\n<span class=\"has-inline-color has-luminous-vivid-orange-color\">|    6 |    2 |  244 |   1 |\r\n|    5 |    2 |  233 |   2 |<\/span>\r\n|    7 |    3 |  333 |   1 |\r\n+------+------+------+-----+\r\n5 rows in set (Elapsed: 00:00:00.07)\r\n<\/code><\/pre>\n\n\n\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>\u5357\u5927\u901a\u7528GBase 8a\u7a97\u53e3\u51fd\u6570row_number\u7684\u4f7f\u7528\u6837\u4f8b<\/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-6649","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\/6649","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=6649"}],"version-history":[{"count":9,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/6649\/revisions"}],"predecessor-version":[{"id":6660,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/6649\/revisions\/6660"}],"wp:attachment":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/media?parent=6649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/categories?post=6649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/tags?post=6649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}