{"id":5206,"date":"2024-11-14T09:57:09","date_gmt":"2024-11-14T01:57:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5206\/"},"modified":"2024-11-14T09:57:09","modified_gmt":"2024-11-14T01:57:09","slug":"sql-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\/5206\/","title":{"rendered":"sql\u00a0rank\u51fd\u6570\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  sql rank() \u51fd\u6570\u53ef\u5bf9\u67e5\u8be2\u7ed3\u679c\u6309\u7ed9\u5b9a\u5217\u8fdb\u884c\u6392\u540d\uff0c\u8fd4\u56de\u6bcf\u4e2a\u884c\u7684\u76f8\u5bf9\u4f4d\u7f6e\u3002\u5b83\u53ef\u7528\u4e8e\u5206\u533a\u6392\u540d\uff0c\u5177\u6709\u91cd\u590d\u503c\u65f6\u8df3\u8fc7\u6392\u540d\uff0c\u5e76\u8fd4\u56de null \u503c\u884c\u7684 null \u6392\u540d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/24\/2024052420422259453.jpg\" class=\"aligncenter\" title=\"sql\u00a0rank\u51fd\u6570\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql\u00a0rank\u51fd\u6570\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL RANK() \u51fd\u6570\u7528\u6cd5<\/strong><\/p>\n<p>SQL RANK() \u51fd\u6570\u7528\u4e8e\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u6392\u540d\uff0c\u53ef\u4ee5\u4e3a\u6bcf\u884c\u5206\u914d\u4e00\u4e2a\u6392\u540d\u503c\u3002\u6392\u540d\u503c\u8868\u793a\u8be5\u884c\u5728\u7ed3\u679c\u96c6\u4e2d\u7684\u76f8\u5bf9\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>RANK() OVER (PARTITION BY partition_expression ORDER BY order_expression)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u53c2\u6570<\/strong><\/p>\n<ul>\n<li> <strong>partition_expression (\u53ef\u9009)\uff1a<\/strong>\u5c06\u7ed3\u679c\u96c6\u5212\u5206\u4e3a\u4e0d\u540c\u7684\u5206\u533a\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5206\u533a\u5185\u7684\u884c\u8fdb\u884c\u6392\u540d\u3002\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5219\u5bf9\u6574\u4e2a\u7ed3\u679c\u96c6\u8fdb\u884c\u6392\u540d\u3002<\/li>\n<li> <strong>order_expression\uff1a<\/strong>\u6307\u5b9a\u7528\u4e8e\u6392\u5e8f\u7684\u5217\uff0c\u6392\u540d\u5c06\u57fa\u4e8e\u6b64\u5217\u7684\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u529f\u80fd<\/strong><\/p>\n<p>RANK() \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6570\u5b57\uff0c\u8868\u793a\u8be5\u884c\u5728\u7ed3\u679c\u96c6\u4e2d\u7684\u6392\u540d\u3002\u5b83\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a<\/p>\n<ul>\n<li>\u5177\u6709\u76f8\u540c\u503c\u7684\u884c\u5c06\u83b7\u5f97\u76f8\u540c\u7684\u6392\u540d\u3002<\/li>\n<li>\u5982\u679c\u6709\u91cd\u590d\u7684\u884c\uff0c\u5b83\u4eec\u7684\u6392\u540d\u5c06\u76f8\u540c\uff0c\u4f46\u540e\u7eed\u884c\u5c06\u8df3\u8fc7\u8fd9\u4e9b\u6392\u540d\uff08\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u884c\u6392\u5728\u7b2c 3 \u4f4d\uff0c\u5e76\u4e14\u4e0b\u4e00\u884c\u4e0e\u4e4b\u76f8\u7b49\uff0c\u90a3\u4e48\u4e0b\u4e00\u884c\u7684\u6392\u540d\u5c06\u662f\u7b2c 5 \u4f4d\uff09\u3002<\/li>\n<li>\u5bf9\u4e8e\u5177\u6709 NULL \u503c\u7684\u884c\uff0c\u5b83\u4eec\u7684\u6392\u540d\u5c06\u4e3a NULL\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5bf9\u5458\u5de5\u8868\u4e2d\u7684\u5458\u5de5\u6309\u5de5\u8d44\u8fdb\u884c\u6392\u540d\uff1a<\/p>\n<pre>SELECT employee_id, employee_name, salary,\nRANK() OVER (ORDER BY salary DESC) AS rank_by_salary\nFROM employees;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u7ed3\u679c<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>employee_id<\/th>\n<th>employee_name<\/th>\n<th>salary<\/th>\n<th>rank_by_salary<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>John Smith<\/td>\n<td>1000<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Jane Doe<\/td>\n<td>900<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>Mark Jones<\/td>\n<td>800<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>Mary Johnson<\/td>\n<td>700<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>David Green<\/td>\n<td>700<\/td>\n<td>4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cJohn Smith \u6392\u5728\u7b2c\u4e00\u4f4d\uff0c\u56e0\u4e3a\u4ed6\u7684\u5de5\u8d44\u6700\u9ad8\u3002Jane Doe \u6392\u5728\u7b2c\u4e8c\u4f4d\uff0c\u56e0\u4e3a\u5979\u7684\u5de5\u8d44\u4f4e\u4e8e John Smith\u3002Mark Jones \u6392\u5728\u7b2c\u4e09\u4f4d\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002\u6ce8\u610f\uff0cMary Johnson \u548c David Green \u7684\u5de5\u8d44\u76f8\u540c\uff0c\u56e0\u6b64\u4ed6\u4eec\u83b7\u5f97\u76f8\u540c\u7684\u6392\u540d\uff08\u7b2c 4 \u4f4d\uff09\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql&nbsp;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>sql rank() \u51fd\u6570\u53ef\u5bf9\u67e5\u8be2\u7ed3\u679c\u6309\u7ed9\u5b9a\u5217\u8fdb\u884c\u6392\u540d\uff0c\u8fd4\u56de\u6bcf\u4e2a\u884c\u7684\u76f8\u5bf9\u4f4d\u7f6e\u3002\u5b83\u53ef\u7528\u4e8e\u5206\u533a\u6392\u540d\uff0c\u5177\u6709\u91cd\u590d\u503c\u65f6\u8df3\u8fc7\u6392\u540d\uff0c\u5e76\u8fd4\u56de null \u503c\u884c\u7684 null \u6392\u540d\u3002 SQL RANK() \u51fd\u6570\u7528\u6cd5 SQL RANK() \u51fd\u6570\u7528\u4e8e\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u6392\u540d\uff0c\u53ef\u4ee5\u4e3a\u6bcf\u884c\u5206\u914d\u4e00\u4e2a\u6392\u540d\u503c\u3002\u6392\u540d\u503c\u8868\u793a\u8be5\u884c\u5728\u7ed3\u679c\u96c6\u4e2d\u7684\u76f8\u5bf9\u4f4d\u7f6e\u3002 \u8bed\u6cd5 RANK() OVER (PARTITION BY partition_expression ORDER BY order_expression) \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570 partition_expression (\u53ef\u9009)\uff1a\u5c06\u7ed3\u679c\u96c6\u5212\u5206\u4e3a\u4e0d\u540c\u7684\u5206\u533a\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5206\u533a\u5185\u7684\u884c\u8fdb\u884c\u6392\u540d\u3002\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5219\u5bf9\u6574\u4e2a\u7ed3\u679c\u96c6\u8fdb\u884c\u6392\u540d\u3002 order_expression\uff1a\u6307\u5b9a\u7528\u4e8e\u6392\u5e8f\u7684\u5217\uff0c\u6392\u540d\u5c06\u57fa\u4e8e\u6b64\u5217\u7684\u503c\u3002 \u529f\u80fd RANK() \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6570\u5b57\uff0c\u8868\u793a\u8be5\u884c\u5728\u7ed3\u679c\u96c6\u4e2d\u7684\u6392\u540d\u3002\u5b83\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a \u5177\u6709\u76f8\u540c\u503c\u7684\u884c\u5c06\u83b7\u5f97\u76f8\u540c\u7684\u6392\u540d\u3002 \u5982\u679c\u6709\u91cd\u590d\u7684\u884c\uff0c\u5b83\u4eec\u7684\u6392\u540d\u5c06\u76f8\u540c\uff0c\u4f46\u540e\u7eed\u884c\u5c06\u8df3\u8fc7\u8fd9\u4e9b\u6392\u540d\uff08\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u884c\u6392\u5728\u7b2c 3 \u4f4d\uff0c\u5e76\u4e14\u4e0b\u4e00\u884c\u4e0e\u4e4b\u76f8\u7b49\uff0c\u90a3\u4e48\u4e0b\u4e00\u884c\u7684\u6392\u540d\u5c06\u662f\u7b2c 5 \u4f4d\uff09\u3002 \u5bf9\u4e8e\u5177\u6709 NULL \u503c\u7684\u884c\uff0c\u5b83\u4eec\u7684\u6392\u540d\u5c06\u4e3a NULL\u3002 \u793a\u4f8b \u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5bf9\u5458\u5de5\u8868\u4e2d\u7684\u5458\u5de5\u6309\u5de5\u8d44\u8fdb\u884c\u6392\u540d\uff1a SELECT employee_id, employee_name, salary, RANK() OVER (ORDER BY salary DESC) AS rank_by_salary FROM [&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-5206","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5206","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=5206"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5206\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}