{"id":1216,"date":"2019-06-19T09:01:12","date_gmt":"2019-06-19T01:01:12","guid":{"rendered":"http:\/\/www.hefeiyu.com\/?p=1216"},"modified":"2019-06-19T09:01:12","modified_gmt":"2019-06-19T01:01:12","slug":"tiptop-%e5%8f%96%e6%9c%80%e8%bf%91%e9%87%87%e8%b4%ad%e5%8d%95%e4%bb%b7sql%e8%a7%a3%e9%87%8a","status":"publish","type":"post","link":"https:\/\/www.hefeiyu.com\/?p=1216","title":{"rendered":"TIPTOP \u53d6\u6700\u8fd1\u91c7\u8d2d\u5355\u4ef7SQL\u89e3\u91ca"},"content":{"rendered":"<p>SELECT Max(pmn31) Keep (dense_rank First Order By pmm04 ASC) price_lastt,<br \/>\nMax(pmn31t) Keep (dense_rank First Order By pmm04 ASC) price_last<br \/>\nFROM pmn_file,pmm_file WHERE pmm01=pmn01 AND pmm09=? AND pmn04=? GROUP By pmm09,pmn04<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 18pt;\"><strong>\u9644\uff1aOracle\u4e2drow_number()\u3001rank()\u3001dense_rank() \u7684\u533a\u522b<\/strong><\/span><\/p>\n<p><strong>row_numbe<\/strong>r\u7684\u7528\u9014\u975e\u5e38\u5e7f\u6cdb\uff0c\u6392\u5e8f\u6700\u597d\u7528\u5b83\uff0c\u5b83\u4f1a\u4e3a\u67e5\u8be2\u51fa\u6765\u7684\u6bcf\u4e00\u884c\u8bb0\u5f55\u751f\u6210\u4e00\u4e2a\u5e8f\u53f7\uff0c\u4f9d\u6b21\u6392\u5e8f\u4e14\u4e0d\u4f1a\u91cd\u590d\uff0c\u6ce8\u610f\u4f7f\u7528row_number\u51fd\u6570\u65f6\u5fc5\u987b\u8981\u7528over\u5b50\u53e5\u9009\u62e9\u5bf9\u67d0\u4e00\u5217\u8fdb\u884c\u6392\u5e8f\u624d\u80fd\u751f\u6210\u5e8f\u53f7\u3002<\/p>\n<p><strong>rank<\/strong>\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u7ed3\u679c\u96c6\u7684\u5206\u533a\u5185\u6bcf\u884c\u7684\u6392\u540d\uff0c\u884c\u7684\u6392\u540d\u662f\u76f8\u5173\u884c\u4e4b\u524d\u7684\u6392\u540d\u6570\u52a0\u4e00\u3002\u7b80\u5355\u6765\u8bf4rank\u51fd\u6570\u5c31\u662f\u5bf9\u67e5\u8be2\u51fa\u6765\u7684\u8bb0\u5f55\u8fdb\u884c\u6392\u540d\uff0c\u4e0erow_number\u51fd\u6570\u4e0d\u540c\u7684\u662f\uff0crank\u51fd\u6570\u8003\u8651\u5230\u4e86over\u5b50\u53e5\u4e2d\u6392\u5e8f\u5b57\u6bb5\u503c\u76f8\u540c\u7684\u60c5\u51b5\uff0c\u5982\u679c\u4f7f\u7528rank\u51fd\u6570\u6765\u751f\u6210\u5e8f\u53f7\uff0cover\u5b50\u53e5\u4e2d\u6392\u5e8f\u5b57\u6bb5\u503c\u76f8\u540c\u7684\u5e8f\u53f7\u662f\u4e00\u6837\u7684\uff0c\u540e\u9762\u5b57\u6bb5\u503c\u4e0d\u76f8\u540c\u7684\u5e8f\u53f7\u5c06\u8df3\u8fc7\u76f8\u540c\u7684\u6392\u540d\u53f7\u6392\u4e0b\u4e00\u4e2a\uff0c\u4e5f\u5c31\u662f\u76f8\u5173\u884c\u4e4b\u524d\u7684\u6392\u540d\u6570\u52a0\u4e00\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u6839\u636e\u5f53\u524d\u7684\u8bb0\u5f55\u6570\u751f\u6210\u5e8f\u53f7\uff0c\u540e\u9762\u7684\u8bb0\u5f55\u4f9d\u6b64\u7c7b\u63a8\u3002<\/p>\n<p><strong>dense_rank<\/strong>\u51fd\u6570\u7684\u529f\u80fd\u4e0erank\u51fd\u6570\u7c7b\u4f3c\uff0cdense_rank\u51fd\u6570\u5728\u751f\u6210\u5e8f\u53f7\u65f6\u662f\u8fde\u7eed\u7684\uff0c\u800crank\u51fd\u6570\u751f\u6210\u7684\u5e8f\u53f7\u6709\u53ef\u80fd\u4e0d\u8fde\u7eed\u3002dense_rank\u51fd\u6570\u51fa\u73b0\u76f8\u540c\u6392\u540d\u65f6\uff0c\u5c06\u4e0d\u8df3\u8fc7\u76f8\u540c\u6392\u540d\u53f7\uff0crank\u503c\u7d27\u63a5\u4e0a\u4e00\u6b21\u7684rank\u503c\u3002\u5728\u5404\u4e2a\u5206\u7ec4\u5185\uff0crank()\u662f\u8df3\u8dc3\u6392\u5e8f\uff0c\u6709\u4e24\u4e2a\u7b2c\u4e00\u540d\u65f6\u63a5\u4e0b\u6765\u5c31\u662f\u7b2c\u4e09\u540d\uff0cdense_rank()\u662f\u8fde\u7eed\u6392\u5e8f\uff0c\u6709\u4e24\u4e2a\u7b2c\u4e00\u540d\u65f6\u4ecd\u7136\u8ddf\u7740\u7b2c\u4e8c\u540d\u3002<\/p>\n<p>\u501f\u52a9\u5b9e\u4f8b\u80fd\u66f4\u76f4\u89c2\u5730\u7406\u89e3\uff1a<\/p>\n<p>\u5047\u8bbe\u73b0\u5728\u6709\u4e00\u5f20\u5b66\u751f\u8868student\uff0c\u5b66\u751f\u8868\u4e2d\u6709\u59d3\u540d\u3001\u5206\u6570\u3001\u8bfe\u7a0b\u7f16\u53f7\u3002<\/p>\n<p>select * from student;<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.hefeiyu.com\/wp-content\/uploads\/2019\/06\/356ea6b0aafa897a4d1b44c77c38ad7a.png\" \/><\/p>\n<p><strong>\u73b0\u5728\u9700\u8981\u6309\u7167\u8bfe\u7a0b\u5bf9\u5b66\u751f\u7684\u6210\u7ee9\u8fdb\u884c\u6392\u5e8f\uff1a<\/strong><\/p>\n<p>&#8211;row_number() \u987a\u5e8f\u6392\u5e8f<\/p>\n<p>select name,course,<strong>row_number()<\/strong> over(partition by course order by score desc) rank from student;<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.hefeiyu.com\/wp-content\/uploads\/2019\/06\/3d289308dd874d46b6d7e11440b3f635.png\" \/><\/p>\n<p>&#8211;rank() \u8df3\u8dc3\u6392\u5e8f\uff0c\u5982\u679c\u6709\u4e24\u4e2a\u7b2c\u4e00\u7ea7\u522b\u65f6\uff0c\u63a5\u4e0b\u6765\u662f\u7b2c\u4e09\u7ea7\u522b<\/p>\n<p>select name,course,<strong>rank()<\/strong> over(partition by course order by score desc) rank from student;<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.hefeiyu.com\/wp-content\/uploads\/2019\/06\/746a8605ccd8fa19737244fbb80dd94f.png\" \/><\/p>\n<p>&#8211;dense_rank() \u8fde\u7eed\u6392\u5e8f\uff0c\u5982\u679c\u6709\u4e24\u4e2a\u7b2c\u4e00\u7ea7\u522b\u65f6\uff0c\u63a5\u4e0b\u6765\u662f\u7b2c\u4e8c\u7ea7\u522b<\/p>\n<p>select name,course,<strong>dense_rank()<\/strong> over(partition by course order by score desc) rank from student;<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.hefeiyu.com\/wp-content\/uploads\/2019\/06\/16bc07c42d2401e131291228f88a9fcd.png\" \/><\/p>\n<p><strong>\u53d6\u5f97\u6bcf\u95e8\u8bfe\u7a0b\u7684\u7b2c\u4e00\u540d\uff1a<\/strong><\/p>\n<p>&#8211;\u6bcf\u95e8\u8bfe\u7a0b\u7b2c\u4e00\u540d\u53ea\u53d6\u4e00\u4e2a\uff1a<\/p>\n<p>select * from (select name,course,<strong>row_number()<\/strong> over(partition by course order by score desc) rank from student) where rank=<strong>1<\/strong>;<\/p>\n<p>&#8211;\u6bcf\u95e8\u8bfe\u7a0b\u7b2c\u4e00\u540d\u53d6\u6240\u6709\uff1a<\/p>\n<p>select * from (select name,course,<strong>dense_rank()<\/strong> over(partition by course order by score desc) rank from student) where rank=<strong>1<\/strong>;<\/p>\n<p>&#8211;\u6bcf\u95e8\u8bfe\u7a0b\u7b2c\u4e00\u540d\u53d6\u6240\u6709\uff1a<\/p>\n<p>select * from (select name,course,<strong>rank()<\/strong> over(partition by course order by score desc) rank from student) where rank=<strong>1<\/strong>;<\/p>\n<p>\u9644\uff1a\u6bcf\u95e8\u8bfe\u7a0b\u7b2c\u4e00\u540d\u53d6\u6240\u6709\u7684\u5176\u4ed6\u65b9\u6cd5\uff08\u4f7f\u7528group by \u800c\u4e0d\u662fpartition by\uff09\uff1a<\/p>\n<p>select <strong>s.<\/strong><strong>*<\/strong> from student s<\/p>\n<p>inner join(select course,max(score) as score from student group by course) c<\/p>\n<p>on s.course=c.course and s.score=c.score;<\/p>\n<p>&#8211;\u6216\u8005\u4f7f\u7528using\u5173\u952e\u5b57\u7b80\u5316\u8fde\u63a5<\/p>\n<p>select <strong>*<\/strong> from student s<\/p>\n<p>inner join(select course,max(score) as score from student group by course) c<\/p>\n<p><strong>using<\/strong>(course,score);<\/p>\n<p><strong>\u5173\u4e8e<\/strong><strong>Parttion\u00a0by<\/strong><strong>\uff1a<\/strong><\/p>\n<p>Parttion\u00a0by\u5173\u952e\u5b57\u662fOracle\u4e2d\u5206\u6790\u6027\u51fd\u6570\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u7ed9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u533a\u3002\u5b83\u548c\u805a\u5408\u51fd\u6570Group by\u4e0d\u540c\u7684\u5730\u65b9\u5728\u4e8e\u5b83\u53ea\u662f\u5c06\u539f\u59cb\u6570\u636e\u8fdb\u884c\u540d\u6b21\u6392\u5217\uff0c\u80fd\u591f\u8fd4\u56de\u4e00\u4e2a\u5206\u7ec4\u4e2d\u7684\u591a\u6761\u8bb0\u5f55\uff08\u8bb0\u5f55\u6570\u4e0d\u53d8\uff09\uff0c\u800cGroup by\u662f\u5bf9\u539f\u59cb\u6570\u636e\u8fdb\u884c\u805a\u5408\u7edf\u8ba1\uff0c\u4e00\u822c\u53ea\u6709\u4e00\u6761\u53cd\u6620\u7edf\u8ba1\u503c\u7684\u7ed3\u679c\uff08\u6bcf\u7ec4\u8fd4\u56de\u4e00\u6761\uff09\u3002<\/p>\n<p>TIPS\uff1a<\/p>\n<p>\u4f7f\u7528rank over()\u7684\u65f6\u5019\uff0c\u7a7a\u503c\u662f\u6700\u5927\u7684\uff0c\u5982\u679c\u6392\u5e8f\u5b57\u6bb5\u4e3anull, \u53ef\u80fd\u9020\u6210null\u5b57\u6bb5\u6392\u5728\u6700\u524d\u9762\uff0c\u5f71\u54cd\u6392\u5e8f\u7ed3\u679c\u3002<\/p>\n<p>\u53ef\u4ee5\u8fd9\u6837\uff1a rank over(partition by course order by score desc nulls last)<\/p>\n<p><strong>\u603b\u7ed3\uff1a<\/strong><\/p>\n<p>\u5728\u4f7f\u7528\u6392\u540d\u51fd\u6570\u7684\u65f6\u5019\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e09\u70b9\uff1a<\/p>\n<p>1\u3001\u6392\u540d\u51fd\u6570\u5fc5\u987b\u6709\u00a0OVER\u00a0\u5b50\u53e5\u3002<\/p>\n<p>2\u3001\u6392\u540d\u51fd\u6570\u5fc5\u987b\u6709\u5305\u542b ORDER BY \u7684 OVER \u5b50\u53e5\u3002<\/p>\n<p>3\u3001\u5206\u7ec4\u5185\u4ece1\u5f00\u59cb\u6392\u5e8f\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.hefeiyu.com\">\u8d6b\u975e\u57df<\/a> &raquo; <a href=\"https:\/\/www.hefeiyu.com\/?p=1216\">TIPTOP \u53d6\u6700\u8fd1\u91c7\u8d2d\u5355\u4ef7SQL\u89e3\u91ca<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>SELECT Max(pmn31) Keep (dense_rank First Order By pmm04 ASC) price_lastt, Max(pmn31t) Keep (dense_rank First Order By pmm04 A [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[426,427,425],"class_list":["post-1216","post","type-post","status-publish","format-standard","hentry","category-unix","tag-dense","tag-max","tag-rank"],"_links":{"self":[{"href":"https:\/\/www.hefeiyu.com\/index.php?rest_route=\/wp\/v2\/posts\/1216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hefeiyu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hefeiyu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hefeiyu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hefeiyu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1216"}],"version-history":[{"count":1,"href":"https:\/\/www.hefeiyu.com\/index.php?rest_route=\/wp\/v2\/posts\/1216\/revisions"}],"predecessor-version":[{"id":1217,"href":"https:\/\/www.hefeiyu.com\/index.php?rest_route=\/wp\/v2\/posts\/1216\/revisions\/1217"}],"wp:attachment":[{"href":"https:\/\/www.hefeiyu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hefeiyu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hefeiyu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}