2013-02-20 97 views
2

這可能是一個簡單的,但我不能讓我的頭靠近它。計數記錄問題

我有MemberBusinessCats的表,它包含一個BusinessCatID和MEMBERID ..表可以提出這樣的:

+-----------------------+-----------------+------------+ 
| MemberBusinessCatID | BusinessCatID | MemberID | 
+-----------------------+-----------------+------------+ 
| 27     | 45    | 102  | 
+-----------------------+-----------------+------------+ 
| 28     | 55    | 102  | 
+-----------------------+-----------------+------------+ 
| 29     | 61    | 102  | 
+-----------------------+-----------------+------------+ 
| 30     | 45    | 33  | 
+-----------------------+-----------------+------------+ 
| 31     | 23    | 33  | 
+-----------------------+-----------------+------------+ 
| 32     | 45    | 73  | 
+-----------------------+-----------------+------------+ 
| 32     | 61    | 73  | 
+-----------------------+-----------------+------------+ 
| 32     | 45    | 73  | 
+-----------------------+-----------------+------------+ 

如何使一個腳本來顯示下列數據

+-----------------+---------------------+ 
| BusinessCatID | NumMembers In Cat | 
+-----------------+---------------------+ 
| 45    | 3     | 
+-----------------+---------------------+ 
| 55    | 1     | 
+-----------------+---------------------+ 
| 61    | 2     | 
+-----------------+---------------------+ 
| 23    | 1     | 
+-----------------+---------------------+ 

非常感謝提前。

neojakey

回答

4

試試這個

select BusinessCatID ,count(BusinessCatID) as NumMembers_In_Cat 
    from MemberBusinessCats 
    group by BusinessCatID 

DEMO SQLFIDDLE

5

您需要使用聚合函數與GROUP BY

select BusinessCatID, count(*) NumMembersInCat 
from MemberBusinessCats 
group by BusinessCatID 

SQL Fiddle with Demo

這也可以使用count() over()寫:

select distinct BusinessCatID, 
    count(*) over(partition by BusinessCatID) NumMembersInCat 
from MemberBusinessCats 

SQL Fiddle with Demo

如果要計算每種類別中成員的數量,那麼你可以使用:

select BusinessCatID, 
    count(distinct MemberID) NumMembersInCat 
from MemberBusinessCats 
group by BusinessCatID 

SQL Fiddle with Demo

0

試試這個

select BusinessCatID, COUNT(BusinessCatID) 
from MemberBusinessCatID 
group by BusinessCatID 
1

試試這個(或像這樣):

 select BusinessCatID ,count(*) as NumMembersInCat 
     from MemberBusinessCats 
     group by BusinessCatID 
0
Select BusinessCatID, count(MemberID) as [NumMembers In Cat] 
from MemberBusinessCats 
group by BusinessCatID, MemberID 
+2

我不你認爲你想用'memberID'進行分組 - 請參閱前面的4個答案... – 2013-02-20 17:58:51

2

基於這樣的事實,你有一個組合BusinessCatID & MEMBERID被列出了兩次,但只算(45,73)一旦你需要做一個COUNT(DISTINCT x)的

SELECT BusinessCatID, COUNT(DISTINCT MemberID) as NumMembersInCat 
FROM MemberBusinessCatID 
GROUP BY BusinessCatID 

這將計算每BusinessCatID獨特成員(基於MEMBERID)。如果你不擔心dups,那麼使用COUNT(MemberID)或者COUNT(1)就可以正常工作。

+0

以上答案中的非答案給出了他*所需*的期望輸出。他接受了錯誤的答案。每個人都很親密,但你比任何人都更親近。我認爲它應該是'Count(Distinct MemberBusinessCatID)' – Kaf 2013-02-20 18:09:15

+0

實際上,我的確給出了正確的答案,而不是順序。 Count(Distinct MemberBusinessCatID)給出了相同的答案。該操作並沒有真正指定他想要的方式。儘管簡單的改變。 – 2013-02-20 18:17:59

0

我明白這個問題來自不同的TRIPLE(MemberBusinessCATID,BusinessCATID,MemberID),總和爲BusinessCATID。在這種情況下看不同三元組或雙打是相同的,但在較大的數據集可能有差異:

創建的視圖:

create view dist_catView as 
SELECT distinct MemberBusinessCATID, BusinessCATID, Member ID from cat_table 

然後

SELECT BusinessCATID, count(MemberID) from dist_catView 
group by BusinessCATID