{"id":1569,"date":"2020-06-01T21:55:13","date_gmt":"2020-06-01T12:55:13","guid":{"rendered":"https:\/\/obenkyolab.com\/?p=1569"},"modified":"2020-06-01T21:55:13","modified_gmt":"2020-06-01T12:55:13","slug":"postgresql-%e7%b4%af%e7%a9%8d%e5%80%a4%e3%82%92%e8%a8%88%e7%ae%97%e3%81%99%e3%82%8bsql","status":"publish","type":"post","link":"https:\/\/obenkyolab.com\/?p=1569","title":{"rendered":"PostgreSQL \u7d2f\u7a4d\u5024\u3092\u8a08\u7b97\u3059\u308bSQL"},"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=1569\/#%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=1569\/#Step1_%E3%83%86%E3%83%BC%E3%83%96%E3%83%AB%E7%94%A8%E6%84%8F\" >Step1 : \u30c6\u30fc\u30d6\u30eb\u7528\u610f<\/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=1569\/#Step2_%E7%B4%AF%E7%A9%8D%E5%80%A4%E3%82%92%E8%A8%88%E7%AE%97%E3%81%99%E3%82%8BSQL\" >Step2 : \u7d2f\u7a4d\u5024\u3092\u8a08\u7b97\u3059\u308bSQL<\/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>\u3042\u308b\u5024\u306e\u7d2f\u7a4d\u5024\u3092SQL\u3067\u8a08\u7b97\u3059\u308b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step1_%E3%83%86%E3%83%BC%E3%83%96%E3%83%AB%E7%94%A8%E6%84%8F\"><\/span>Step1 : \u30c6\u30fc\u30d6\u30eb\u7528\u610f<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30c6\u30fc\u30d6\u30eb\u3092\u7528\u610f\u3057\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nCREATE TABLE test_accumulation(\n\u5e74 int\n,\u6708 int\n,\u5024 int\n)\n<\/pre><\/div>\n\n\n<p>\u3053\u306e\u5e74\u3001\u6708\u3001\u5024\u3068\u3044\u3046\u30ab\u30e9\u30e0\u306b\u305d\u308c\u305e\u308c\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u5165\u308c\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n  \u5e74  | \u6708 | \u5024\n------+----+----\n 2019 |  4 |  5\n 2019 |  5 | 13\n 2019 |  6 |  5\n 2019 |  7 |  9\n 2019 |  8 | 15\n 2019 |  9 | 24\n 2019 | 10 |  5\n 2019 | 11 | 32\n 2019 | 12 |  9\n 2020 |  1 | 13\n 2020 |  2 | 24\n 2020 |  3 | 19\n(12 \u884c)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step2_%E7%B4%AF%E7%A9%8D%E5%80%A4%E3%82%92%E8%A8%88%E7%AE%97%E3%81%99%E3%82%8BSQL\"><\/span>Step2 : \u7d2f\u7a4d\u5024\u3092\u8a08\u7b97\u3059\u308bSQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4e0a\u8a18\u306e\u5024\u306e\u6a2a\u306b\u3001\u305d\u306e\u5e74\u3001\u6708\u307e\u3067\u306e\u7d2f\u7a4d\u5024\u304c\u8a08\u7b97\u3055\u308c\u305f\u30ab\u30e9\u30e0\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002\u3053\u308c\u306fOVER\u53e5\u3092\u4f7f\u7528\u3057\u305f\u4ee5\u4e0b\u306e\u3088\u3046\u306aSQL\u3067\u7c21\u5358\u306b\u5b9f\u73fe\u53ef\u80fd\u3067\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; highlight: [5]; title: ; notranslate\" title=\"\">\nSELECT\n\t\u5e74\n\t,\u6708\n\t,\u5024\n\t,SUM(\u5024) OVER(ORDER BY \u5e74,\u6708) AS \u7d2f\u7a4d\u5024\nFROM\n\ttest_accumulation\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  \u5e74  | \u6708 | \u5024 | \u7d2f\u7a4d\u5024\n------+----+----+--------\n 2019 |  4 |  5 |      5\n 2019 |  5 | 13 |     18\n 2019 |  6 |  5 |     23\n 2019 |  7 |  9 |     32\n 2019 |  8 | 15 |     47\n 2019 |  9 | 24 |     71\n 2019 | 10 |  5 |     76\n 2019 | 11 | 32 |    108\n 2019 | 12 |  9 |    117\n 2020 |  1 | 13 |    130\n 2020 |  2 | 24 |    154\n 2020 |  3 | 19 |    173\n<\/pre><\/div>\n\n\n<p>OVER\u53e5\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u8a72\u5f53\u3059\u308b\u884c\u307e\u3067\u306e\u9593\u304cSUM\u3055\u308c\u308b\u306e\u3067\u3001\u7d50\u679c\u3068\u3057\u3066\u7d2f\u7a4d\u5024\u304c\u5f97\u3089\u308c\u307e\u3059\u3002\u30dd\u30a4\u30f3\u30c8\u3068\u3057\u3066\u306f\u3001\u7d2f\u7a4d\u5024\u3092\u6c42\u3081\u308b\u554f\u984c\u306e\u5834\u5408\u3001\u9806\u756a\u304c\u91cd\u8981\u306b\u306a\u308b\u3053\u3068\u304c\u591a\u3044\u306f\u305a\u3067\u3059\u306e\u3067\u3001OVER\u53e5\u5185\u306bORDER BY\u3067\u30bd\u30fc\u30c8\u3057\u3066\u304a\u304f\u3053\u3068\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u7684 \u3042\u308b\u5024\u306e\u7d2f\u7a4d\u5024\u3092SQL\u3067\u8a08\u7b97\u3059\u308b\u3002 Step1 : \u30c6\u30fc\u30d6\u30eb\u7528&#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=1569","footnotes":""},"categories":[12],"tags":[],"class_list":["post-1569","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\/1569","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=1569"}],"version-history":[{"count":1,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/1569\/revisions"}],"predecessor-version":[{"id":1570,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/1569\/revisions\/1570"}],"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=1569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}