如何在SQL Server中获取所有数据以及Group BY Count

Jac*_*der 1 sql sql-server sql-server-2008

所以我有ID,CustomerID,Name,Salary的表

我希望数据返回的方式是使用新列的所有数据,按客户ID显示记录组的总数,

 ID        CustomerID    Name    Salary
  1            1          John    3000
  2            1          Kim     1000
  3            2          Sarah   2000
  4            2          Jim     4000
  5            2          Kane    2000 
  6            3          Bul     2500
Run Code Online (Sandbox Code Playgroud)

所以我想要这样的东西,新的专栏按总记录显示组,

  ID        CustomerID    Name    Salary   Count
  1            1          John    3000       2
  2            1          Kim     1000       2
  3            2          Sarah   2000       3
  4            2          Jim     4000       3
  5            2          Kane    2000       3
  6            3          Bul     2500       1
Run Code Online (Sandbox Code Playgroud)

Gor*_*off 5

您正在寻找窗口函数,在这种情况下,count():

select t.*, count(*) over (partition by CustomerId) as cnt
from table t;
Run Code Online (Sandbox Code Playgroud)