{"id":1784,"date":"2020-07-31T22:04:19","date_gmt":"2020-07-31T13:04:19","guid":{"rendered":"https:\/\/esoro.jp\/?p=1784"},"modified":"2020-07-31T22:04:19","modified_gmt":"2020-07-31T13:04:19","slug":"postgres%e3%81%a7%e5%b1%a5%e6%ad%b4%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%8b%e3%82%89%ef%bc%91%e6%99%82%e9%96%93%e6%af%8e%e3%81%ae%e9%9b%86%e8%a8%88%e3%82%92sql%e3%81%a7%e5%8f%96%e5%be%97%e3%81%99","status":"publish","type":"post","link":"https:\/\/esoro.jp\/?p=1784","title":{"rendered":"postgres\u3067\u5c65\u6b74\u30c6\u30fc\u30d6\u30eb\u304b\u3089\uff11\u6642\u9593\u6bce\u306e\u96c6\u8a08\u3092SQL\u3067\u53d6\u5f97\u3059\u308b"},"content":{"rendered":"<p>\u3000metabase\u3067postgres\u30c7\u30fc\u30bf\u3092\u53ef\u8996\u5316\u3059\u308b\u3088\u3046\u306b\u3057\u305f\u6240\u3001\u5c65\u6b74\u30c7\u30fc\u30bf\u304c\u683c\u7d0d\u3055\u308c\u3066\u3044\u308b\u30c6\u30fc\u30d6\u30eb\u304b\u3089\uff11\u6642\u9593\u6bce\u306e\u96c6\u8a08\u3092\u53d6\u308a\u305f\u3044\u3063\u3066\u4e8b\u3067\u3001SQL1\u767a\u3067\u51fa\u6765\u308b\u3093\u304b\u3044\u306a\u3068\u8a66\u884c\u932f\u8aa4\u3002<\/p>\n<p>\u3000\u4eca\u56de\u306e\u5c65\u6b74\u30c6\u30fc\u30d6\u30eb\u306ftimestamp\u578b\u3067\u958b\u59cb\u6642\u9593\u3068\u7d42\u4e86\u6642\u9593\u304c\u30bb\u30c3\u30c8\u3055\u308c\u3066\u3044\u3066\u3001\u8ab0\u304c\u4f55\u3092\u3044\u3064\u304b\u3089\u3044\u3064\u307e\u3067\u5b9f\u65bd\u3057\u3066\u3044\u305f\u304b\u304c\u8a18\u9332\u3055\u308c\u3066\u3044\u308b\u3001\u3088\u3046\u306a\u5f62\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u3092\uff11\u6642\u9593\u5358\u4f4d\u3067\u305d\u306e\u6642\u9593\u5e2f\u306b\u3069\u306e\u304f\u3089\u3044\u5b9f\u65bd\u3057\u3066\u3044\u305f\u304b\u3092\u30b5\u30de\u30ea\u305f\u3044\u3068\u3044\u3046\u5185\u5bb9\u3067\u3059\u3002<\/p>\n<p>\u5177\u4f53\u7684\u306b\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u30c7\u30fc\u30bf\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>selet * from xx_history ;<\/p>\n<p><code>id, start_time, end_time, user_id, task_id<br \/>\n10, 2020-07-27 16:15:23, 2020-07-27 18:17:43, 432, 2<br \/>\n11, 2020-07-27 18:43:54, 2020-07-27 19:02:04, 154, 4<br \/>\n12, 2020-07-27 19:14:52, 2020-07-27 19:45:21, 432, 5<br \/>\n13, 2020-07-27 19:02:15, 2020-07-27 19:42:53, 154, 4<\/code><\/p>\n<p>\u3000\u3053\u308c\u3092\u4e0b\u8a18\u306e\u3088\u3046\u306b\uff11\u6642\u9593\u6bce\u306b\u6642\u9593\u3092\u5408\u8a08\u3059\u308b\u3088\u3046\u306a\u5f62\u3067\u62bd\u51fa\u3057\u305f\u3044\u3068\u3044\u3046\u4e8b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><code>\u6642\u9593\u7bc4\u56f2\uff08XX\u6642\u53f0\uff09, \u5408\u8a08\u6642\u9593<br \/>\n2020-07-27 16:00, 00:44:37<br \/>\n2020-07-27 17:00, 01:00:00<br \/>\n2020-07-27 18:00, 00:33:49<br \/>\n2020-07-27 19:00, 01:13:11<\/code><\/p>\n<p>\u3000\u5c65\u6b74\u30c6\u30fc\u30d6\u30eb\u306e\u958b\u59cb\u6642\u9593\u3068\u7d42\u4e86\u6642\u9593\u306b\u5bfe\u3057\u3066\u3001\u305d\u306e\u6642\u9593\u5e2f\u304c\u8a72\u5f53\u3059\u308b\u306e\u306f\u3001\u4e0b\u8a18\uff14\u3064\u306e\u30d1\u30bf\u30fc\u30f3\u304c\u3042\u308a\u3001\u305d\u308c\u305e\u308c\u6642\u9593\u306e\u53d6\u308a\u65b9\u304c\u5909\u308f\u3063\u3066\u304f\u308b\u306e\u3082\u9762\u5012\u306a\u30c8\u30b3\u30ed\u3067\u3059\u3002<\/p>\n<p>\u30fb\u958b\u59cb\u6642\u9593\u3068\u7d42\u4e86\u6642\u9593\u304c\u305d\u306e\u6642\u9593\u7bc4\u56f2\u5185<br \/>\n\u3000\u2192\u3000\u958b\u59cb\u6642\u9593\u304b\u3089\u7d42\u4e86\u6642\u9593\u307e\u3067\u306e\u6642\u9593<br \/>\n\u30fb\u958b\u59cb\u6642\u9593\u304c\u6642\u9593\u7bc4\u56f2\u3088\u308a\u904e\u53bb\u3060\u304c\u3001\u7d42\u4e86\u6642\u9593\u304c\u6642\u9593\u7bc4\u56f2\u5185<br \/>\n\u3000\u2192\u3000\u305d\u306e\u6642\u9593\u7bc4\u56f2\u306e\u958b\u59cb\u6642\u523b\u304b\u3089\u7d42\u4e86\u6642\u9593\u307e\u3067\u306e\u6642\u9593<br \/>\n\u30fb\u958b\u59cb\u6642\u9593\u304c\u6642\u9593\u7bc4\u56f2\u5185\u3060\u304c\u3001\u7d42\u4e86\u6642\u9593\u306f\u6642\u9593\u7bc4\u56f2\u3088\u308a\u672a\u6765<br \/>\n\u3000\u2192\u3000\u958b\u59cb\u6642\u9593\u304b\u3089\u305d\u306e\u6642\u9593\u7bc4\u56f2\u306e\u7d42\u4e86\u6642\u523b\u307e\u3067\u306e\u6642\u9593<br \/>\n\u30fb\u958b\u59cb\u6642\u9593\u306f\u6642\u9593\u7bc4\u56f2\u3088\u308a\u904e\u53bb\u3067\u3001\u7d42\u4e86\u6642\u9593\u304c\u6642\u9593\u7bc4\u56f2\u3088\u308a\u672a\u6765<br \/>\n\u3000\u2192\u30001\u6642\u9593\u56fa\u5b9a<\/p>\n<p>\u3000\u8abf\u3079\u3066\u3044\u308b\u3068<a href=\"http:\/\/rnk.mitelog.jp\/oyaji\/2016\/03\/postgresqldual-6927.html\" rel=\"noopener noreferrer\" target=\"_blank\">\u30b3\u30c1\u30e9<\/a>\u3092\u767a\u898b\u3001postgres\u306b\u306fgenerate_series\u3068\u3044\u3046\u4fbf\u5229\u306a\u3082\u306e\u304c\u3042\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u3000generate_series\u3067\uff11\u6642\u9593\u6bce\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u4e00\u5b9a\u671f\u9593\u5206\u751f\u6210\u51fa\u6765\u308b\u3088\u3046\u306a\u306e\u3067\u3001\u5c65\u6b74\u30c6\u30fc\u30d6\u30eb\u3068Join\u3057\u3066SQL\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:pgsql decode:true \" title=\"history.sql\" >select time,\r\nsum(case when xx_history.start_time &gt;= time and xx_history.end_time &lt;= time + interval '1 hours' then xx_history.end_time - xx_history.start_time\r\n     when xx_history.start_time &lt; time and xx_history.end_time &gt;= time and xx_history.end_time &lt;= time + interval '1 hours' then xx_history.end_time - time\r\n     when xx_history.start_time &gt;= time and xx_history.start_time &lt;= time + interval '1 hours' and xx_history.end_time &gt; time + interval '1 hours' then time + interval '1 hours' - xx_history.start_time\r\n     when xx_history.start_time &lt; time and xx_history.end_time &gt; time + interval '1 hours' then time + interval '1 hours' - time\r\n\t ELSE time - time END)\r\nfrom generate_series( date_trunc('day', current_timestamp) - interval '2 days' , date_trunc('day', current_timestamp), '1 hours') as time\r\nleft join xx_history \r\n   on (xx_history.start_time &gt;= time and xx_history.end_time &lt;= time + interval '1 hours')\r\n      or (xx_history.start_time &lt; time and xx_history.end_time &gt;= time and xx_history.end_time &lt;= time + interval '1 hours')\r\n      or (xx_history.start_time &gt;= time and xx_history.start_time &lt;= time + interval '1 hours' and xx_history.end_time &gt; time + interval '1 hours')\r\n      or (xx_history.start_time &lt; time and xx_history.end_time &gt; time + interval '1 hours')\r\ngroup by time\r\norder by time\r\n<\/pre>\n<p>\u3000where\u3068Case\u306e\u6761\u4ef6\u306f\u4e00\u7dd2\u306a\u306e\u3067\u3001Where\u306e\u65b9\u306f\u3082\u3063\u3068\u30b3\u30b9\u30c8\u3092\u304b\u3051\u306a\u3044\u6761\u4ef6\u306b\u898b\u76f4\u3057\u3057\u305f\u65b9\u304c\u3088\u3044\u3067\u3059\u306d\u3002<br \/>\n\u3000\u5b9f\u969b\u306f\u3082\u3063\u3068\u8907\u96d1\u306a\u5f62\u306b\u306a\u308b\u306e\u3067\u3059\u304c\u3001\u3068\u308a\u3042\u3048\u305aSQL1\u767a\u3067\u51fa\u6765\u305d\u3046\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3000metabase\u3067postgres\u30c7\u30fc\u30bf\u3092\u53ef\u8996\u5316\u3059\u308b\u3088\u3046\u306b\u3057\u305f\u6240\u3001\u5c65\u6b74\u30c7\u30fc\u30bf\u304c &hellip; <a href=\"https:\/\/esoro.jp\/?p=1784\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts\/1784"}],"collection":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1784"}],"version-history":[{"count":0,"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts\/1784\/revisions"}],"wp:attachment":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}