简体   繁体   English

Count + Distinct SQL Query

[英]Count + Distinct SQL Query

I have a research table like 我有一张研究表

researcher    award

 person1      award1
 person1      award2
 person2      award3

What I want to get is to count the award based on researcher but it researcher shouldnt be repeated. 我想得到的是根据研究人员计算奖项,但研究人员不应该重复。 So in this example. 所以在这个例子中。 The result should be 结果应该是

 2

Coz award1 and award2 is the same person + award3 which is a different person. Coz award1和award2是同一个人+ award3,是另一个人。

I already tried 我已经试过了

SELECT count(award) from research where researcher=(select distinct(researcher) from researcher)

But it says 但它说

ERROR:  more than one row returned by a subquery used as an expression

So any alternative solution or changes? 那么任何替代解决方案或变化?

This will give you researcher and count 这将给你研究员和计数

select researcher, count(*) as c
from table
group by researcher

maybe you only want awarded ones? 也许你只想要获奖?

select researcher, count(*) as c
from table
where award is not null
group by researcher

你可以做一个小组并计算不同的奖项。

SELECT count(distinct award) from research group by researcher

You just need to use GROUP BY : 您只需要使用GROUP BY

SELECT    COUNT( DISTINCT Award ) AS awardCount 

FROM      Research 

GROUP BY  Researcher
select count(*) from (select researcher, count(*) 
from MyTable
group by researcher) as tempTable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM