2014-03-12 86 views
0

我有一個像計數+鮮明的SQL查詢

researcher award 

person1  award1 
person1  award2 
person2  award3 

研究表我想是計算基於研究員獎,但研究者不應該是 重複。所以在這個例子中。結果應該是

2 

因爲獎勵1和獎勵2是同一個人+ award3這是不同的人。

我已經嘗試過

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

但它說

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

因此,任何替代解決方案或變化?

+0

'SELECT研究員,計數(頒獎)從研究GROUP BY researcher'? –

+0

你想數人還是獎?換句話說,結果是「2」,因爲人1有2個獎項,或者因爲有2個人有獎勵? – mayabelle

+0

是的,因爲這兩個人都有獎項。 – Kino

回答

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

爲什麼有'as cnt'?我們不能抹去它嗎?我嘗試擦除「作爲tempTable」,但它表示子查詢應該有一個名稱。 – Kino

+0

噢,是的,我可以在哪裏放'獎勵不是空'? – Kino

+0

此查詢沒有意義 – Hogan

2

這會給你的研究員和計數

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

也許你只是想獲得的呢?

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

你可以做一個小組,並計算出不同的獎項。

SELECT count(distinct award) from research group by researcher 
0

你只需要使用GROUP BY

SELECT COUNT(DISTINCT Award) AS awardCount 

FROM  Research 

GROUP BY Researcher