在下表中,我想计算至少提交一个问题解决方案的人数。
下表如下:
身份证user_name problem_name约翰一书2 约翰树3 约翰·苏姆斯4马丁总和5棵马丁树6棵马丁树7 吉姆树
这是我正在寻找的结果:
总和 2树木 3
我会按problem_name
分组,但前提是user_name
列相同。我不知道如何在SQL中编写它。
使用 count(distinct)
:
select problem_name, count(distinct user_name) as count
from mytable
group by problem_name
select table_2.problem_name, count(*) from
(select user_name, problem_name
from table_1
group by user_name, problem_name) as table_2
group by problem_name
我认为使用子查询比使用distinct
更快,如果你有很多行,但我不确定。