Count number of records returned by group by

前端 未结 13 1783
梦毁少年i
梦毁少年i 2020-11-30 20:01

How do I count the number of records returned by a group by query,

For eg:

select count(*) 
from temptable
group by column_1, column_2, column_3, co         


        
相关标签:
13条回答
  • 2020-11-30 20:30

    You can do both in one query using the OVER clause on another COUNT

    select
        count(*) RecordsPerGroup,
        COUNT(*) OVER () AS TotalRecords
    from temptable
    group by column_1, column_2, column_3, column_4
    
    0 讨论(0)
  • 2020-11-30 20:30

    In PostgreSQL this works for me:

    select count(count.counts) 
    from 
        (select count(*) as counts 
         from table 
         group by concept) as count;
    
    0 讨论(0)
  • 2020-11-30 20:30

    you can also get by the below query

    select column_group_by,count(*) as Coulm_name_to_be_displayed from Table group by Column;
    
    -- For example:
    select city,count(*) AS Count from people group by city
    
    0 讨论(0)
  • 2020-11-30 20:32

    I know it's rather late, but nobody's suggested this:

    select count ( distinct column_1, column_2, column_3, column_4) 
    from   temptable
    

    This works in Oracle at least - I don't currently have other databases to test it out on, and I'm not so familiar with T-Sql and MySQL syntax.

    Also, I'm not entirely sure whether it's more efficient in the parser to do it this way, or whether everyone else's solution of nesting the select statement is better. But I find this one to be more elegant from a coding perspective.

    0 讨论(0)
  • 2020-11-30 20:33

    Try this query:

    select top 1 TotalRows = count(*) over () 
    from yourTable
    group by column1, column2
    
    0 讨论(0)
  • 2020-11-30 20:34

    Can you execute the following code below. It worked in Oracle.

    SELECT COUNT(COUNT(*))
    FROM temptable
    GROUP BY column_1, column_2, column_3, column_4
    
    0 讨论(0)
提交回复
热议问题