获取日期范围的月末日期
Getting Month End Dates for a date range
我有一个 table 它将给我名字并开始 date.Using 我需要获取从开始日期到日期的名称和月末日期行的记录。
我的基本 table 如下所示
Name | Start_date
Fly | 01-Jul-16
我的输出如下所示
Name Start_date
Fly| 31-Jul-16
Fly 31-Aug-16
Fly 30-Sep-16
Fly 31-Oct-16
Fly 30-Nov-16
Fly 31-Dec-16
Fly 31-Jan-17
Fly 28-Feb-17
Fly 31-Mar-17
Fly 30-Apr-17
Fly 31-May-17
Fly 30-Jun-17
Fly 31-Jul-17
Fly 31-Aug-17
我尝试了一些使用按级别连接和使用最后一天功能的方法,但是 table 没有名称可以加入基本 table.I 我的 [= 中有超过 100 个名称20=] .请您指导.
如果您有多个姓名和一个日期,您可以按照以下方法进行操作,但只有当输入的姓名不同时才有效。 (否则可以修改查询以适应非唯一名称。)
奇怪的是,您希望调用月末日期 start_date
,与输入数据中的相同。此外,name
是 Oracle 关键字,不应用作列名。我会让你处理这两个问题。
with
test_data ( name, start_date ) as (
select 'Fly', to_date('01-Jul-16', 'dd-Mon-yy') from dual union all
select 'Two', to_date('15-Feb-17', 'dd-Mon-yy') from dual
)
-- End of simulated inputs (for testing only, not part of the solution).
-- Use your actual table and column names in the SQL below.
select name, add_months(last_day(start_date), level - 1) as start_date
from test_data
connect by add_months(last_day(start_date), level - 1) <= last_day(sysdate)
and prior name = name
and prior sys_guid() is not null
order by name, start_date
;
输出:
NAME START_DATE
--- ----------
Fly 31-Jul-16
Fly 31-Aug-16
Fly 30-Sep-16
Fly 31-Oct-16
Fly 30-Nov-16
Fly 31-Dec-16
Fly 31-Jan-17
Fly 28-Feb-17
Fly 31-Mar-17
Fly 30-Apr-17
Fly 31-May-17
Fly 30-Jun-17
Fly 31-Jul-17
Fly 31-Aug-17
Two 28-Feb-17
Two 31-Mar-17
Two 30-Apr-17
Two 31-May-17
Two 30-Jun-17
Two 31-Jul-17
Two 31-Aug-17
我有一个 table 它将给我名字并开始 date.Using 我需要获取从开始日期到日期的名称和月末日期行的记录。 我的基本 table 如下所示
Name | Start_date
Fly | 01-Jul-16
我的输出如下所示
Name Start_date
Fly| 31-Jul-16
Fly 31-Aug-16
Fly 30-Sep-16
Fly 31-Oct-16
Fly 30-Nov-16
Fly 31-Dec-16
Fly 31-Jan-17
Fly 28-Feb-17
Fly 31-Mar-17
Fly 30-Apr-17
Fly 31-May-17
Fly 30-Jun-17
Fly 31-Jul-17
Fly 31-Aug-17
我尝试了一些使用按级别连接和使用最后一天功能的方法,但是 table 没有名称可以加入基本 table.I 我的 [= 中有超过 100 个名称20=] .请您指导.
如果您有多个姓名和一个日期,您可以按照以下方法进行操作,但只有当输入的姓名不同时才有效。 (否则可以修改查询以适应非唯一名称。)
奇怪的是,您希望调用月末日期 start_date
,与输入数据中的相同。此外,name
是 Oracle 关键字,不应用作列名。我会让你处理这两个问题。
with
test_data ( name, start_date ) as (
select 'Fly', to_date('01-Jul-16', 'dd-Mon-yy') from dual union all
select 'Two', to_date('15-Feb-17', 'dd-Mon-yy') from dual
)
-- End of simulated inputs (for testing only, not part of the solution).
-- Use your actual table and column names in the SQL below.
select name, add_months(last_day(start_date), level - 1) as start_date
from test_data
connect by add_months(last_day(start_date), level - 1) <= last_day(sysdate)
and prior name = name
and prior sys_guid() is not null
order by name, start_date
;
输出:
NAME START_DATE
--- ----------
Fly 31-Jul-16
Fly 31-Aug-16
Fly 30-Sep-16
Fly 31-Oct-16
Fly 30-Nov-16
Fly 31-Dec-16
Fly 31-Jan-17
Fly 28-Feb-17
Fly 31-Mar-17
Fly 30-Apr-17
Fly 31-May-17
Fly 30-Jun-17
Fly 31-Jul-17
Fly 31-Aug-17
Two 28-Feb-17
Two 31-Mar-17
Two 30-Apr-17
Two 31-May-17
Two 30-Jun-17
Two 31-Jul-17
Two 31-Aug-17