{"id":5600,"date":"2024-11-14T12:49:34","date_gmt":"2024-11-14T04:49:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5600\/"},"modified":"2024-11-14T12:49:34","modified_gmt":"2024-11-14T04:49:34","slug":"oracle-rank%e5%87%bd%e6%95%b0%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5600\/","title":{"rendered":"oracle rank\u51fd\u6570\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  oracle rank() \u51fd\u6570\u901a\u8fc7\u6307\u5b9a\u7684\u6392\u5e8f\u5b57\u6bb5\u5bf9\u6570\u636e\u96c6\u8fdb\u884c\u6392\u540d\uff0c\u4ece 1 \u5f00\u59cb\u5206\u914d\u6392\u540d\u3002\u7528\u6cd5\u793a\u4f8b\uff1asql select employee_id, name, salary, rank() over (order by salary asc) as salary_rank from employees;\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053018094286956.jpg\" class=\"aligncenter\" title=\"oracle rank\u51fd\u6570\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"oracle rank\u51fd\u6570\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>Oracle RANK() \u51fd\u6570\u7684\u4f7f\u7528<\/strong><\/p>\n<p>RANK() \u51fd\u6570\u5728 Oracle \u4e2d\u662f\u4e00\u4e2a\u5206\u6790\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u4e00\u7ec4\u884c\u8fdb\u884c\u6392\u540d\u3002\u6392\u540d\u4ece 1 \u5f00\u59cb\uff0c\u6839\u636e\u6307\u5b9a\u6392\u5e8f\u6761\u4ef6\u7684\u5347\u5e8f\u6216\u964d\u5e8f\u987a\u5e8f\u5206\u914d\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>RANK() OVER ( [PARTITION BY \u5206\u533a\u5b50\u53e5] ORDER BY \u6392\u5e8f\u5b57\u6bb5 [ASC|DESC] )<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u53c2\u6570\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>PARTITION BY \u5206\u533a\u5b50\u53e5\uff1a<\/strong>\u53ef\u9009\uff0c\u5c06\u6570\u636e\u96c6\u5212\u5206\u4e3a\u4e0d\u540c\u7684\u7ec4\uff0c\u5bf9\u6bcf\u4e2a\u7ec4\u8fdb\u884c\u5355\u72ec\u6392\u540d\u3002<\/li>\n<li> <strong>ORDER BY \u6392\u5e8f\u5b57\u6bb5\uff1a<\/strong>\u7528\u4e8e\u786e\u5b9a\u6392\u540d\u7684\u6392\u5e8f\u5b57\u6bb5\u3002<\/li>\n<li> <strong>ASC|DESC\uff1a<\/strong>\u53ef\u9009\uff0c\u6307\u5b9a\u662f\u5347\u5e8f\u8fd8\u662f\u964d\u5e8f\u6392\u540d\uff08\u9ed8\u8ba4\u5347\u5e8f\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<p>RANK() \u51fd\u6570\u901a\u5e38\u4e0e\u5176\u4ed6\u5206\u6790\u51fd\u6570\uff08\u5982 ROW_NUMBER() \u6216 DENSE_RANK()\uff09\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u63d0\u4f9b\u7075\u6d3b\u7684\u6392\u540d\u9009\u9879\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6309 &#8220;salary&#8221; \u5b57\u6bb5\u5347\u5e8f\u5bf9 &#8220;employees&#8221; \u8868\u4e2d\u7684\u5458\u5de5\u8fdb\u884c\u6392\u540d\uff1a<\/p>\n<pre>SELECT employee_id, name, salary, RANK() OVER (ORDER BY salary ASC) AS salary_rank\nFROM employees;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7ed3\u679c\u5c06\u663e\u793a\u6bcf\u4e2a\u5458\u5de5\u7684\u5458\u5de5 ID\u3001\u59d3\u540d\u3001\u85aa\u6c34\u548c\u85aa\u6c34\u6392\u540d\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>RANK() \u51fd\u6570\u5bf9\u91cd\u590d\u503c\u5206\u914d\u76f8\u540c\u7684\u6392\u540d\u3002<\/li>\n<li>\u5982\u679c\u672a\u6307\u5b9a PARTITION BY \u5b50\u53e5\uff0c\u5219\u5bf9\u6574\u4e2a\u6570\u636e\u96c6\u8fdb\u884c\u6392\u540d\u3002<\/li>\n<li>\u5982\u679c\u672a\u6307\u5b9a ORDER BY \u5b50\u53e5\uff0c\u5219\u6309\u8868\u7684\u9ed8\u8ba4\u6392\u5e8f\u987a\u5e8f\u8fdb\u884c\u6392\u540d\u3002<\/li>\n<li>RANK() \u51fd\u6570\u5bf9 NULL \u503c\u6267\u884c\u7279\u6b8a\u5904\u7406\uff0c\u5c06\u5b83\u4eec\u5f52\u4e3a\u6700\u540e\u4e00\u540d\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f rank\u51fd\u6570\u600e\u4e48\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>oracle rank() \u51fd\u6570\u901a\u8fc7\u6307\u5b9a\u7684\u6392\u5e8f\u5b57\u6bb5\u5bf9\u6570\u636e\u96c6\u8fdb\u884c\u6392\u540d\uff0c\u4ece 1 \u5f00\u59cb\u5206\u914d\u6392\u540d\u3002\u7528\u6cd5\u793a\u4f8b\uff1asql select employee_id, name, salary, rank() over (order by salary asc) as salary_rank from employees; Oracle RANK() \u51fd\u6570\u7684\u4f7f\u7528 RANK() \u51fd\u6570\u5728 Oracle \u4e2d\u662f\u4e00\u4e2a\u5206\u6790\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u4e00\u7ec4\u884c\u8fdb\u884c\u6392\u540d\u3002\u6392\u540d\u4ece 1 \u5f00\u59cb\uff0c\u6839\u636e\u6307\u5b9a\u6392\u5e8f\u6761\u4ef6\u7684\u5347\u5e8f\u6216\u964d\u5e8f\u987a\u5e8f\u5206\u914d\u3002 \u8bed\u6cd5\uff1a RANK() OVER ( [PARTITION BY \u5206\u533a\u5b50\u53e5] ORDER BY \u6392\u5e8f\u5b57\u6bb5 [ASC|DESC] ) \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570\uff1a PARTITION BY \u5206\u533a\u5b50\u53e5\uff1a\u53ef\u9009\uff0c\u5c06\u6570\u636e\u96c6\u5212\u5206\u4e3a\u4e0d\u540c\u7684\u7ec4\uff0c\u5bf9\u6bcf\u4e2a\u7ec4\u8fdb\u884c\u5355\u72ec\u6392\u540d\u3002 ORDER BY \u6392\u5e8f\u5b57\u6bb5\uff1a\u7528\u4e8e\u786e\u5b9a\u6392\u540d\u7684\u6392\u5e8f\u5b57\u6bb5\u3002 ASC|DESC\uff1a\u53ef\u9009\uff0c\u6307\u5b9a\u662f\u5347\u5e8f\u8fd8\u662f\u964d\u5e8f\u6392\u540d\uff08\u9ed8\u8ba4\u5347\u5e8f\uff09\u3002 \u7528\u6cd5\uff1a RANK() \u51fd\u6570\u901a\u5e38\u4e0e\u5176\u4ed6\u5206\u6790\u51fd\u6570\uff08\u5982 ROW_NUMBER() \u6216 DENSE_RANK()\uff09\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u63d0\u4f9b\u7075\u6d3b\u7684\u6392\u540d\u9009\u9879\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5600","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5600"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5600\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}