根据另一组不同值对不同值的数量进行计数

时间:2019-02-28 14:52:13

标签: sql sql-server sql-server-2016 counting

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

-----------------------------------------------------    
|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条未列出特定客户的代理商的结果。

再次,非常感谢!

3 个答案:

答案 0 :(得分: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 :(得分: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;

答案 2 :(得分:1)

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

但是,在查询中,您不想以不同的方式对待代理,甚至不在乎它们是否来自同一行。为了使它们全部相同,我们将您的表转换为仅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层中的布局(即在应用程序或网站中使用循环)。