Oracle查询以查找过去两个月内存在的客户ID

Oracle查询以查找过去两个月内存在的客户ID,oracle,Oracle,我在DB中有过去6个月的客户记录 select CN_NUMBER,ACCOUNT_NUMBER,TRNX_CREATED_DATE from TRANSACTION_REPORTS t /* a transaction in the previous month */ where trunc(last_day(t.TRNX_CREATED_DATE)) = trunc(last_day(add_months(sysdate, -1))) /* no transactions in this

我在DB中有过去6个月的客户记录

select CN_NUMBER,ACCOUNT_NUMBER,TRNX_CREATED_DATE
from TRANSACTION_REPORTS t
/* a transaction in the previous month */
where trunc(last_day(t.TRNX_CREATED_DATE)) = trunc(last_day(add_months(sysdate, -1)))
  /* no transactions in this month */
  and NOT exists(
                  select 1
                  from TRANSACTION_REPORTS t2
                  where t2.ACCOUNT_NUMBER = t.ACCOUNT_NUMBER
                    and trunc(last_day(t2.TRNX_CREATED_DATE)) = trunc(last_day(sysdate))
                 )
目前,我已经创建了columnTRNX__DATE来确定交易何时完成

select CN_NUMBER,ACCOUNT_NUMBER,TRNX_CREATED_DATE
from TRANSACTION_REPORTS t
/* a transaction in the previous month */
where trunc(last_day(t.TRNX_CREATED_DATE)) = trunc(last_day(add_months(sysdate, -1)))
  /* no transactions in this month */
  and NOT exists(
                  select 1
                  from TRANSACTION_REPORTS t2
                  where t2.ACCOUNT_NUMBER = t.ACCOUNT_NUMBER
                    and trunc(last_day(t2.TRNX_CREATED_DATE)) = trunc(last_day(sysdate))
                 )
下面是我用来获取一个月的客户记录的查询

select distinct(CN_NUMBER),ACCOUNT_NUMBER,TRNX_CREATED_DATE from TRANSACTION_REPORTS 
where TRNX_CREATED_DATE>=add_months(trunc(sysdate,'mm'),-1) 
select CN_NUMBER,ACCOUNT_NUMBER,TRNX_CREATED_DATE
from TRANSACTION_REPORTS t
/* a transaction in the previous month */
where trunc(last_day(t.TRNX_CREATED_DATE)) = trunc(last_day(add_months(sysdate, -1)))
  /* no transactions in this month */
  and NOT exists(
                  select 1
                  from TRANSACTION_REPORTS t2
                  where t2.ACCOUNT_NUMBER = t.ACCOUNT_NUMBER
                    and trunc(last_day(t2.TRNX_CREATED_DATE)) = trunc(last_day(sysdate))
                 )
通过此查询,我可以获得上一个月的客户记录。我不知道如何获得本月新增的客户、最近两个月出现的客户或上个月出现但本月不再出现的客户

select CN_NUMBER,ACCOUNT_NUMBER,TRNX_CREATED_DATE
from TRANSACTION_REPORTS t
/* a transaction in the previous month */
where trunc(last_day(t.TRNX_CREATED_DATE)) = trunc(last_day(add_months(sysdate, -1)))
  /* no transactions in this month */
  and NOT exists(
                  select 1
                  from TRANSACTION_REPORTS t2
                  where t2.ACCOUNT_NUMBER = t.ACCOUNT_NUMBER
                    and trunc(last_day(t2.TRNX_CREATED_DATE)) = trunc(last_day(sysdate))
                 )
但是现在我想写三个独立的查询来完成下面的功能

select CN_NUMBER,ACCOUNT_NUMBER,TRNX_CREATED_DATE
from TRANSACTION_REPORTS t
/* a transaction in the previous month */
where trunc(last_day(t.TRNX_CREATED_DATE)) = trunc(last_day(add_months(sysdate, -1)))
  /* no transactions in this month */
  and NOT exists(
                  select 1
                  from TRANSACTION_REPORTS t2
                  where t2.ACCOUNT_NUMBER = t.ACCOUNT_NUMBER
                    and trunc(last_day(t2.TRNX_CREATED_DATE)) = trunc(last_day(sysdate))
                 )
