{"id":1131,"date":"2021-12-29T00:57:03","date_gmt":"2021-12-28T16:57:03","guid":{"rendered":"https:\/\/blog.cauchyschwarz.com\/?p=1131"},"modified":"2021-12-29T00:57:15","modified_gmt":"2021-12-28T16:57:15","slug":"write-better-sql","status":"publish","type":"post","link":"https:\/\/blog.cauchyschwarz.com\/?p=1131","title":{"rendered":"write better sql"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-right counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69e0ad9fdadc5\" class=\"ez-toc-cssicon-toggle-label\"><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><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69e0ad9fdadc5\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=1131\/#%E5%8F%AF%E4%BB%A5%E4%BC%98%E5%8C%96%E7%9A%84%E7%82%B9\" >\u53ef\u4ee5\u4f18\u5316\u7684\u70b9<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=1131\/#hive%E4%B8%8A%E7%9A%84%E4%BC%98%E5%8C%96\" >hive\u4e0a\u7684\u4f18\u5316<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=1131\/#ck%E4%B8%8A%E7%9A%84%E4%BC%98%E5%8C%96\" >ck\u4e0a\u7684\u4f18\u5316<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=1131\/#FAQ\" >FAQ<\/a><\/li><\/ul><\/nav><\/div>\n<h1 class=\"wp-block-heading\" id=\"%E5%8F%AF%E4%BB%A5%E4%BC%98%E5%8C%96%E7%9A%84%E7%82%B9\"><span class=\"ez-toc-section\" id=\"%E5%8F%AF%E4%BB%A5%E4%BC%98%E5%8C%96%E7%9A%84%E7%82%B9\"><\/span>\u53ef\u4ee5\u4f18\u5316\u7684\u70b9<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<ul class=\"wp-block-list\"><li>\u76f8\u540c\u7684\u8868\u8fbe\u5f0f\u4e0d\u8981\u5199\u591a\u904d<\/li><li>\u4f4d\u8fd0\u7b97\u5199\u7684\u66f4\u52a0\u660e\u767d\u4e00\u70b9<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"hive%E4%B8%8A%E7%9A%84%E4%BC%98%E5%8C%96\"><span class=\"ez-toc-section\" id=\"hive%E4%B8%8A%E7%9A%84%E4%BC%98%E5%8C%96\"><\/span>hive\u4e0a\u7684\u4f18\u5316<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><code>with cube<\/code>\u7684\u4f7f\u7528<br><a href=\"https:\/\/tech.youzan.com\/cube\/\">https:\/\/tech.youzan.com\/cube\/<\/a><br>\u5176\u5b9e\u5c31\u662f\u628agroup by\u5217\u51fa\u6765\u7684\u7ef4\u5ea6\u7684\u53ef\u80fd\u7ec4\u5408\u90fdgroup by\u4e86\u4e00\u904d<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ck%E4%B8%8A%E7%9A%84%E4%BC%98%E5%8C%96\"><span class=\"ez-toc-section\" id=\"ck%E4%B8%8A%E7%9A%84%E4%BC%98%E5%8C%96\"><\/span>ck\u4e0a\u7684\u4f18\u5316<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4f7f\u7528with\u8bed\u53e5\u907f\u514d\u4e00\u4e2a\u8868\u8fbe\u5f0f\u5199\u591a\u904d<\/li><li>\u7528hex\u51fd\u6570\u6253\u5370\u6570\u5b57\u768416\u8fdb\u5236\u5f62\u5f0f<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"faq\"><span class=\"ez-toc-section\" id=\"FAQ\"><\/span>FAQ<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<ul class=\"wp-block-list\"><li>\u83b7\u53d6array\u7684\u957f\u5ea6<ul><li>hive<br><code>size(col)<\/code><\/li><li>ck<\/li><\/ul><\/li><li>\u83b7\u53d6string\u7684\u957f\u5ea6<ul><li>hive<\/li><li>ck<br><code>length(col)<\/code><\/li><\/ul><\/li><li><a href=\"https:\/\/www.codeproject.com\/articles\/547333\/counting-consecutive-dates-using-sql\">\u83b7\u53d6\u8fde\u7eed\u7684\u533a\u95f4<\/a><br>\u7528\u884c\u53f7\u548c\u60f3\u8981\u8fde\u7eed\u533a\u95f4\u7684\u5217\u76f8\u51cf\uff0c\u7136\u540e\u6839\u636egroup by\u7684\u7ed3\u679c\u518d\u53d6\u6700\u5927\u503c\u3001\u6700\u5c0f\u503c\u5c31\u5f97\u5230\u4e86\u8fde\u7eed\u533a\u95f4<br><a href=\"https:\/\/blog.csdn.net\/yyoc97\/article\/details\/93345918\">\u8fd9\u7bc7csdn\u535a\u5ba2<\/a>\u4e5f\u53ef\u4ee5\u63d0\u4f9b\u4e00\u5b9a\u7684\u53c2\u8003\u3002<\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/57347444\/collapsing-overlapping-time-intervals-using-clickhouse\">\u5408\u5e76\u533a\u95f4<\/a><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">SELECT\n    intervals,\n\n    arraySort(x -&gt; x, intervals) sortedIntervals,\n\n    <em>\/* try to merge each interval with precede ones *\/<\/em>\n    arrayMap((x, index) -&gt; index != 1\n        ? (arrayReduce(\n            'min', \n            arrayMap(\n              i -&gt; sortedIntervals[i + 1].1, \n              <em>\/* get indexes of intervals that can be merged with the current one (index is zero-based) *\/<\/em>              \n              arrayFilter(\n                i -&gt; x.1 &lt;= sortedIntervals[i + 1].2 AND x.2 &gt;= sortedIntervals[i + 1].1, \n                range(index)))),\n          arrayReduce(\n            'max', \n            arrayMap(\n              i -&gt; sortedIntervals[i + 1].2,  \n              <em>\/* get indexes of intervals that can be merged with the current one (index is zero-based) *\/<\/em>              \n              arrayFilter(\n                i -&gt; x.1 &lt;= sortedIntervals[i + 1].2 AND x.2 &gt;= sortedIntervals[i + 1].1, \n                range(index)))))\n        : x,\n      sortedIntervals, \n      arrayEnumerate(sortedIntervals)) rawResult,\n\n    <em>\/* filter out intervals nested to other ones *\/<\/em>\n    arrayFilter(\n      (x, index) -&gt; index == length(rawResult) OR x.1 != rawResult[index + 1].1,\n      rawResult, \n      arrayEnumerate(rawResult)) result\nFROM\n(\n    SELECT [(1, 5), (2, 3), (3, 8), (10, 15)] intervals\n    UNION ALL\n    SELECT [(2, 4), (1, 3), (3, 6), (12, 14), (7, 7), (13, 16), (9, 9), (8, 9), (10, 15)]\n    UNION ALL\n    SELECT [(20, 22), (18, 18), (16, 21), (1, 8), (2, 9), (3, 5), (10, 12), (11, 13), (14, 15)]\n    UNION ALL\n    SELECT []\n    UNION ALL \n    SELECT [(1, 11)]\n)\n\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5bb9\u6613\u5ffd\u89c6\u7684\u5730\u65b9<ul><li>null\u503c\u7684\u5904\u7406<ul><li>\u8ba1\u7b97avg\u65f6\uff0cnull\u503c\u7684\u5904\u7406<\/li><li>join\u8868\u6216\u8005lateral view explode\u6216\u8005array join\u65f6null\u503c\u7684\u5904\u7406<\/li><\/ul><\/li><\/ul><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u53ef\u4ee5\u4f18\u5316\u7684\u70b9 \u76f8\u540c\u7684\u8868\u8fbe\u5f0f\u4e0d\u8981\u5199\u591a\u904d \u4f4d\u8fd0\u7b97\u5199\u7684\u66f4\u52a0\u660e\u767d\u4e00\u70b9 hive\u4e0a\u7684\u4f18\u5316 with cube\u7684\u4f7f\u7528https:\/\/tech.youzan.com\/cube\/\u5176\u5b9e\u5c31\u662f\u628agroup by\u5217\u51fa\u6765\u7684\u7ef4\u5ea6&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[31],"class_list":["post-1131","post","type-post","status-publish","format-standard","hentry","category-10","tag-sql"],"_links":{"self":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/1131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1131"}],"version-history":[{"count":1,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/1131\/revisions"}],"predecessor-version":[{"id":1132,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/1131\/revisions\/1132"}],"wp:attachment":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}