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

Count number of distinct values based on another set of distinct values

我正在尝试根据另一个不同的值集计算一组值:

-----------------------------------------------------    
|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 个没有为特定客户列出代理的结果。

再次感谢!

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

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;

这会将计数放在带有代理名称的列名称中,但这似乎是您想要的。

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

     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;

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

但是,在您的查询中,您不想区别对待代理,您甚至不关心它们是否来自同一客户行。为了对它们一视同仁,我们会将您的 table 转换为纯 cus-agent table,其中每一行都有一对 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 层中的布局(即在您的应用或网站中使用循环)。