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                  |
+-----------------+---------------------+

提前谢谢了。

新杰克

4

7 回答 7

5

您需要使用带有 a 的聚合函数GROUP BY

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

请参阅带有演示的 SQL Fiddle

这也可以写成count() over()

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

请参阅带有演示的 SQL Fiddle

如果要计算每个类别中的成员数,则可以使用:

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

请参阅带有演示的 SQL Fiddle

于 2013-02-20T17:45:34.277 回答
4

试试这个

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

演示 SQLFIDDLE

于 2013-02-20T17:45:25.613 回答
2

基于您拥有 BusinessCatID 和 MemberID (45,73) 组合的事实,该组合列出了两次,但只计算一次您需要执行 COUNT(DISTINCT x)

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

这将计算每个 BusinessCatID 的不同成员(基于 MemberID)。如果您不担心重复,那么使用 COUNT(MemberID) 甚至 COUNT(1) 都可以正常工作。

于 2013-02-20T17:58:34.290 回答
1

试试这个(或类似的东西):

       select BusinessCatID ,count(*) as NumMembersInCat 
       from MemberBusinessCats 
       group by BusinessCatID
于 2013-02-20T17:48:17.570 回答
0

试试这个

select BusinessCatID, COUNT(BusinessCatID)
from MemberBusinessCatID
group by BusinessCatID
于 2013-02-20T17:47:35.057 回答
0
Select BusinessCatID, count(MemberID) as [NumMembers In Cat] 
from MemberBusinessCats 
group by BusinessCatID, MemberID
于 2013-02-20T17:48:42.250 回答
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
于 2013-02-20T19:37:15.587 回答