每月提供利润的代理的 SQL 计数

Posted

技术标签:

【中文标题】每月提供利润的代理的 SQL 计数【英文标题】:SQL Count of Agents providing profit every month 【发布时间】:2021-04-08 22:29:18 【问题描述】:

一个表有 2 列。 AgentID 和 BusinessDate。

查询以查找每个月的代理计数。但计数应仅包括前一个月有 businessDate 的代理。

例如。

AgentID BusinessDate
1 Jan 2020
2 Jan 2020
3 Jan 2020
4 Jan 2020
5 Feb 2020
1 Feb 2020
2 Feb 2020
3 Feb 2020
6 March 2020
7 March 2020
1 March 2020
2 March 2020
2 March 2020

输出

Month Count
Jan 4
Feb 3
Mar 2

仅统计每个连续月份提供业务的代理商。 我希望我已经很好地解释了这个问题。

我尝试了以下方法:

select 
    Case 
         when (BusDate Between '01/01/2020' and '01/31/2020') and (BusDate Between '02/01/2020' and '02/28/2020')  then ID end as 'Month'
    ,BusDate

from #Temp
group by Case 
         when (BusDate Between '01/01/2020' and '01/31/2020') and (BusDate Between '02/01/2020' and '02/28/2020')  then ID end
         BusDate



select ID 
from #Temp where BusDate between '01/01/2020' and '01/31/2020' and BusFlg =1
intersect
select ID 
from #Temp where BusDate between '02/01/2020' and '02/28/2020' and BusFlg =1


select ID 
from #Temp where BusDate between '01/01/2020' and '01/31/2020'  BusDate between '02/01/2020' and '02/28/2020' and BusFlg =1


select Month(BusDate), Count(ID) Over (partition by Month(BusDate)) from #Temp
group by BusDate,ID

select Month(BusDate), Sum(case when BusDate between '01/01/2020' and '01/31/2020' then 1 
                                when Month(BusDate)=2 and BusDate between  '01/01/2020' and '01/31/2020' and 
    )
from #Temp where BusFlg=1
Group by Month(BusDate)


select distinct ID, Count(Distinct ID) AS Cnt from #Temp where Month(BusDate)=2 and 
ID in (Select Distinct ID from #Temp where Month(BusDate)=1 and BusFlg=1)
And BusFlg = 1 Group by ID

【问题讨论】:

你试过什么?你在哪里卡住了?请向我们展示您的尝试。 我找到了每个月的单独计数,但无法编写通用查询。 【参考方案1】:

您可以使用递归 CTE,只考虑 CTE 递归部分中已经存在的代理。我还打算按顺序考虑月份:一月、二月、三月。

    declare @table table(agentId int, businessdate varchar(10))
    insert into @table values
         (1 ,'Jan 2020')
        ,(2 ,'Jan 2020')
        ,(3 ,'Jan 2020')
        ,(4 ,'Jan 2020')
        ,(5 ,'Feb 2020')
        ,(1 ,'Feb 2020')
        ,(2 ,'Feb 2020')
        ,(3 ,'Feb 2020')
        ,(6 ,'March 2020')
        ,(7 ,'March 2020')
        ,(1 ,'March 2020')
        ,(2 ,'March 2020')
        ,(2 ,'March 2020');

    ;

    with
        cte_months as(
            SELECT 1 as rnk, 'Jan 2020' as mon
            union all
            select 2, 'Feb 2020'
            union all
            select 3, 'March 2020'
        ),
        cte_agents as(
            SELECT cm.rnk, agentid, businessdate
            from
                @table as t
                    inner join cte_months as cm
                    on cm.mon = t.businessdate
            where cm.rnk = 1
            union all
            -- consider only new months
            SELECT cm.rnk, t.agentid, t.businessdate
            from
                @table as t
                    inner join cte_Agents as c
                    on c.agentid = t.agentid
                    --
                    inner join cte_months as cm
                    on
                        cm.mon = t.businessdate
                    and cm.rnk = c.rnk + 1
        )
    -- SELECT * FROM cte_agents
    SELECT
        businessdate,
        count(distinct AgentId) as countOfAgents
    FROM CTE_AGENTS
    group by businessdate
businessdate countOfAgents
Feb 2020 3
Jan 2020 4
March 2020 2

【讨论】:

【参考方案2】:

您可以按月密集排列月份和代理。然后只取等级相等的行。然后聚合:

select v.yyyymm, count(distinct agentid)
from (select t.*, v.yyyymm,
             dense_rank() over (order by yyyymm) as seqnum,
             dense_rank() over (partition by agentid order by yyyymm) as seqnum_agentid
      from t cross apply
           (values (datefromparts(year(businessdate), month(businessdate), 1))
           ) v(yyyymm) 
     ) t
where seqnum = seqnum_agentid

【讨论】:

第 7 行 v 附近出现语法错误。很抱歉,我无法修复它。

以上是关于每月提供利润的代理的 SQL 计数的主要内容,如果未能解决你的问题,请参考以下文章

表中每个组的每月计数

每月计数

每月、每年分组的值计数 - Pandas

如何使用一系列日期和每月计数创建查询?

按时间范围确定的每月计数

按计数排序未正确排序 - SQL (MS Access 2007)