根据另一组不同的值计算不同值的数量

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

我试图根据另一个不同的值集来计算一组值:

-----------------------------------------------------    
|CusID  |Agent1  |Agent2 |Agent 3 |Agent 4 |Agent 5 |
-----------------------------------------------------
|1      |Pat     |Pat    |Jane    |Simon   |Jane    |
|2      |Pater   |Pat    |Simon   |Simon   |Pat     |
|1      |Jane    |Jane   |Simon   |Pat     |Pat     |
|3      |Simon   |Simon  |Jane    |Pat     |Peter   |
|3      |Jane    |Simon  |Pat     |Pater   |Pat     |
-----------------------------------------------------

我想得到:

----------------------------------------------------------------------------
|CusIDUnq  |AgentName|Count|AgentName|Count|AgentName|Count|AgentName|Count|
----------------------------------------------------------------------------
|1         |Pat      |4    |Jane     |4    |Simon    |2    |Peter    |0    |
|2         |Pat      |2    |Jane     |0    |Simon    |2    |Pater    |1    |
|3         |Pat      |3    |Jane     |2    |Simon    |3    |Peter    |2    |
----------------------------------------------------------------------------

我怎么能用SQL做到这一点?

提前致谢!

编辑:我需要告诉客户和代理商的数量可能会随着时间而变化。所以,我认为像下面这样的解决方案也适合我。关键是,我无法将代理名称或客户ID编码到查询中。

-----------------------
|CusID|AgentName|Count|
-----------------------
|1    |Pat      |4    |
|1    |Jane     |4    |
|1    |Simon    |2    |
|2    |Pat      |2    |
|2    |Simon    |2    |
|2    |Peter    |1    |

在这里,我需要省略0结果,其中没有为特定客户列出代理。

再次,非常感谢!

sql sql-server sql-server-2016 counting
3个回答
1
投票

在您的数据模型中,cus记录恰好有五个代理,并且代理有一个优先级(或时间线等),一个代理是Agent1,一个代理是Agent2,依此类推。

但是,在您的查询中,您不希望以不同方式处理代理,您甚至不关心它们是否来自同一个cus行。为了对它们进行相同的处理,我们将您的表转换为仅仅cus-agent表,其中每一行都有一对cus和agent。然后我们可以简单地聚合和计数。

select cusid, agent, count(*)
from
(
  select cusid, agent1 as agent from mytable
  union all
  select cusid, agent2 as agent from mytable
  union all
  select cusid, agent3 as agent from mytable
  union all
  select cusid, agent4 as agent from mytable
  union all
  select cusid, agent5 as agent from mytable
) cus_agent
group by cusid, agent
order by cusid, agent;

如果您想要代理的单独列而不是行,那么请使用上面的查询并关注GUI层中的布局(即在您的应用或网站中使用循环)。


3
投票

如果我理解正确,你可以这样做:

select id,
       sum(case when 'Pat' in (Agent1, Agent2, Agent3, Agent4, Agent5)
                then 1 else 0 end
           end) as num_pat,
       sum(case when 'Jane' in (Agent1, Agent2, Agent3, Agent4, Agent5)
                then 1 else 0 end
           end) as num_jane,
       sum(case when 'Simon' in (Agent1, Agent2, Agent3, Agent4, Agent5)
                then 1 else 0 end
           end) as num_simon,
       sum(case when 'Peter' in (Agent1, Agent2, Agent3, Agent4, Agent5)
                then 1 else 0 end
           end) as num_peter
from t
group by id;

这会将列名称中的计数与代理名称放在一起,但这似乎就是您想要的。


1
投票

搞乱了逻辑后,我得到了一些结果,如下面的查询

     SELECT CUST_ID, 'Pat'
     Case when 'Pat' IN (Agent1,Agent2,
       Agent3,Agent4,Agent5)
      then 
      count(Agent1)+count(Agent2)+
         count(Agent3)+count(Agent4)+
          count(Agent5)
           else 0;
        End As 'Count_Pat',
      'Jane',

       Case when 'Jane' IN 
       (Agent1,Agent2,
       Agent3,Agent4,Agent5)
      then 
      count(Agent1)+count(Agent2)+
         count(Agent3)+count(Agent4)+
          count(Agent5)
           else 0;
        End as 'Count_Jane',
    'Simon',
     Case when 'Simon' IN 
       (Agent1,Agent2,
       Agent3,Agent4,Agent5)
      then 
      count(Agent1)+count(Agent2)+
         count(Agent3)+count(Agent4)+
          count(Agent5)
           else 0;
        End As 'Count_Simon',
         'Peter',
     Case when 'Peter' IN 
       (Agent1,Agent2,
       Agent3,Agent4,Agent5)
      then 
      count(Agent1)+count(Agent2)+
         count(Agent3)+count(Agent4)+
          count(Agent5)
           else 0;
        End as 'Count_Peter',

       From Table 

       group by cust_id;
© www.soinside.com 2019 - 2024. All rights reserved.