簡體   English   中英

Count + Distinct SQL Query

[英]Count + Distinct SQL Query

我有一張研究表

researcher    award

 person1      award1
 person1      award2
 person2      award3

我想得到的是根據研究人員計算獎項,但研究人員不應該重復。 所以在這個例子中。 結果應該是

 2

Coz award1和award2是同一個人+ 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

那么任何替代解決方案或變化?

這將給你研究員和計數

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

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

SELECT count(distinct award) from research group by researcher

您只需要使用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