{"id":5471,"date":"2021-03-10T10:22:53","date_gmt":"2021-03-10T02:22:53","guid":{"rendered":"https:\/\/nj.transwarp.cn:8180\/?p=5471"},"modified":"2025-12-30T16:17:02","modified_gmt":"2025-12-30T08:17:02","slug":"inceptor%e8%bf%94%e5%9b%9e%e4%b8%a4%e4%b8%aa%e6%97%a5%e6%9c%9f%e4%b9%8b%e9%97%b4%e7%9a%84%e6%89%80%e6%9c%89%e6%97%a5%e6%9c%9f","status":"publish","type":"post","link":"https:\/\/kbwp.transwarp.cn\/?p=5471","title":{"rendered":"inceptor\u8fd4\u56de\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u65e5\u671f\u548c\u5c0f\u65f6\u6e05\u5355"},"content":{"rendered":"<h3>\u6982\u8981\u63cf\u8ff0<\/h3>\n<hr \/>\n<p><strong>oracle\u4e2d\u53ef\u4ee5\u901a\u8fc7connect by level\u4f2a\u5217\u6765\u5b9e\u73b0\uff0c\u53c2\u8003\u5982\u4e0bsql\u8bed\u53e5:<\/strong><\/p>\n<pre><code class=\"language-sql\">create table test (begin_date date,end_date date);\ninsert into test values(trunc(sysdate),trunc(sysdate+5));\ncommit;\n\nselect begin_date, end_date, begin_date + level - 1 as today\n  from test\nconnect by begin_date + level - 1 <= end_date;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/03\/image-1623314414785.png\" alt=\"file\" \/><\/p>\n<p><strong>\u800c\u6211\u4eec\u7684inceptor\u4e0d\u652f\u6301connect by level\u65b9\u5f0f\uff0c\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u7684\u65b9\u6cd5\u8fd4\u56de\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u65e5\u671f\u548c\u5c0f\u65f6\u6e05\u5355\u3002<\/strong><\/p>\n<h3>\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<hr \/>\n<h4>1. \u6784\u5efa\u6837\u4f8b\u8868<\/h4>\n<pre><code class=\"language-sql\">DROP TABLE IF EXISTS dtbetween;\nCREATE TABLE dtbetween(id INT,start_date STRING ,end_date STRING ) STORED AS ORC;\n\n--\u6b63\u5e38\u6570\u636e\nINSERT INTO dtbetween SELECT '1', '2020-01-05','2020-01-09' FROM system.dual;\n--\u5f02\u5e38\u6570\u636e\nINSERT INTO dtbetween SELECT '2', '2020-02-09','2020-02-05' FROM system.dual;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/03\/image-1615342541430.png\" alt=\"file\" \/><\/p>\n<h4>2. \u8ba1\u7b97\u4e2d\u95f4\u65e5\u671f<\/h4>\n<pre><code class=\"language-sql\">SET character.literal.as.string=true;\n\nSELECT\n    tmp.*,\n    date_add(tmp.start_date, pos) as mid_date,\n    t.pos\nFROM dtbetween tmp lateral view posexplode(\n    split(space(datediff(end_date, start_date)+1), '')\n    ) t AS pos,val\nWHERE tmp.start_date<=tmp.end_date;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/03\/image-1615355456388.png\" alt=\"file\" \/><\/p>\n<p>\u539f\u7406\u6765\u8bf4\uff0c\u5c31\u662f\u901a\u8fc7<code>datediff<\/code>\u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u76f8\u5dee\u7684\u5929\u6570N\uff0c\u7136\u540e\u901a\u8fc7<code>split<\/code>,<code>space<\/code>\u51fd\u6570\u6784\u9020\u4e00\u4e2aN\u4e2a\u5b57\u6bb5\u7684<code>array<\/code>\u7ed9<code>lateral view posexplode<\/code>\u51fd\u6570\u505a\u8f6c\u5217\uff0c\u7136\u540e\u518d\u901a\u8fc7<code>date_add<\/code>\u51fd\u6570\u5c06start_date\u548c\u8fd9\u4e2apos\u5dee\u76f8\u52a0\uff0c\u83b7\u53d6\u4e2d\u95f4\u65e5\u671f\u3002<\/p>\n<p>\u66f4\u7b80\u6d01\u7684\uff1a<\/p>\n<pre><code class=\"language-sql\">SET character.literal.as.string=true;\n\nSELECT\n    date_add('2026-01-01', pos) as mid_date\nFROM system.dual lateral view posexplode(\n    split(space(datediff('2026-12-31', '2026-01-01')+1), '')\n    ) t AS pos,val;<\/code><\/pre>\n<h4>3. \u62d3\u5c55-\u5c0f\u65f6\u7c92\u5ea6<\/h4>\n<pre><code class=\"language-sql\">DROP TABLE IF EXISTS dtbetween_hour;\nCREATE TABLE dtbetween_hour(id INT,start_date STRING  ,end_date STRING ) STORED AS ORC;\n\n--\u6b63\u5e38\u6570\u636e\nINSERT INTO dtbetween_hour select '1' ,'2020-01-01 20:00:00', '2020-01-02 03:00:00' FROM system.dual;\n--\u5f02\u5e38\u6570\u636e\nINSERT INTO dtbetween_hour select '2' , '2020-02-02 3:00:00','2020-02-01 20:00:00' FROM system.dual;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/03\/image-1615346889877.png\" alt=\"file\" \/><\/p>\n<pre><code class=\"language-sql\">SET character.literal.as.string=TRUE;\n\nSELECT\n    tmp.*,\n    from_unixtime(unix_timestamp(start_date) + pos * 3600) as mid_hour,\n    t.pos\nFROM dtbetween_hour tmp lateral view posexplode(\n    split(space(\n    hour(end_date) - hour(start_date) + datediff(end_date, start_date)*24 + 1\n      ), '')\n    ) t AS pos,val\nWHERE tmp.start_date<=tmp.end_date;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/03\/image-1615355531760.png\" alt=\"file\" \/><\/p>\n<h4>3. \u62d3\u5c55-\u6708\u4efd\u7c92\u5ea6<\/h4>\n<pre><code class=\"language-sql\">SET character.literal.as.string=TRUE;\nWITH tmp AS \n(\nSELECT 1 AS id ,date('20220615') AS start_date, date('20220905') AS end_date FROM system.dual UNION ALL\nSELECT 2 AS id ,date('20220615') AS start_date, date('20220620') AS end_date FROM system.dual\n)\nSELECT \n    tmp.*,\n    add_months(trunc(end_date,'MM'), -pos) as mid_date,\n    t.pos\nFROM tmp lateral view posexplode(\n    split(space(EXTRACT(MONTH FROM end_date)-EXTRACT(MONTH FROM start_date)+(EXTRACT(YEAR FROM end_date)-EXTRACT(YEAR FROM start_date))*12 +1), '')\n    ) t AS pos,val\nWHERE tmp.start_date<=tmp.end_date;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/03\/image-1655366940418.png\" alt=\"file\" \/><\/p>\n<h4>4. \u62d3\u5c55-\u7edf\u8ba1\u4e24\u4e2a\u65e5\u671f\u4e4b\u95f4\u7684\u5de5\u4f5c\u65e5<\/h4>\n<pre><code class=\"language-sql\">SET character.literal.as.string=true;\n\nWITH tb AS \n(\nSELECT date('20221025') AS START_DATE,date('20221029') AS END_DATE FROM SYSTEM.DUAL UNION ALL \nSELECT date('20221024') AS START_DATE,date('20221029') AS END_DATE FROM SYSTEM.DUAL UNION ALL \nSELECT date('20221024') AS START_DATE,date('20221031') AS END_DATE FROM SYSTEM.DUAL\n)\nSELECT start_date,end_date,sum(ifweekend) FROM (\nselect  \n    start_date,\n    end_date, \n    date_add(start_date,i) as day_,\n    case when dayofweek(date_add(start_date,i)) in (1,7) then 0 else 1 END AS ifweekend,\n    i\nfrom tb tmp lateral view posexplode(split(space(datediff(end_date,start_date)+1),'')) pe as i,x )\nGROUP BY start_date,end_date;<\/code><\/pre>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2021\/03\/image-1666938102657.png\" alt=\"file\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981\u63cf\u8ff0 oracle\u4e2d\u53ef\u4ee5\u901a\u8fc7connect by level\u4f2a\u5217\u6765\u5b9e\u73b0\uff0c\u53c2\u8003\u5982\u4e0bsql\u8bed\u53e5: create ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/kbwp.transwarp.cn\/?p=5471\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-5471","post","type-post","status-publish","format-standard","hentry","category-sql_issue"],"acf":[],"_links":{"self":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/5471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5471"}],"version-history":[{"count":2,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/5471\/revisions"}],"predecessor-version":[{"id":17842,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/5471\/revisions\/17842"}],"wp:attachment":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}