Sql 对多连接语句的查询结果计数

Sql 对多连接语句的查询结果计数,sql,Sql,我已经为我们的系统写了一个查询,该查询提取了所有加拿大联系人及其所在组的列表 现在,对于在多个组中的人(他们的loginid出现多次),我需要确定他们在其中的组数(返回计数)。但是,我不确定如何进行计数 我希望我的输出采用以下格式: select cnt.loginid, grp.last_name as 'Group Name' from contact cnt right join grpmem list on cnt.contact_uuid = list.member

我已经为我们的系统写了一个查询,该查询提取了所有加拿大联系人及其所在组的列表

现在,对于在多个组中的人(他们的loginid出现多次),我需要确定他们在其中的组数(返回计数)。但是,我不确定如何进行计数

我希望我的输出采用以下格式:

select cnt.loginid, grp.last_name as 'Group Name' 
from contact cnt 
    right join grpmem list on cnt.contact_uuid = list.member 
    left join contact grp on grp.contact_uuid = list.group_id 
    join contact_acctyp cntacc on cnt.contact_uuid = cntacc.contact_uuid
where cntacc.c_acctyp_id in (select id from acctyp_v2 where sym like 'CDN%')

我似乎不知道如何把我写的东西变成那样

假设您想做的只是聚合您已经返回的信息,而不详细查看您的查询,下面是一个猜测:

| USER ID | # of Groups |

到现在为止,一直都还不错。现在我只需要一种方法来列出count(*)>1的位置意味着添加:HAVING count(*)>1
select 
    cnt.loginid, 
    COUNT(*)
from contact cnt 
    right join grpmem list on cnt.contact_uuid = list.member 
    left join contact grp on grp.contact_uuid = list.group_id 
    join contact_acctyp cntacc on cnt.contact_uuid = cntacc.contact_uuid
where cntacc.c_acctyp_id in (select id from acctyp_v2 where sym like 'CDN%')
GROUP BY
     cnt.loginid