SUM of grouped COUNT in SQL Query

前端 未结 17 1720
夕颜
夕颜 2020-12-04 08:19

I have a table with 2 fields:

ID  Name
--  -------
1   Alpha
2   Beta
3   Beta
4   Beta
5   Charlie
6   Charlie

I want to group them by name, with \'co

相关标签:
17条回答
  • 2020-12-04 08:58

    Try this:

    SELECT  ISNULL(Name,'SUM'), count(*) as Count
    FROM table_name
    Group By Name
    WITH ROLLUP
    
    0 讨论(0)
  • 2020-12-04 08:59

    For Sql server you can try this one.

    SELECT ISNULL([NAME],'SUM'),Count([NAME]) AS COUNT
    FROM TABLENAME
    GROUP BY [NAME] WITH CUBE
    
    0 讨论(0)
  • 2020-12-04 09:00

    You can use union to joining rows.

    select Name, count(*) as Count from yourTable group by Name
    union all
    select "SUM" as Name, count(*) as Count from yourTable
    
    0 讨论(0)
  • 2020-12-04 09:01

    select sum(s) from (select count(Col_name) as s from Tab_name group by Col_name having count(*)>1)c

    0 讨论(0)
  • 2020-12-04 09:03

    After the query, run below to get the total row count

    select @@ROWCOUNT
    
    0 讨论(0)
提交回复
热议问题