报告另一列中每个分区值的 MIN(VALUE)

Reporting MIN(VAL) for Each Partitioned Value in Another Column

甲骨文 11g

我如何 return 单行 min(email_sent) 每个 email_type 每个 ID

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

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

给你,根据你的措辞,而不是你的结果(你第一次发送,但你显示最后发送):

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