{"id":939,"date":"2019-12-04T23:01:08","date_gmt":"2019-12-04T14:01:08","guid":{"rendered":"https:\/\/obenkyolab.com\/?p=939"},"modified":"2020-05-19T22:50:45","modified_gmt":"2020-05-19T13:50:45","slug":"postgresql-%e5%85%a5%e5%87%ba%e9%87%91%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%8b%e3%82%89%e6%9c%88%e7%b7%a0%e3%82%81%e9%9b%86%e8%a8%88%e3%83%93%e3%83%a5%e3%83%bc%e4%bd%9c%e6%88%90","status":"publish","type":"post","link":"https:\/\/obenkyolab.com\/?p=939","title":{"rendered":"PostgreSQL \u5165\u51fa\u91d1\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u6708\u7de0\u3081\u96c6\u8a08\u30d3\u30e5\u30fc\u4f5c\u6210"},"content":{"rendered":"\n<p>\u5165\u51fa\u91d1\u60c5\u5831\u304c\u5165\u3063\u305f\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u6708\u7de0\u3081\u306e\u96c6\u8a08\u30d3\u30e5\u30fc\u3092\u4f5c\u308b\u65b9\u6cd5\u3092\u8a66\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\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=939\/#%E5%85%83%E3%83%87%E3%83%BC%E3%82%BF\" >\u5143\u30c7\u30fc\u30bf<\/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=939\/#%E4%B8%80%E6%AC%A1%E5%87%A6%E7%90%86\" >\u4e00\u6b21\u51e6\u7406<\/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=939\/#%E4%BA%8C%E6%AC%A1%E5%87%A6%E7%90%86\" >\u4e8c\u6b21\u51e6\u7406<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%85%83%E3%83%87%E3%83%BC%E3%82%BF\"><\/span>\u5143\u30c7\u30fc\u30bf<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u30c7\u30fc\u30bf\u3092\u5bfe\u8c61\u306b\u3057\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nselect * from account;\n\n#\u7d50\u679c\n\n   dates    | in_amount | out_amount | balance\n------------+-----------+------------+---------\n 2019-10-01 |       100 |            |   10100\n 2019-10-02 |           |       1000 |    9100\n 2019-10-02 |      3000 |            |   12100\n 2019-10-02 |     10000 |            |   22100\n 2019-10-20 |           |       1000 |   21100\n 2019-10-24 |      2000 |            |   23100\n 2019-11-03 |           |       3000 |   20100\n 2019-11-06 |           |       4000 |   16100\n 2019-11-15 |     30000 |            |   46100\n 2019-11-15 |           |       2000 |   44100\n 2019-11-25 |           |        100 |   44000\n 2019-11-26 |      3000 |            |   47000\n 2019-11-27 |           |      25000 |   22000\n 2019-11-28 |      1000 |            |   23000\n(14 \u884c)\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E4%B8%80%E6%AC%A1%E5%87%A6%E7%90%86\"><\/span>\u4e00\u6b21\u51e6\u7406<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>row_number() over()\u3068\u5e74\u6708\u3092\u62bd\u51fa\u3057\u305f\u884c\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nselect\n\trow_number() over()\n\t,to_char(dates,&#039;yyyymm&#039;) as year_month\n\t,*\nfrom\n\taccount\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 row_number | year_month |   dates    | in_amount | out_amount | balance\n------------+------------+------------+-----------+------------+---------\n          1 | 201910     | 2019-10-01 |       100 |            |   10100\n          2 | 201910     | 2019-10-02 |           |       1000 |    9100\n          3 | 201910     | 2019-10-02 |      3000 |            |   12100\n          4 | 201910     | 2019-10-02 |     10000 |            |   22100\n          5 | 201910     | 2019-10-20 |           |       1000 |   21100\n          6 | 201910     | 2019-10-24 |      2000 |            |   23100\n          7 | 201911     | 2019-11-03 |           |       3000 |   20100\n          8 | 201911     | 2019-11-06 |           |       4000 |   16100\n          9 | 201911     | 2019-11-15 |     30000 |            |   46100\n         10 | 201911     | 2019-11-15 |           |       2000 |   44100\n         11 | 201911     | 2019-11-25 |           |        100 |   44000\n         12 | 201911     | 2019-11-26 |      3000 |            |   47000\n         13 | 201911     | 2019-11-27 |           |      25000 |   22000\n         14 | 201911     | 2019-11-28 |      1000 |            |   23000\n(14 \u884c)\n\n   \n\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E4%BA%8C%E6%AC%A1%E5%87%A6%E7%90%86\"><\/span>\u4e8c\u6b21\u51e6\u7406<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u4e00\u6b21\u51e6\u7406\u3067\u5f97\u3089\u308c\u305fyear_month\u3054\u3068\u306bgroup_by\u3057\u3001row_number\u304c\u6700\u5927\u306e\u30ec\u30b3\u30fc\u30c9\u3060\u3051\u6b8b\u3059\u30af\u30a8\u30ea\u3068\u4e00\u6b21\u51e6\u7406\u306e\u7d50\u679c\u3092\u7d50\u5408\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>SQL\u3068\u3057\u3066\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nselect\n\tt1.year_month\n\t,dates\n\t,balance\nfrom\n\t(\n\tselect\n\t\tmax(row_number) as row_number\n\t\t,year_month\n\tfrom\n\t\t(\n\t\tselect\n\t\t\trow_number() over() as row_number\n\t\t\t,to_char(dates,&#039;yyyymm&#039;) as year_month\n\t\t\t,*\n\t\tfrom\n\t\t\taccount\n\t\t) as t0\n\tgroup by year_month\n\t) as t1\nleft join\n\t(\n\tselect\n\trow_number() over() as row_number\n\t,to_char(dates,&#039;yyyymm&#039;) as year_month\n\t,*\nfrom\n\taccount\n\t) as t2\nusing(row_number)\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<p>\u7d50\u679c<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n year_month |   dates    | balance\n------------+------------+---------\n 201910     | 2019-10-24 |   23100\n 201911     | 2019-11-28 |   23000\n(2 \u884c)\n\n<\/pre><\/div>\n\n\n<p>\u6708\u672b\u306e\u5024\u3060\u3051\u53d6\u308a\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5165\u51fa\u91d1\u60c5\u5831\u304c\u5165\u3063\u305f\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u6708\u7de0\u3081\u306e\u96c6\u8a08\u30d3\u30e5\u30fc\u3092\u4f5c\u308b\u65b9\u6cd5\u3092\u8a66\u3057\u307e\u3059&#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=939","footnotes":""},"categories":[12],"tags":[34,32],"class_list":["post-939","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql","tag-sql","tag-32","ja"],"_links":{"self":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/939","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=939"}],"version-history":[{"count":5,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/939\/revisions"}],"predecessor-version":[{"id":944,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/939\/revisions\/944"}],"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=939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}