SQL来查找连续的一半
我正在寻找连续半年的期间开始和结束日期。就像今天的2019年9月24日一样,我们正在寻找sql,它可以提供以下输出,并且根据当前日期是动态的SQL来查找连续的一半,sql,oracle,Sql,Oracle,我正在寻找连续半年的期间开始和结束日期。就像今天的2019年9月24日一样,我们正在寻找sql,它可以提供以下输出,并且根据当前日期是动态的 | Period | Start Date | End Date | +---------------+------------+-----------+ | First Half | 1-Jan-19 | 30-Jun-19 | | Previous Half | 1-Jul-18 | 31-Dec-18 | 有人能在这里
| Period | Start Date | End Date |
+---------------+------------+-----------+
| First Half | 1-Jan-19 | 30-Jun-19 |
| Previous Half | 1-Jul-18 | 31-Dec-18 |
有人能在这里提供意见吗
我在尝试下面的内容,但似乎需要很多if-else语句
select (case when extract(month from sysdate) <= 6 then 1
else 2
end) as half_year
from dual
我是这样理解的:
SQL> with test (curdate) as
2 (select date '2019-09-24' from dual union all
3 select date '2015-03-12' from dual
4 ),
5 inter as
6 (select curdate,
7 to_number(to_char(curdate, 'mm')) curdate_mon
8 from test
9 )
10 select curdate,
11 case when curdate_mon <= 6 then add_months(trunc(curdate, 'yyyy'), -12)
12 else add_months(trunc(curdate, 'yyyy'), -6)
13 end prev_start,
14 case when curdate_mon <= 6 then add_months(trunc(curdate, 'yyyy'), -6) - 1
15 else trunc(curdate, 'yyyy') - 1
16 end prev_end,
17 --
18 case when curdate_mon <= 6 then add_months(trunc(curdate, 'yyyy'), -6)
19 else trunc(curdate, 'yyyy')
20 end first_start,
21 case when curdate_mon <= 6 then trunc(curdate, 'yyyy') - 1
22 else add_months(trunc(curdate, 'yyyy'), 6) - 1
23 end first_end
24 from inter;
CURDATE PREV_START PREV_END FIRST_STAR FIRST_END
---------- ---------- ---------- ---------- ----------
24.09.2019 01.07.2018 31.12.2018 01.01.2019 30.06.2019
12.03.2015 01.01.2014 30.06.2014 01.07.2014 31.12.2014
SQL>
谢谢你的快速回复。但如果您看到我2019年9月24日的要求输出截图,下半年未完成,因此预计最晚的半年为2019年1月1日和2019年6月30日,而上半年为2018年7月1日和2018年12月31日,显然我误解了这个问题。至少现在已经修好了,我希望如此。请看一看。