Oracle SQL - 来自/来自带有日期的表的日期

Posted

技术标签:

【中文标题】Oracle SQL - 来自/来自带有日期的表的日期【英文标题】:Oracle SQL - Dates from/to from a table with dates 【发布时间】:2021-02-11 14:45:23 【问题描述】:

我有一个问题,我不知道最好的解决方法是什么:

我有以下信息:

PRODUCT IS_BUY DATE
A 1 01/01/2021
A 1 02/01/2021
A 1 03/01/2021
A 0 04/01/2021
A 1 05/01/2021
A 1 06/01/2021
B 1 01/01/2021
C 1 01/01/2021
C 0 02/01/2021
C 0 03/01/2021
C 1 04/01/2021

我需要将以下结果留在每个产品存在或不购买的日期之间的位置:

PRODUCT IS_BUY FROM_DATE TO_DATE
A 1 01/01/2021 04/01/2021
A 0 04/01/2021 05/01/2021
A 1 05/01/2021 null
B 1 01/01/2021 null
C 1 01/01/2021 02/01/2021
C 0 02/01/2021 04/01/2021
C 1 04/01/2021 null

感谢您的帮助!

【问题讨论】:

请注意:Oracle SQL 不称为“PL/SQL”。 PL/SQL 是完全不同的东西。我不会告诉你它是什么——我相信你可以使用谷歌搜索来找出答案。我相应地更改了您的标签(以及您的问题的标题)。 【参考方案1】:

您没有告诉我们您的 Oracle 版本。下面我将展示两个解决方案:第一个是更高级的,使用 Oracle 12.1 中添加的 match_recognize 子句,然后是旧的方法,使用 Tabibitosan 方法解决间隙和岛屿问题(您的问题所属的类到)。

数据设置:

create table my_data (product, is_buy, eff_date) as
  select 'A', 1, to_date('01/01/2021', 'mm/dd/yyyy') from dual union all
  select 'A', 1, to_date('02/01/2021', 'mm/dd/yyyy') from dual union all
  select 'A', 1, to_date('03/01/2021', 'mm/dd/yyyy') from dual union all
  select 'A', 0, to_date('04/01/2021', 'mm/dd/yyyy') from dual union all
  select 'A', 1, to_date('05/01/2021', 'mm/dd/yyyy') from dual union all
  select 'A', 1, to_date('06/01/2021', 'mm/dd/yyyy') from dual union all
  select 'B', 1, to_date('01/01/2021', 'mm/dd/yyyy') from dual union all
  select 'C', 1, to_date('01/01/2021', 'mm/dd/yyyy') from dual union all
  select 'C', 0, to_date('02/01/2021', 'mm/dd/yyyy') from dual union all
  select 'C', 0, to_date('03/01/2021', 'mm/dd/yyyy') from dual union all
  select 'C', 1, to_date('04/01/2021', 'mm/dd/yyyy') from dual
;

(顺便说一下,这是在您的帖子中包含示例数据的首选方式!)

注意date是保留关键字;我将列名更改为eff_date

第一个解决方案,使用match_recognize匹配数据中的模式:

select product, is_buy, eff_date, end_date
from   my_data
match_recognize(
  partition by product
  order     by eff_date
  measures  a.is_buy       as is_buy, 
            a.eff_date     as eff_date,
            next(eff_date) as end_date
  pattern   ( a b* )
  define    b as is_buy = a.is_buy
);

PRODUCT IS_BUY EFF_DATE   END_DATE  
------- ------ ---------- ----------
A            1 01/01/2021 04/01/2021
A            0 04/01/2021 05/01/2021
A            1 05/01/2021           
B            1 01/01/2021           
C            1 01/01/2021 02/01/2021
C            0 02/01/2021 04/01/2021
C            1 04/01/2021           

第二种解决方案,仅使用解析函数和聚合(Tabibitosan 方法):

with prep as (
  select product, is_buy, eff_date,
         lead(eff_date) over (partition by product 
                              order by eff_date) as next_date,
         row_number() over (partition by product         order by eff_date) -
         row_number() over (partition by product, is_buy order by eff_date)
                 as grp
  from   my_data
)
select product, is_buy, min(eff_date) as eff_date,
       max(next_date) keep (dense_rank last order by eff_date) as end_date
from   prep
group  by product, is_buy, grp
order  by product, eff_date
;

【讨论】:

【参考方案2】:

试试这个

Select
Product,
Is_Buy,
(select  min(x.DATE) from products x where x.product=a.product and x.is_buy=a.is_buy) from_date,
(select  max(x.DATE) from products x where x.product=a.product and x.is_buy=a.is_buy) to_date,
from products a
group by a.Product, a.Is_Buy

【讨论】:

以上是关于Oracle SQL - 来自/来自带有日期的表的日期的主要内容,如果未能解决你的问题,请参考以下文章

访问具有最新日期的 SQL 唯一记录,包括来自单个表的空日期

我的 Oracle SQL 代码正在编译我的代码块,该代码块将游标用于来自两个表的循环,但 dbms 输出行未显示结果

sql 用于将ftp日志存储到PostgreSQL中的表的SQL和PLPGSQL代码(来自rsyslog)

SQL:来自具有不同日期格式的不同表的聚合

Oracle SQL 显示来自同一个表的匹配数据

Oracle SQL:使用来自同一表的另一列的数据更新表