我想要计算有多少人注册了我们的年度会议,但我想只计算每个不同的会议一次(有时人们在单个会议上注册额外的功能,这样他们记录中的活动就会显示单个会议的多个“出席”)。答案表应在"# of 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如果我能得到任何帮助,我将不胜感激!
发布于 2015-01-07 08:11:02
请尝试对activity表中的product_code列使用COUNT(DISTINCT...)子句,如下所示:
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_nameCOUNT(DISTINCT somecolumn)为您提供了每个特定查询组中somecolumn的不同值的计数。
https://stackoverflow.com/questions/27809327
复制相似问题