2013-02-20 64 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