{"id":191696,"date":"2024-11-12T17:04:19","date_gmt":"2024-11-12T09:04:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/191696\/"},"modified":"2024-11-12T17:04:20","modified_gmt":"2024-11-12T09:04:20","slug":"%e8%a9%b3%e8%a7%a3sql-server-2005%e5%9b%9b%e7%a8%ae%e6%8e%92%e5%90%8d%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/191696\/","title":{"rendered":"\u8a73\u89e3SQL Server 2005\u56db\u7a2e\u6392\u540d\u51fd\u6578"},"content":{"rendered":"<h1 id=\"%e8%a9%b3%e8%a7%a3sql-server-2005%e5%9b%9b%e7%a8%ae%e6%8e%92%e5%90%8d%e5%87%bd%e6%95%b8-tNVIQAJoid\">\u8a73\u89e3SQL Server 2005\u56db\u7a2e\u6392\u540d\u51fd\u6578<\/h1>\n<p>\u5728\u8cc7\u6599\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0c\u6392\u540d\u51fd\u6578\u662f\u4e00\u7a2e\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u5c0d\u67e5\u8a62\u7d50\u679c\u9032\u884c\u6392\u5e8f\u548c\u6392\u540d\u7684\u60c5\u6cc1\u4e0b\u3002SQL Server 2005\u5f15\u5165\u4e86\u56db\u7a2e\u4e3b\u8981\u7684\u6392\u540d\u51fd\u6578\uff1aROW_NUMBER()\u3001RANK()\u3001DENSE_RANK()\u548cNTILE()\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u9019\u56db\u7a2e\u6392\u540d\u51fd\u6578\u7684\u7528\u6cd5\u53ca\u5176\u7279\u9ede\u3002<\/p>\n<h2 id=\"1-row_number-tNVIQAJoid\">1. ROW_NUMBER()<\/h2>\n<p>ROW_NUMBER()\u51fd\u6578\u70ba\u67e5\u8a62\u7d50\u679c\u4e2d\u7684\u6bcf\u4e00\u884c\u5206\u914d\u4e00\u500b\u552f\u4e00\u7684\u6574\u6578\u503c\uff0c\u9019\u500b\u503c\u662f\u6839\u64da\u6307\u5b9a\u7684\u6392\u5e8f\u9806\u5e8f\u751f\u6210\u7684\u3002\u9019\u610f\u5473\u8457\u5373\u4f7f\u6709\u591a\u884c\u5177\u6709\u76f8\u540c\u7684\u503c\uff0cROW_NUMBER()\u4e5f\u6703\u70ba\u5b83\u5011\u5206\u914d\u4e0d\u540c\u7684\u5e8f\u865f\u3002<\/p>\n<pre><code>SELECT \n    ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RowNum, \n    EmployeeName, \n    Salary \nFROM \n    Employees;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6839\u64da\u85aa\u6c34\u7684\u964d\u5e8f\u6392\u5217\uff0c\u70ba\u6bcf\u4f4d\u54e1\u5de5\u5206\u914d\u4e86\u4e00\u500b\u884c\u865f\u3002\u9019\u5728\u9700\u8981\u7372\u53d6\u524dN\u540d\u8a18\u9304\u6642\u7279\u5225\u6709\u7528\u3002<\/p>\n<h2 id=\"2-rank-tNVIQAJoid\">2. RANK()<\/h2>\n<p>RANK()\u51fd\u6578\u4e5f\u70ba\u67e5\u8a62\u7d50\u679c\u4e2d\u7684\u6bcf\u4e00\u884c\u5206\u914d\u4e00\u500b\u6392\u540d\uff0c\u4f46\u5b83\u6703\u5c0d\u76f8\u540c\u7684\u503c\u7d66\u4e88\u76f8\u540c\u7684\u6392\u540d\uff0c\u4e26\u5728\u5f8c\u7e8c\u7684\u6392\u540d\u4e2d\u8df3\u904e\u76f8\u61c9\u7684\u6578\u5b57\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u5169\u500b\u54e1\u5de5\u7684\u85aa\u6c34\u76f8\u540c\u4e14\u6392\u540d\u7b2c\u4e00\uff0c\u90a3\u9ebc\u4e0b\u4e00\u500b\u54e1\u5de5\u7684\u6392\u540d\u5c07\u662f\u7b2c\u4e09\u3002<\/p>\n<pre><code>SELECT \n    RANK() OVER (ORDER BY Salary DESC) AS Rank, \n    EmployeeName, \n    Salary \nFROM \n    Employees;<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u6392\u540d\u65b9\u5f0f\u5728\u9700\u8981\u8003\u616e\u5e73\u5c40\u60c5\u6cc1\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h2 id=\"3-dense_rank-tNVIQAJoid\">3. DENSE_RANK()<\/h2>\n<p>DENSE_RANK()\u51fd\u6578\u8207RANK()\u985e\u4f3c\uff0c\u4f46\u5b83\u4e0d\u6703\u5728\u6392\u540d\u4e2d\u8df3\u904e\u6578\u5b57\u3002\u9019\u610f\u5473\u8457\u5982\u679c\u6709\u5169\u500b\u54e1\u5de5\u7684\u85aa\u6c34\u76f8\u540c\u4e14\u6392\u540d\u7b2c\u4e00\uff0c\u4e0b\u4e00\u500b\u54e1\u5de5\u7684\u6392\u540d\u4ecd\u7136\u662f\u7b2c\u4e8c\u3002<\/p>\n<pre><code>SELECT \n    DENSE_RANK() OVER (ORDER BY Salary DESC) AS DenseRank, \n    EmployeeName, \n    Salary \nFROM \n    Employees;<\/code><\/pre>\n<p>\u9019\u7a2e\u6392\u540d\u65b9\u5f0f\u9069\u5408\u9700\u8981\u9023\u7e8c\u6392\u540d\u7684\u60c5\u6cc1\uff0c\u7279\u5225\u662f\u5728\u5831\u544a\u548c\u7d71\u8a08\u5206\u6790\u4e2d\u3002<\/p>\n<h2 id=\"4-ntile-tNVIQAJoid\">4. NTILE()<\/h2>\n<p>NTILE()\u51fd\u6578\u5c07\u67e5\u8a62\u7d50\u679c\u5206\u6210\u6307\u5b9a\u6578\u91cf\u7684\u7d44\uff0c\u4e26\u70ba\u6bcf\u4e00\u884c\u5206\u914d\u4e00\u500b\u7d44\u865f\u3002\u9019\u5728\u9700\u8981\u5c07\u6578\u64da\u5206\u7d44\u4ee5\u9032\u884c\u5206\u6790\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT \n    NTILE(4) OVER (ORDER BY Salary DESC) AS Quartile, \n    EmployeeName, \n    Salary \nFROM \n    Employees;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6839\u64da\u85aa\u6c34\u7684\u964d\u5e8f\u5c07\u54e1\u5de5\u5206\u6210\u56db\u500b\u7d44\uff0c\u9019\u6a23\u53ef\u4ee5\u8f15\u9b06\u5730\u5206\u6790\u4e0d\u540c\u85aa\u6c34\u7bc4\u570d\u5167\u7684\u54e1\u5de5\u5206\u4f48\u60c5\u6cc1\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-tNVIQAJoid\">\u7e3d\u7d50<\/h2>\n<p>SQL Server 2005\u7684\u56db\u7a2e\u6392\u540d\u51fd\u6578\u5404\u6709\u5176\u7279\u9ede\u548c\u9069\u7528\u5834\u666f\u3002<strong>ROW_NUMBER()<\/strong>\u9069\u5408\u9700\u8981\u552f\u4e00\u884c\u865f\u7684\u60c5\u6cc1\uff0c<strong>RANK()<\/strong>\u548c<strong>DENSE_RANK()<\/strong>\u5247\u9069\u5408\u9700\u8981\u8003\u616e\u5e73\u5c40\u7684\u6392\u540d\uff0c\u800c<strong>NTILE()<\/strong>\u5247\u9069\u5408\u5c07\u6578\u64da\u5206\u7d44\u9032\u884c\u5206\u6790\u3002\u4e86\u89e3\u9019\u4e9b\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u8655\u7406\u548c\u5206\u6790\u6578\u64da\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc<em>VPS<\/em>\u3001<em>\u9999\u6e2fVPS<\/em>\u6216\u5176\u4ed6<em>\u4f3a\u670d\u5668<\/em>\u89e3\u6c7a\u65b9\u6848\u611f\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9 <a href=\"https:\/\/server.hk\">Server.HK<\/a> \u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u63a2\u8a0eSQL Server 2005\u7684\u56db\u7a2e\u6392\u540d\u51fd\u6578\uff0c\u4e86\u89e3\u5176\u7528\u6cd5\u3001\u7279\u6027\u53ca\u5be6\u969b\u61c9\u7528\uff0c\u63d0\u5347\u6578\u64da\u67e5\u8a62\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-191696","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/191696","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=191696"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/191696\/revisions"}],"predecessor-version":[{"id":191697,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/191696\/revisions\/191697"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=191696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=191696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=191696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}