count without group

前端 未结 6 2062
眼角桃花
眼角桃花 2020-12-14 01:19

I have one table named GUYS(ID,NAME,PHONE) and i need to add a count of how many guys have the same name and at the same time show all of them so i can\'t group them. exampl

6条回答
  •  眼角桃花
    2020-12-14 01:41

    DECLARE @tbl table 
    (ID int,NAME varchar(20), PHONE int)
    insert into @tbl
    select 
    1  ,'John',  335   
    union all
    select 
    2  ,'Harry', 444
    union all
    select 
    3  ,'James', 367
    union all
    select 
    4  ,'John',  742
    union all
    select 
    5  ,'John',  654
    
    SELECT
     ID
     , Name
     , Phone
     , count(*) over(partition by Name)
    FROM @tbl 
    ORDER BY ID
    

提交回复
热议问题