计算" in"中的不同答案值功能:

时间:2015-01-06 23:56:17

标签: mysql

我想计算人们报名参加年度会​​议的次数,但我想只计算一次每次会议(有时人们会在一次会议上报名参加其他活动,因此他们的记录中的活动会显示多个“出席“单一会议”。答案表应在“#conf”字段中显示不超过2个:

SELECT distinct n.id, n.last_name, n.first_name, count(n.id) as "# of conf" from name n
join activity a
on n.id = a.id
where a.product_code in ('conf_12','conf13') and n.id <>'' and n.last_name <>''
group by n.id, n.last_name, n.first_name
order by n.last_name

我很感激我能得到任何帮助!

1 个答案:

答案 0 :(得分:1)

请尝试使用COUNT(DISTINCT...)表格中product_code列的activity条款,如下所示:

SELECT 
    n.id, n.last_name, n.first_name, 
    count(distinct a.product_code) as "# of conf" 
from 
    name n
    join activity a
        on n.id = a.id
where 
    a.product_code in ('conf_12','conf13') 
    and n.id <>'' 
    and n.last_name <>''
group by 
    n.id, n.last_name, n.first_name
order by 
    n.last_name

COUNT(DISTINCT somecolumn)为您提供每个特定查询组中somecolumn的不同值的计数。