{"id":5235,"date":"2021-03-30T14:33:14","date_gmt":"2021-03-30T06:33:14","guid":{"rendered":"https:\/\/www.gbase8.cn\/?p=5235"},"modified":"2021-06-29T09:59:51","modified_gmt":"2021-06-29T01:59:51","slug":"clickhouse-%e7%aa%97%e5%8f%a3%e5%87%bd%e6%95%b0row_numberrank-over%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.gbase8.cn\/en\/5235","title":{"rendered":"ClickHouse \u7a97\u53e3\u51fd\u6570row_number,Rank Over\u7684\u4f7f\u7528"},"content":{"rendered":"<p>\u5728\u5f53\u524d\u7248\u672c\u7684ClickHouse\u91cc\uff0c\u5df2\u7ecf\u5b9e\u9a8c\u6027\u7684\u652f\u6301\u4e86\u7a97\u53e3\u51fd\u6570\uff0c \u6bd4\u5982Rank over partition order\uff0c\u4f46\u4ece\u6267\u884c\u770b\u8fd8\u662f\u5904\u4e8e\u5b9e\u9a8c\u9636\u6bb5\uff0c\u540e\u9762\u3010\u53ef\u80fd\u3011\u5b58\u5728\u5e95\u5c42\u6539\u52a8\uff0c\u9700\u8981\u8bbe\u7f6e\u53c2\u6570Set allow_experimental_window_functions = 1\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-6a2ae76abaf9e\" 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-6a2ae76abaf9e\"  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\/5235\/#%E7%89%88%E6%9C%AC\" >\u7248\u672c<\/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\/5235\/#%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-3\" href=\"https:\/\/www.gbase8.cn\/en\/5235\/#%E6%95%B0%E6%8D%AE\" >\u6570\u636e<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.gbase8.cn\/en\/5235\/#%E8%AE%BE%E7%BD%AE%E5%8F%82%E6%95%B0\" >\u8bbe\u7f6e\u53c2\u6570<\/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\/5235\/#%E6%89%A7%E8%A1%8C%E6%95%88%E6%9E%9C\" >\u6267\u884c\u6548\u679c<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.gbase8.cn\/en\/5235\/#row_number_over%E7%9A%84%E4%BE%8B%E5%AD%90\" >row_number over\u7684\u4f8b\u5b50<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%89%88%E6%9C%AC\"><\/span>\u7248\u672c<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>clickhouse-client-21.3.4.25-2.noarch\nclickhouse-server-21.3.4.25-2.noarch\nclickhouse-common-static-21.3.4.25-2.x86_64 <\/code><\/pre>\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%95%B0%E6%8D%AE\"><\/span>\u6570\u636e<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase_rh7_001 :) show create table t1;\n\nSHOW CREATE TABLE t1\n\nQuery id: f6fece5e-23f8-4787-8acf-9cca89d7ab5c\n\n\u250c\u2500statement\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502 CREATE TABLE testdb.t1\n(\n    `id` Int32,\n    `name` String,\n    `birthday` Date\n)\nENGINE = TinyLog \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n\n1 rows in set. Elapsed: 0.004 sec.\ngbase_rh7_001 :) select * from t1;\n\nSELECT *\nFROM t1\n\nQuery id: 7e7b2912-9f1b-4462-856f-5864de752476\n\n\u250c\u2500id\u2500\u252c\u2500name\u2500\u2500\u2500\u252c\u2500\u2500\u2500birthday\u2500\u2510\n\u2502  1 \u2502 First  \u2502 2011-01-01 \u2502\n\u2502  2 \u2502 Second \u2502 2012-02-02 \u2502\n\u2502  3 \u2502 Second \u2502 2011-01-01 \u2502\n\u2514\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n\n3 rows in set. Elapsed: 0.006 sec.\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%AE%BE%E7%BD%AE%E5%8F%82%E6%95%B0\"><\/span>\u8bbe\u7f6e\u53c2\u6570<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u76f4\u63a5\u6267\u884c\u4f1a\u62a5\u9519\uff0c\u9700\u8981\u8bbe\u7f6e\u53c2\u6570 Set allow_experimental_window_functions = 1<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase_rh7_001 :) select id,name,rank() over (partition by birthday order by name) ran  from t1;\n\nSELECT\n    id,\n    name,\n    rank() OVER (PARTITION BY birthday ORDER BY name ASC) AS ran\nFROM t1\n\nQuery id: 152d1f62-8ddb-4fbd-b46a-764234004851\n\n\n0 rows in set. Elapsed: 0.003 sec.\n\nReceived exception from server (version 21.3.4):\nCode: 48. DB::Exception: Received from localhost:9000. DB::Exception: The support for window functions is experimental and will change in backwards-incompatible ways in the future releases. Set allow_experimental_window_functions = 1 to enable it. While processing 'rank() OVER (PARTITION BY birthday ORDER BY name ASC) AS ran'.\n\ngbase_rh7_001 :)\ngbase_rh7_001 :) set allow_experimental_window_functions=1;\n\nSET allow_experimental_window_functions = 1\n\nQuery id: 976e4a63-1c6c-44e9-b0ad-9de1dd28b5f6\n\nOk.\n\n0 rows in set. Elapsed: 0.029 sec.\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%89%A7%E8%A1%8C%E6%95%88%E6%9E%9C\"><\/span>\u6267\u884c\u6548\u679c<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u529f\u80fd\u6ca1\u95ee\u9898\uff0c\u6027\u80fd\u8fd8\u6ca1\u6d4b\u8bd5\u8fc7\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase_rh7_001 :) select id,name,rank() over (partition by birthday order by name) ran  from t1;\n\nSELECT\n    id,\n    name,\n    rank() OVER (PARTITION BY birthday ORDER BY name ASC) AS ran\nFROM t1\n\nQuery id: 8461d23b-c244-42ab-9f51-d0ea8683727d\n\n\u250c\u2500id\u2500\u252c\u2500name\u2500\u2500\u2500\u252c\u2500ran\u2500\u2510\n\u2502  1 \u2502 First  \u2502   1 \u2502\n\u2502  3 \u2502 Second \u2502   2 \u2502\n\u2502  2 \u2502 Second \u2502   1 \u2502\n\u2514\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\n\n3 rows in set. Elapsed: 0.085 sec.\n\ngbase_rh7_001 :)\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"row_number_over%E7%9A%84%E4%BE%8B%E5%AD%90\"><\/span>row_number over\u7684\u4f8b\u5b50<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6ce8\u610f\u5176\u4e2d\u7684limit 1 by name\u7684\u7528\u6cd5\uff0c\u4e0d\u7528\u518d\u50cf\u5176\u5b83\u6570\u636e\u5e93\u90a3\u6837\u5d4c\u5957\u4e00\u5c42rownum\u4e86\u3002\u76f4\u63a5\u53ef\u4ee5limit\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase_rh7_001 :) select id,name,row_number() over (partition by birthday order by name) ran  from t1;\n\nSELECT\n    id,\n    name,\n    row_number() OVER (PARTITION BY birthday ORDER BY name ASC) AS ran\nFROM t1\n\nQuery id: 5446a392-7591-4c17-8618-a34cec6a7d38\n\n\u250c\u2500id\u2500\u252c\u2500name\u2500\u2500\u2500\u252c\u2500ran\u2500\u2510\n\u2502  1 \u2502 First  \u2502   1 \u2502\n\u2502  3 \u2502 Second \u2502   2 \u2502\n\u2502  2 \u2502 Second \u2502   1 \u2502\n\u2514\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\n\n3 rows in set. Elapsed: 0.007 sec.\n\ngbase_rh7_001 :) select id,name,row_number() over (partition by birthday order by name) ran  from t1 limit for 2;\n\nSyntax error: failed at position 95 ('2'):\n\nselect id,name,row_number() over (partition by birthday order by name) ran  from t1 limit for 2;\n\nExpected one of: UNION, LIMIT, BY, LIKE, GLOBAL NOT IN, end of query, AS, DIV, IS, INTO OUTFILE, OR, QuestionMark, BETWEEN, OFFSET, NOT LIKE, MOD, AND, Comma, alias, WITH TIES, SETTINGS, IN, ILIKE, FORMAT, Dot, NOT ILIKE, NOT, Arrow, token, NOT IN, GLOBAL IN\n\ngbase_rh7_001 :) select id,name,row_number() over (partition by birthday order by name) ran  from t1 limit 1 by name;\n\nSELECT\n    id,\n    name,\n    row_number() OVER (PARTITION BY birthday ORDER BY name ASC) AS ran\nFROM t1\nLIMIT 1 BY name\n\nQuery id: a325089c-7201-468f-95f6-1ea446bacb89\n\n\u250c\u2500id\u2500\u252c\u2500name\u2500\u2500\u2500\u252c\u2500ran\u2500\u2510\n\u2502  1 \u2502 First  \u2502   1 \u2502\n\u2502  3 \u2502 Second \u2502   2 \u2502\n\u2514\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\n\n2 rows in set. Elapsed: 0.013 sec.\n\ngbase_rh7_001 :)\n<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f53\u524d\u7248\u672c\u7684ClickHouse\u91cc\uff0c\u5df2\u7ecf\u5b9e\u9a8c\u6027\u7684\u652f\u6301\u4e86\u7a97\u53e3\u51fd\u6570\uff0c \u6bd4\u5982Rank over partition order\uff0c\u4f46\u4ece\u6267\u884c\u770b\u8fd8\u662f\u5904\u4e8e\u5b9e\u9a8c\u9636\u6bb5\uff0c\u540e\u9762\u3010\u53ef\u80fd\u3011\u5b58\u5728\u5e95\u5c42\u6539\u52a8\uff0c\u9700\u8981\u8bbe\u7f6e\u53c2\u6570Set allow_experimental_window_functions = 1\u3002<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[36,77],"class_list":["post-5235","post","type-post","status-publish","format-standard","hentry","category-clickhouse","tag-36","tag-olap"],"_links":{"self":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/5235","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=5235"}],"version-history":[{"count":1,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/5235\/revisions"}],"predecessor-version":[{"id":5236,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/5235\/revisions\/5236"}],"wp:attachment":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/media?parent=5235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/categories?post=5235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/tags?post=5235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}