如何在sql中计算类似的数据?

时间:2016-01-15 10:08:36

标签: mysql sql

我有一张表tmp

查询:

select * from tmp

enter image description here

我希望以下列方式得到结果:

customer_id | subscriber_id  | totalSubscribers    
320         | 433            |     3  
320         | 434            |     3   

你能告诉我如何实现这个目标吗?

2 个答案:

答案 0 :(得分:2)

以下是您提问的解决方案

SELECT customer_id , subscriber_id , count(*) AS totalSubscribers 
FROM `tmp` GROUP BY 1,2

SELECT customer_id , subscriber_id , count(*) AS totalSubscribers 
FROM `tmp` GROUP BY customer_id , subscriber_id

这是执行查询的screnshot enter image description here

答案 1 :(得分:0)

以下是答案

  SELECT customer_id , subscriber_id , count(*) as [TOTAL]
    FROM  tmp
    GROUP BY customer_id , subscriber_id