【问题标题】:Reporting MIN(VAL) for Each Partitioned Value in Another Column报告另一列中每个分区值的 MIN(VALUE)
【发布时间】:2021-08-01 23:08:53
【问题描述】:

甲骨文 11g

如何为每个 email_type 每个 ID 返回单行 min(email_sent)

也就是说,我想将第一个邀请日期和第一个确认日期放在一行中。

with mailings  as (
select 1 as recipient_id, 'INVITE' as email_type, to_date('JAN-01-2020','MON-DD-YYYY') as email_sent from dual union all
select 1 as recipient_id, 'INVITE' as email_type, to_date('JAN-02-2020','MON-DD-YYYY') as email_sent from dual union all
select 1 as recipient_id, 'INVITE' as email_type, to_date('JAN-03-2020','MON-DD-YYYY') as email_sent from dual union all
select 1 as recipient_id, 'CONFIRM'as email_type, to_date('JAN-10-2020','MON-DD-YYYY') as email_sent from dual union all
select 1 as recipient_id, 'CONFIRM'as email_type, to_date('JAN-11-2020','MON-DD-YYYY') as email_sent from dual
)
 select *
from (
select recipient_id, 
       email_type, 
       min(email_sent) over (partition by recipient_id, email_type) as first_invite,
       min(email_sent) over (partition by recipient_id, email_type) as first_confirmation,
       rank()          over (partition by recipient_id, email_type order by email_sent) as email_type
                                                        from mailings
                                                    
)
where email_type=1;

期望的结果:报告第一次邀请和第一次确认的日期。

Recipient_ID FIRST_INVITE FIRST_CONFIRMATION

1 JAN-01-2020 JAN-10-2020

【问题讨论】:

    标签: sql oracle oracle11g min


    【解决方案1】:

    这里是,根据您的措辞,而不是您的结果(如您第一次发送,但您显示上次发送):

    with mailings  as (
    select 1 as recipient_id, 'INVITE' as email_type, to_date('JAN-01-2020','MON-DD-YYYY') as email_sent from dual union all
    select 1 , 'INVITE' , to_date('JAN-02-2020','MON-DD-YYYY')  from dual union all
    select 1 , 'INVITE' , to_date('JAN-03-2020','MON-DD-YYYY')  from dual union all
    select 1 , 'CONFIRM', to_date('JAN-10-2020','MON-DD-YYYY')  from dual union all
    select 1 , 'CONFIRM', to_date('JAN-11-2020','MON-DD-YYYY')  from dual
    )
    select recipient_id, 
           min(case when email_type='INVITE' then email_sent else null end) sent, 
           min(case when email_type='CONFIRM' then email_sent else null end) confirmed 
    from mailings
    group by recipient_id;
    
    RECIPIENT_ID    SENT        CONFIRMED
    1               01-JAN-20   10-JAN-20
    

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 2012-06-26
      • 1970-01-01
      相关资源
      最近更新 更多