{"id":5207,"date":"2024-11-14T09:16:16","date_gmt":"2024-11-14T01:16:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5207\/"},"modified":"2024-11-14T09:16:16","modified_gmt":"2024-11-14T01:16:16","slug":"sql%e6%8e%92%e5%90%8d%e5%87%bd%e6%95%b0rank%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5207\/","title":{"rendered":"sql\u6392\u540d\u51fd\u6570rank\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  sql\u7684rank\u51fd\u6570\u901a\u8fc7\u5bf9\u8868\u4e2d\u7684\u6570\u636e\u6392\u5e8f\u5e76\u5206\u914d\u6392\u540d\uff0c\u6309\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6bcf\u4e00\u884c\u8fdb\u884c\u6392\u5e8f\u3002\u5b83\u7684\u8bed\u6cd5\u4e3arank() over (partition by partition_expression order by order_expression)\uff0c\u5176\u4e2dpartition_expression\u7528\u4e8e\u786e\u5b9a\u5206\u533a\uff0corder_expression\u7528\u4e8e\u6307\u5b9a\u6392\u5e8f\u8868\u8fbe\u5f0f\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/24\/2024052420453512296.jpg\" class=\"aligncenter\" title=\"sql\u6392\u540d\u51fd\u6570rank\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql\u6392\u540d\u51fd\u6570rank\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL\u6392\u540d\u51fd\u6570RANK\u306e\u4f7f\u3044\u65b9<\/strong><\/p>\n<p><strong>\u6982\u8981<\/strong><\/p>\n<p>RANK\u51fd\u6570\u662f\u4e00\u4e2aSQL\u6392\u540d\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u8868\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u5e76\u5206\u914d\u6392\u540d\u3002\u5b83\u8fd4\u56de\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6bcf\u4e00\u884c\u7684\u6392\u540d\uff0c\u4e0e\u884c\u4e2d\u7684\u5b9e\u9645\u503c\u65e0\u5173\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\uff1a<\/strong>\u5c06\u6570\u636e\u5212\u5206\u4e3a\u7ec4\u7684\u53ef\u9009\u8868\u8fbe\u5f0f\u3002\u5982\u679c\u7701\u7565\uff0c\u5219\u6574\u4e2a\u8868\u5c06\u88ab\u89c6\u4e3a\u4e00\u4e2a\u7ec4\u3002<\/li>\n<li> <strong>order_expression\uff1a<\/strong>\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u7684\u8868\u8fbe\u5f0f\u3002<\/li>\n<\/ul>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<ol>\n<li> <strong>\u786e\u5b9a\u5206\u533a\uff1a<\/strong>\u5982\u679c\u9700\u8981\u6309\u7ec4\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u540d\uff0c\u8bf7\u6307\u5b9a\u5206\u533a\u8868\u8fbe\u5f0f\u3002\u5426\u5219\uff0c\u7701\u7565\u8be5\u53c2\u6570\u3002<\/li>\n<li> <strong>\u6307\u5b9a\u6392\u5e8f\u8868\u8fbe\u5f0f\uff1a<\/strong>\u6307\u5b9a\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u7684\u8868\u8fbe\u5f0f\u3002\u8be5\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u6570\u503c\u5217\u3001\u65e5\u671f\u5217\u6216\u5176\u4ed6\u53ef\u6bd4\u8f83\u7684\u5217\u3002<\/li>\n<li> <strong>\u5e94\u7528RANK\u51fd\u6570\uff1a<\/strong>\u4f7f\u7528RANK\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u5e76\u5206\u914d\u6392\u540d\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p><strong>\u5458\u5de5\u8868<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u59d3\u540d<\/th>\n<th>\u90e8\u95e8<\/th>\n<th>\u9500\u552e\u989d<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u7ea6\u7ff0<\/td>\n<td>A<\/td>\n<td>100<\/td>\n<\/tr>\n<tr>\n<td>\u739b\u4e3d<\/td>\n<td>B<\/td>\n<td>200<\/td>\n<\/tr>\n<tr>\n<td>\u4fdd\u7f57<\/td>\n<td>A<\/td>\n<td>150<\/td>\n<\/tr>\n<tr>\n<td>\u82cf\u73ca<\/td>\n<td>B<\/td>\n<td>250<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u6309\u90e8\u95e8\u6392\u540d\uff1a<\/strong><\/p>\n<pre>SELECT \u59d3\u540d, \u90e8\u95e8, RANK() OVER (PARTITION BY \u90e8\u95e8 ORDER BY \u9500\u552e\u989d DESC) AS \u6392\u540d\nFROM \u5458\u5de5\u8868\nORDER BY \u6392\u540d;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u59d3\u540d<\/th>\n<th>\u90e8\u95e8<\/th>\n<th>\u6392\u540d<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u82cf\u73ca<\/td>\n<td>B<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>\u739b\u4e3d<\/td>\n<td>B<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>\u4fdd\u7f57<\/td>\n<td>A<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>\u7ea6\u7ff0<\/td>\n<td>A<\/td>\n<td>2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728\u8be5\u793a\u4f8b\u4e2d\uff0c\u6570\u636e\u6309\u90e8\u95e8\u8fdb\u884c\u5206\u533a\uff0c\u7136\u540e\u6309\u9500\u552e\u989d\u964d\u5e8f\u6392\u5e8f\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u90e8\u95e8\uff0cRANK\u51fd\u6570\u5206\u914d\u4e86\u552f\u4e00\u7684\u6392\u540d\uff0c\u4ece1\u5f00\u59cb\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u6392\u540d\u51fd\u6570rank\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\u7684rank\u51fd\u6570\u901a\u8fc7\u5bf9\u8868\u4e2d\u7684\u6570\u636e\u6392\u5e8f\u5e76\u5206\u914d\u6392\u540d\uff0c\u6309\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6bcf\u4e00\u884c\u8fdb\u884c\u6392\u5e8f\u3002\u5b83\u7684\u8bed\u6cd5\u4e3arank() over (partition by partition_expression order by order_expression)\uff0c\u5176\u4e2dpartition_expression\u7528\u4e8e\u786e\u5b9a\u5206\u533a\uff0corder_expression\u7528\u4e8e\u6307\u5b9a\u6392\u5e8f\u8868\u8fbe\u5f0f\u3002 SQL\u6392\u540d\u51fd\u6570RANK\u306e\u4f7f\u3044\u65b9 \u6982\u8981 RANK\u51fd\u6570\u662f\u4e00\u4e2aSQL\u6392\u540d\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u8868\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u5e76\u5206\u914d\u6392\u540d\u3002\u5b83\u8fd4\u56de\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6bcf\u4e00\u884c\u7684\u6392\u540d\uff0c\u4e0e\u884c\u4e2d\u7684\u5b9e\u9645\u503c\u65e0\u5173\u3002 \u8bed\u6cd5 RANK() OVER (PARTITION BY partition_expression ORDER BY order_expression) \u767b\u5f55\u540e\u590d\u5236 \u53c2\u6570 partition_expression\uff1a\u5c06\u6570\u636e\u5212\u5206\u4e3a\u7ec4\u7684\u53ef\u9009\u8868\u8fbe\u5f0f\u3002\u5982\u679c\u7701\u7565\uff0c\u5219\u6574\u4e2a\u8868\u5c06\u88ab\u89c6\u4e3a\u4e00\u4e2a\u7ec4\u3002 order_expression\uff1a\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u7684\u8868\u8fbe\u5f0f\u3002 \u7528\u6cd5 \u786e\u5b9a\u5206\u533a\uff1a\u5982\u679c\u9700\u8981\u6309\u7ec4\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u540d\uff0c\u8bf7\u6307\u5b9a\u5206\u533a\u8868\u8fbe\u5f0f\u3002\u5426\u5219\uff0c\u7701\u7565\u8be5\u53c2\u6570\u3002 \u6307\u5b9a\u6392\u5e8f\u8868\u8fbe\u5f0f\uff1a\u6307\u5b9a\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u7684\u8868\u8fbe\u5f0f\u3002\u8be5\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u6570\u503c\u5217\u3001\u65e5\u671f\u5217\u6216\u5176\u4ed6\u53ef\u6bd4\u8f83\u7684\u5217\u3002 \u5e94\u7528RANK\u51fd\u6570\uff1a\u4f7f\u7528RANK\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u5e76\u5206\u914d\u6392\u540d\u3002 \u793a\u4f8b \u5458\u5de5\u8868 \u59d3\u540d \u90e8\u95e8 \u9500\u552e\u989d \u7ea6\u7ff0 A 100 \u739b\u4e3d B 200 \u4fdd\u7f57 A 150 \u82cf\u73ca B 250 \u6309\u90e8\u95e8\u6392\u540d\uff1a SELECT \u59d3\u540d, \u90e8\u95e8, RANK() OVER (PARTITION BY \u90e8\u95e8 ORDER [&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-5207","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5207","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=5207"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5207\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}