SQL根据连接表获取组的总和

时间:2017-03-21 21:56:08

标签: sql sum

我想获得一组客户的唯一代码数量,例如:

主表:

CustomerID | Group
1000001    | A
1000002    | B
1000003    | A

加入表:

CustomerID | UniqueID
1000001    | 1
1000001    | 1
1000002    | 2
1000003    | 3  

期望的结果:

Group  | Number of Uniques
A      | 2
B      | 1 

1 个答案:

答案 0 :(得分:0)

如果我理解正确,这是一个非常简单的group byjoin查询:

select m.group, count(distinct j.uniqueid)
from maintable m join
     jointable j
     on m.customerid = j.customerid
group by m.group;