{"id":1975,"date":"2020-08-20T23:53:53","date_gmt":"2020-08-20T14:53:53","guid":{"rendered":"https:\/\/obenkyolab.com\/?p=1975"},"modified":"2020-08-20T23:55:21","modified_gmt":"2020-08-20T14:55:21","slug":"%e3%80%90postgresql%e3%80%91%e3%82%b0%e3%83%ab%e3%83%bc%e3%83%97%e5%86%85%e3%81%a7%e3%81%ae%e9%a0%86%e4%bd%8d%e4%bb%98%e3%81%91%e3%82%92%e8%a1%8c%e3%81%86sql","status":"publish","type":"post","link":"https:\/\/obenkyolab.com\/?p=1975","title":{"rendered":"\u3010PostgreSQL\u3011\u30b0\u30eb\u30fc\u30d7\u5185\u3067\u306e\u9806\u4f4d\u4ed8\u3051\u3092\u884c\u3046SQL"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/obenkyolab.com\/?p=1975\/#%E7%9B%AE%E7%9A%84\" >\u76ee\u7684<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/obenkyolab.com\/?p=1975\/#%E3%83%86%E3%82%B9%E3%83%88%E3%83%87%E3%83%BC%E3%82%BF\" >\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/obenkyolab.com\/?p=1975\/#%E9%A0%86%E4%BD%8D%E4%BB%98%E3%81%91%E3%81%99%E3%82%8BSQL\" >\u9806\u4f4d\u4ed8\u3051\u3059\u308bSQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/obenkyolab.com\/?p=1975\/#%E7%89%B9%E5%AE%9A%E3%81%AE%E9%A0%86%E4%BD%8D%E3%81%A0%E3%81%91%E3%82%92%E6%8A%9C%E3%81%8D%E5%87%BA%E3%81%99SQL\" >\u7279\u5b9a\u306e\u9806\u4f4d\u3060\u3051\u3092\u629c\u304d\u51fa\u3059SQL<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%9B%AE%E7%9A%84\"><\/span>\u76ee\u7684<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4e00\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u8907\u6570\u306e\u30b0\u30eb\u30fc\u30d7\u304c\u3054\u3061\u3083\u6df7\u305c\u306b\u5165\u3063\u3066\u3044\u308b\u5834\u5408\u306b\u3001\u30b0\u30eb\u30fc\u30d7\u3054\u3068\u306b\u9806\u4f4d\u4ed8\u3051\u3092\u884c\u3046SQL\u3092\u30e1\u30e2\u308b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E3%83%86%E3%82%B9%E3%83%88%E3%83%87%E3%83%BC%E3%82%BF\"><\/span>\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30c7\u30fc\u30bf\u3092\u4f8b\u306b\u3059\u308b\u3002\u4e2d\u5b66\u751f\u306e\u30c6\u30b9\u30c8\u7d50\u679c\u306e\u3088\u3046\u306a\u30a4\u30e1\u30fc\u30b8\u3002<br>class\u304c\u30af\u30e9\u30b9\u3001socre\u306b\u70b9\u6570\u3001name\u304c\u6c0f\u540d\u3068\u3057\u305f\u3068\u304d\u3001\u30af\u30e9\u30b9\u6bce\u306b\u9806\u4f4d\u3092\u51fa\u3057\u305f\u3044\u3068\u3059\u308b\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n class | score |     name\n-------+-------+--------------\n D     |    58 | \u5927\u5bae\u6b63\u6625\n E     |    83 | \u5bcc\u6c38\u79c0\u96c4\n C     |    56 | \u5bae\u539f\u91cc\u83dc\n A     |    81 | \u79cb\u672c\u65e9\u767e\u5408\n A     |    55 | \u5c0f\u6fa4\u6804\u4f5c\n B     |    47 | \u5e73\u672c\u6843\u9999\n B     |    80 | \u524d\u5cf6\u5343\u7a42\n C     |    60 | \u5409\u91ce\u7f8e\u7d00\u5b50\n D     |    68 | \u5ca9\u672c\u82b3\u5b50\n E     |    96 | \u5927\u5e73\u88d5\u7f8e\n A     |    75 | \u5409\u5ca1\u8087\n E     |    32 | \u5897\u4e95\u516c\u4e00\n E     |    67 | \u7d30\u4e95\u5e78\u6a39\n D     |    78 | \u9577\u8c37\u90e8\u7ae0\u592a\u90ce\n B     |    68 | \u6c60\u672c\u5c1a\n C     |    94 | \u5ba4\u4e95\u7af9\u96c4\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E9%A0%86%E4%BD%8D%E4%BB%98%E3%81%91%E3%81%99%E3%82%8BSQL\"><\/span>\u9806\u4f4d\u4ed8\u3051\u3059\u308bSQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u306eSQL\u3067\u9806\u4f4d\u4ed8\u3051\u304c\u3067\u304d\u308b\u3002OVER\u53e5\u3092\u4f7f\u3063\u3066class\u6bce\u306b\u4e26\u3073\u66ff\u3048\u305f\u3046\u3048\u3067ROW_NUMBER()\u3067\u9806\u4f4d\u3092\u632f\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT \n    *\n    ,ROW_NUMBER() OVER (PARTITION BY class ORDER BY score DESC) AS ranking\nFROM results\n<\/pre><\/div>\n\n\n<p>\u7d50\u679c<\/p>\n\n\n\n<p>\u53f3\u7aef\u304c\u9806\u4f4d\u3067\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n class | score |     name     | ranking\n-------+-------+--------------+--------\n A     |    81 | \u79cb\u672c\u65e9\u767e\u5408   |      1\n A     |    75 | \u5409\u5ca1\u8087       |      2\n A     |    55 | \u5c0f\u6fa4\u6804\u4f5c     |      3\n B     |    80 | \u524d\u5cf6\u5343\u7a42     |      1\n B     |    68 | \u6c60\u672c\u5c1a       |      2\n B     |    47 | \u5e73\u672c\u6843\u9999     |      3\n C     |    94 | \u5ba4\u4e95\u7af9\u96c4     |      1\n C     |    60 | \u5409\u91ce\u7f8e\u7d00\u5b50   |      2\n C     |    56 | \u5bae\u539f\u91cc\u83dc     |      3\n D     |    78 | \u9577\u8c37\u90e8\u7ae0\u592a\u90ce |      1\n D     |    68 | \u5ca9\u672c\u82b3\u5b50     |      2\n D     |    58 | \u5927\u5bae\u6b63\u6625     |      3\n E     |    96 | \u5927\u5e73\u88d5\u7f8e     |      1\n E     |    83 | \u5bcc\u6c38\u79c0\u96c4     |      2\n E     |    67 | \u7d30\u4e95\u5e78\u6a39     |      3\n E     |    32 | \u5897\u4e95\u516c\u4e00     |      4\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%89%B9%E5%AE%9A%E3%81%AE%E9%A0%86%E4%BD%8D%E3%81%A0%E3%81%91%E3%82%92%E6%8A%9C%E3%81%8D%E5%87%BA%E3%81%99SQL\"><\/span>\u7279\u5b9a\u306e\u9806\u4f4d\u3060\u3051\u3092\u629c\u304d\u51fa\u3059SQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u5404\u30af\u30e9\u30b9\u306e\u7279\u5b9a\u306e\u9806\u4f4d\u306e\u4eba\u3060\u3051\u3092\u629c\u304d\u51fa\u3059\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306f\u5404\u30af\u30e9\u30b91\u756a\u306e\u4eba\u3060\u3051\u3092\u629c\u304d\u51fa\u3059\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSELECT\n\t*\nFROM\n\t(\n\tSELECT \n\t    *\n\t\t,ROW_NUMBER() OVER (PARTITION BY class ORDER BY score DESC) AS ranking\n\tFROM results\t\t\n\t) as T\nWHERE seqnum = 1\n<\/pre><\/div>\n\n\n<p>\u7d50\u679c<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n class | score |     name     | ranking\n-------+-------+--------------+--------\n A     |    81 | \u79cb\u672c\u65e9\u767e\u5408   |      1\n B     |    80 | \u524d\u5cf6\u5343\u7a42     |      1\n C     |    94 | \u5ba4\u4e95\u7af9\u96c4     |      1\n D     |    78 | \u9577\u8c37\u90e8\u7ae0\u592a\u90ce |      1\n E     |    96 | \u5927\u5e73\u88d5\u7f8e     |      1\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<p>\u77ed\u3044\u30b3\u30fc\u30c9\u3067\u3059\u304c\u3001\u7d50\u69cb\u4f7f\u3044\u52dd\u624b\u304c\u3044\u3044\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u7684 \u4e00\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u8907\u6570\u306e\u30b0\u30eb\u30fc\u30d7\u304c\u3054\u3061\u3083\u6df7\u305c\u306b\u5165\u3063\u3066\u3044\u308b\u5834\u5408\u306b\u3001&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1042,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/obenkyolab.com\/?p=1975","footnotes":""},"categories":[12],"tags":[],"class_list":["post-1975","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql","ja"],"_links":{"self":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/1975","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1975"}],"version-history":[{"count":2,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/1975\/revisions"}],"predecessor-version":[{"id":1977,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/1975\/revisions\/1977"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/media\/1042"}],"wp:attachment":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}