Sql 点票记录问题

Sql 点票记录问题,sql,sql-server,tsql,Sql,Sql Server,Tsql,这可能是一个简单的问题,但我无法理解它 我有一个MemberBusinessCats表,其中包含一个BusinessCatID和一个MemberID。。该表可以如下所示: +-----------------------+-----------------+------------+ | MemberBusinessCatID | BusinessCatID | MemberID | +-----------------------+-----------------+-------

这可能是一个简单的问题,但我无法理解它

我有一个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        |
+-----------------------+-----------------+------------+
       select BusinessCatID ,count(*) as NumMembersInCat 
       from MemberBusinessCats 
       group by BusinessCatID
如何制作一个脚本来显示以下数据

+-----------------+---------------------+
|  BusinessCatID  |  NumMembers In Cat  |
+-----------------+---------------------+
|  45             |  3                  |
+-----------------+---------------------+
|  55             |  1                  |
+-----------------+---------------------+
|  61             |  2                  |
+-----------------+---------------------+
|  23             |  1                  |
+-----------------+---------------------+
非常感谢

新杰克

试试这个

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

您需要将聚合函数与
分组依据一起使用

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

这也可以使用
count()over()编写。

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

select BusinessCatID, 
  count(distinct MemberID) NumMembersInCat
from MemberBusinessCats
group by BusinessCatID
请参见试试这个

select BusinessCatID, COUNT(BusinessCatID)
from MemberBusinessCatID
group by BusinessCatID
试试这个(或类似的东西):


基于这样一个事实,即您有一个BusinessCatID和MemberID(45,73)的组合,该组合列出了两次,但只计算了一次,您需要进行计数(不同的x)


这将统计每个BusinessCatID的不同成员(基于MemberID)。如果您不担心DUP,那么使用COUNT(MemberID)甚至COUNT(1)都可以

我理解这个问题来自不同的三元组(MemberBusinessCATID、BusinessCATID、MemberID),和BusinessCATID。在这种情况下,查看不同的三元组或双元组是相同的,但在较大的数据集中可能存在差异:

创建了一个视图:

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


以上答案中没有一个给出了他确切要求的期望输出。他接受了错误的答案。每个人都很亲近,但你比任何人都更亲近。我认为应该是
Count(Distinct MemberBusinessCatID)
实际上我的答案是正确的,而不是顺序。Count(Distinct MemberBusinessCatID)给出了相同的答案。op并没有具体说明他想要哪条路。不过,这两种方式都是简单的改变。
Select BusinessCatID, count(MemberID) as [NumMembers In Cat] 
from MemberBusinessCats 
group by BusinessCatID, MemberID
SELECT BusinessCatID, COUNT(DISTINCT MemberID) as NumMembersInCat
FROM MemberBusinessCatID
GROUP BY 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