Oracle SQL 问题 - 需要从表中返回特定行

问题描述 投票:0回答:1

有您的帮助总是很棒。

我正在使用一个名为 Unica 的活动管理系统,该系统在幕后执行 SQL 查询。 在我的工作中,我有一个联系人历史记录表,其中保存了适合营销活动中的人群的每个客户的联系人。 我们有 4 个 contactstatusid(1-找到合适的,2-发送到通道,5-接触,6-导联关闭)

逻辑如下:状态号 1 首先出现,几分钟后,在通信渠道获得该客户后,状态将更改为 2。当 CRM 中的某些状态发生更改时,状态号 5 出现。 我们可以获得多个具有相同客户 ID 的行,但任何客户都可以每天一次适合该活动,但一生中可以不止一次。

我想要获取具有任何状态 ID 号为 2 的行且没有状态 ID 为 5 的拟合行的客户的客户 ID。(如果两种状态的 contactdatetime 位于同一天,则它们是拟合的)并且我需要渠道 = 'CRM' 的行

我试图判断该客户是否有状态 ID 5,所以不要给他回复,但随后我错过了该客户的很多行,这些行没有适合的状态 ID 5。 我也尝试过一些加入和事情,但我没有得到正确的结果。

select * from unica_campaign.ua_contacthistory_dlv

where channel = 'CRM'

and contactstatusid = '2'

and contactdatetime not in

    (select a.custid, a.contactdatetime

    from unica_campaign.ua_contacthistory_dlv a

    inner join unica_campaign.ua_contacthistory_dlv b

    on a.custid = b.custid

    where a.channel = b.channel

    and extract(day from a.contactdatetime)=extract(day from b.contactdatetime)

    and extract(month from a.contactdatetime)=extract(month from b.contactdatetime)

    and extract(year from a.contactdatetime)=extract(year from b.contactdatetime)

    and a.channel = 'CRM'

    and a.contactstatusid = '2'

    and b.contactstatusid = '5'
);

请帮助我! 非常感谢。

sql oracle plsql
1个回答
0
投票

我认为你必须在选择中添加客户 ID

 select * from unica_campaign.ua_contacthistory_dlv c

    where channel = 'CRM'

    and a.contactstatusid = '2'

    and contactdatetime not in

        (select a.contactdatetime

        from unica_campaign.ua_contacthistory_dlv a

        inner join unica_campaign.ua_contacthistory_dlv b

        on a.custid = b.custid

        where a.channel = b.channel

        and extract(day from a.contactdatetime)=extract(day from b.contactdatetime)

        and extract(month from a.contactdatetime)=extract(month from b.contactdatetime)

        and extract(year from a.contactdatetime)=extract(year from b.contactdatetime)

        and a.channel = 'CRM'

        and a.contactstatusid = '2'

        and b.contactstatusid = '5'
        and a.custid=c.custid
    );
© www.soinside.com 2019 - 2024. All rights reserved.