在本月和上个月进行交易的客户。 在本月完成交易但在上个月未完成交易的客户。 本月未完成交易但上个月已完成交易的客户。
请建议。。提前感谢

有很多方法可以做到这一点;在下面,我试图给出一个可读的解决方案。
select CN_NUMBER,ACCOUNT_NUMBER,TRNX_CREATED_DATE
from TRANSACTION_REPORTS t
/* a transaction in the previous month */
where trunc(last_day(t.TRNX_CREATED_DATE)) = trunc(last_day(add_months(sysdate, -1)))
  /* no transactions in this month */
  and NOT exists(
                  select 1
                  from TRANSACTION_REPORTS t2
                  where t2.ACCOUNT_NUMBER = t.ACCOUNT_NUMBER
                    and trunc(last_day(t2.TRNX_CREATED_DATE)) = trunc(last_day(sysdate))
                 )
这解决了第一个问题:

select CN_NUMBER,ACCOUNT_NUMBER,TRNX_CREATED_DATE
from TRANSACTION_REPORTS t
/* a transaction in this month */
where trunc(last_day(t.TRNX_CREATED_DATE)) = trunc(last_day(sysdate)) 
  /* and a transaction in the previous month */
  and exists( select 1
              from TRANSACTION_REPORTS t2
              where t2.ACCOUNT_NUMBER = t.ACCOUNT_NUMBER
                and trunc(last_day(t2.TRNX_CREATED_DATE)) = trunc(last_day(add_months(sysdate, -1)))
             )
select CN_NUMBER,ACCOUNT_NUMBER,TRNX_CREATED_DATE
from TRANSACTION_REPORTS t
/* a transaction in the previous month */
where trunc(last_day(t.TRNX_CREATED_DATE)) = trunc(last_day(add_months(sysdate, -1)))
  /* no transactions in this month */
  and NOT exists(
                  select 1
                  from TRANSACTION_REPORTS t2
                  where t2.ACCOUNT_NUMBER = t.ACCOUNT_NUMBER
                    and trunc(last_day(t2.TRNX_CREATED_DATE)) = trunc(last_day(sysdate))
                 )
第二个非常类似,您只需要一点布尔逻辑:

select CN_NUMBER,ACCOUNT_NUMBER,TRNX_CREATED_DATE
from TRANSACTION_REPORTS t
/* a transaction in this month */
where trunc(last_day(t.TRNX_CREATED_DATE)) = trunc(last_day(sysdate))
  /* no transactions in the previous month */
  and NOT exists(
                  select 1
                  from TRANSACTION_REPORTS t2
                  where t2.ACCOUNT_NUMBER = t.ACCOUNT_NUMBER
                    and trunc(last_day(t2.TRNX_CREATED_DATE)) = trunc(last_day(add_months(sysdate, -1)))
                 ) 
select CN_NUMBER,ACCOUNT_NUMBER,TRNX_CREATED_DATE
from TRANSACTION_REPORTS t
/* a transaction in the previous month */
where trunc(last_day(t.TRNX_CREATED_DATE)) = trunc(last_day(add_months(sysdate, -1)))
  /* no transactions in this month */
  and NOT exists(
                  select 1
                  from TRANSACTION_REPORTS t2
                  where t2.ACCOUNT_NUMBER = t.ACCOUNT_NUMBER
                    and trunc(last_day(t2.TRNX_CREATED_DATE)) = trunc(last_day(sysdate))
                 )
第三个,你只需要交换条件:

select CN_NUMBER,ACCOUNT_NUMBER,TRNX_CREATED_DATE
from TRANSACTION_REPORTS t
/* a transaction in the previous month */
where trunc(last_day(t.TRNX_CREATED_DATE)) = trunc(last_day(add_months(sysdate, -1)))
  /* no transactions in this month */
  and NOT exists(
                  select 1
                  from TRANSACTION_REPORTS t2
                  where t2.ACCOUNT_NUMBER = t.ACCOUNT_NUMBER
                    and trunc(last_day(t2.TRNX_CREATED_DATE)) = trunc(last_day(sysdate))
                 )

请提供您到目前为止尝试的信息。我已经更新了其他信息,请建议。为什么使用mysql标签?谢谢@Aleksej,上面的查询解决了我的要求。