-1

我需要根据他们的收入贡献期在一个月内获得活跃的客户数量。

原始数据:

ACCOUNT_ID  REVENUE_START_DATE       REVENUE_END_DATE
1234         1/14/2010 0:00          4/13/2010 23:59
4567         2/9/2010 0:00           3/8/2010 23:59
1234         5/9/2010 0:00           6/8/2010 23:59

预期结果

Month   Count
Dec-09  0
Jan-10  1
Feb-10  2
Mar-10  2
Apr-10  1
May-10  1
Jun-10  1
Jul-10  0
Aug-10  0
Sep-10  
Oct-10  

下面是我处理的 Oracle 代码(在谷歌的帮助下),但由于日期重叠,我没有得到正确的结果。我请求专家帮助我解决这个问题。(提前致谢)

当前结果:

YEAR_   MONTH_  ACT
2010    January     2
2010    February    3
2010    March       3
2010    April       3

甲骨文代码:

with tab as
(
     select distinct ACCOUNT_ID,  billing_start_date as revenue_start_date, billing_end_date as revenue_end_date
     from accounts
                ),                    
     year_tab as 
                (
                    select 
                             add_months(min_date, level -1) m
                    from 
                            (
                            select min(trunc(revenue_start_date,'YYYY')) min_date, add_months(max(trunc(revenue_end_date,'YYYY')), 12) max_date
                            from tab
                            )
                    connect by level <= months_between(max_date, min_date)
              )     
select to_char(m,'YYYY') year_,
         to_char(m,'Month') month_,
         nvl(act, 0) act
from   year_tab,
            (            
            select m date_,count(*)  act
            from tab, year_tab
            where m between trunc(revenue_start_date,'MM') and trunc(revenue_end_date,'MM')
            group by m
            ) month_tab
where m = date_(+)
order by m;
4

1 回答 1

0

我花了一段时间才明白你为什么认为有问题。使用您提供的原始三行数据,运行查询会准确给出您的“预期结果”。使用 CSV 文件中的 54 行数据,结果为 48 行(涵盖四年),从 2010 年 1 月到 2013 年 1 月的总计非零。返回的前几行是:

YEAR_ MONTH_                                      ACT
----- ------------------------------------ ----------
2010  January                                       2 
2010  February                                      3 
2010  March                                         3 
2010  April                                         3
2010  May                                           2 

但这看起来是正确的:

select * from accounts
where not (billing_start_date > date '2010-02-01'
        or billing_end_date < date '2010-01-01');

ACCOUNT_ID BILLING_START_DATE BILLING_END_DATE 
---------- ------------------ ------------------
      1234 09/01/2010 00:00   08/02/2010 23:59   
      4567 14/01/2010 00:00   13/04/2010 23:59   

 2 rows selected 

select * from accounts
where not (billing_start_date > date '2010-03-01'
        or billing_end_date < date '2010-02-01');

ACCOUNT_ID BILLING_START_DATE BILLING_END_DATE 
---------- ------------------ ------------------
      1234 09/01/2010 00:00   08/02/2010 23:59   
      4567 14/01/2010 00:00   13/04/2010 23:59   
      1234 09/02/2010 00:00   08/03/2010 23:59   

 3 rows selected 

select * from accounts
where not (billing_start_date > date '2010-04-01'
        or billing_end_date < date '2010-03-01');

ACCOUNT_ID BILLING_START_DATE BILLING_END_DATE 
---------- ------------------ ------------------
      4567 14/01/2010 00:00   13/04/2010 23:59   
      1234 09/02/2010 00:00   08/03/2010 23:59   
      1234 09/03/2010 00:00   08/04/2010 23:59   

 3 rows selected 

但我认为你想要的并不是问题中真正强调的:'让客户数量活跃'。假设“客户”是指唯一帐户 ID,您只需要修改计数:

    select m date_,count(distinct account_id)  act
    from tab, year_tab
    ...

...它给出了前几行:

YEAR_ MONTH_                                      ACT
----- ------------------------------------ ----------
2010  January                                       2 
2010  February                                      2 
2010  March                                         2 
2010  April                                         2 
2010  May                                           1 

你做错了什么是试图distinct在你的tab子查询中应用;但是 distinct 返回不同的行,并且由于日期不同,实际上并没有减少返回的行数。

这仍然与您的预期结果不完全匹配,但似乎与数据匹配(如果我对您想要的假设是正确的),并且仍然为您的三行样本提供了您的预期结果。


另一种编写查询的方法,我发现它更容易理解,并使用 ANSI 连接语法:

with t as (
    select add_months(min_date, level - 1) month_start,
        add_months(min_date, level) next_month_start
    from (
        select trunc(min(billing_start_date),'YYYY') min_date,
            add_months(trunc(max(billing_start_date),'YYYY'), 12) max_date
        from accounts
    )
    connect by level <= months_between(max_date, min_date)
)
select to_char(t.month_start,'YYYY') year_,
    to_char(t.month_start,'Month') month_,
    count(distinct a.account_id) act
from t
left join accounts a on not (billing_start_date > t.next_month_start
    or billing_end_date < t.month_start)
group by t.month_start
order by t.month_start;
于 2013-02-08T22:04:01.907 回答