0

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.

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?

4
  • SELECT researcher, count(award) FROM research GROUP BY researcher? Commented Mar 12, 2014 at 16:21
  • Are you trying to count the people or the awards? In other words, is the result "2" because person 1 has 2 awards, or because there are 2 people that have awards? Commented Mar 12, 2014 at 16:21
  • Yes, because the 2 people have awards. Commented Mar 12, 2014 at 16:25
  • This question is very unclear. Commented Mar 12, 2014 at 18:58

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

Comments

0
select count(*) from (select researcher, count(*) 
from MyTable
group by researcher) as tempTable;

6 Comments

why is that there is 'as cnt'? can't we just erase it? I tried erasing 'as tempTable' but it says subquery should have a name.
And oh yes, where can I put 'where award is not null'?
This query makes no sense
removed as cnt since there is no need.
@Hogan this exactly returns what the OP requested. It returns the number of awards without double counting the researchers. It counts the rows of the group by query...
|
0

You could just do a group by and count the distinct award.

SELECT count(distinct award) from research group by researcher

Comments

0

You just need to use GROUP BY:

SELECT    COUNT( DISTINCT Award ) AS awardCount 

FROM      Research 

GROUP BY  Researcher

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.