Count number of records returned by group by

Sql ServerTsqlCountGroup By

Sql Server Problem Overview


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, column_4

Gives me,

1
1
2

I need to count the above records to get 1+1+1 = 3.

Sql Server Solutions


Solution 1 - Sql Server

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

Solution 2 - Sql Server

The simplest solution is to use a derived table:

Select Count(*)
From	(
		Select ...
		From TempTable
		Group By column_1, column_2, column_3, column_4
		) As Z

Another solution is to use a Count Distinct:

Select ...
	, ( Select Count( Distinct column_1, column_2, column_3, column_4 )
		From TempTable ) As CountOfItems
From TempTable
Group By column_1, column_2, column_3, column_4

Solution 3 - Sql Server

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.

Solution 4 - Sql Server

I was trying to achieve the same without subquery and was able to get the required result as below

SELECT DISTINCT COUNT(*) OVER () AS TotalRecords
FROM temptable
GROUP BY column_1, column_2, column_3, column_4

Solution 5 - Sql Server

How about:

SELECT count(column_1)
FROM
    (SELECT * FROM temptable
    GROUP BY column_1, column_2, column_3, column_4) AS Records

Solution 6 - Sql Server

A CTE worked for me:

with cte as (
  select 1 col1
  from temptable
  group by column_1
)

select COUNT(col1)
from cte;

Solution 7 - Sql Server

In PostgreSQL this works for me:

select count(count.counts) 
from 
    (select count(*) as counts 
     from table 
     group by concept) as count;

Solution 8 - Sql Server

You could do:

select sum(counts) total_records from (
    select count(*) as counts
    from temptable
    group by column_1, column_2, column_3, column_4
) as tmp

Solution 9 - Sql Server

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

Solution 10 - Sql Server

Try this query:

select top 1 TotalRows = count(*) over () 
from yourTable
group by column1, column2

Solution 11 - Sql Server

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

Solution 12 - Sql Server

How about using a COUNT OVER (PARTITION BY {column to group by}) partitioning function in SQL Server?

For example, if you want to group product sales by ItemID and you want a count of each distinct ItemID, simply use:

SELECT
{columns you want} ,
COUNT(ItemID) OVER (PARTITION BY ItemID) as BandedItemCount ,
{more columns you want}... ,
FROM {MyTable}

If you use this approach, you can leave the GROUP BY out of the picture -- assuming you want to return the entire list (as you might do report banding where you need to know the entire count of items you are going to band without having to display the entire set of data, i.e. Reporting Services).

Solution 13 - Sql Server

Following for PrestoDb, where FirstField can have multiple values:

select *
            , concat(cast(cast((ThirdTable.Total_Records_in_Group * 100 / ThirdTable.Total_Records_in_baseTable) as DECIMAL(5,2)) as varchar), '%') PERCENTage
from 
(
    SELECT FirstTable.FirstField, FirstTable.SecondField, SecondTable.Total_Records_in_baseTable, count(*) Total_Records_in_Group
    FROM BaseTable FirstTable
    JOIN (
            SELECT FK1, count(*) AS Total_Records_in_baseTable 
            FROM BaseTable
            GROUP BY FK1
        ) SecondTable
    ON FirstTable.FirstField = SecondTable.FK1
    GROUP BY FirstTable.FirstField, FirstTable.SecondField, SecondTable.Total_Records_in_baseTable
    ORDER BY FirstTable.FirstField, FirstTable.SecondField
) ThirdTable

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChrisView Question on Stackoverflow
Solution 1 - Sql ServergbnView Answer on Stackoverflow
Solution 2 - Sql ServerThomasView Answer on Stackoverflow
Solution 3 - Sql ServercartbeforehorseView Answer on Stackoverflow
Solution 4 - Sql ServerManish MulaniView Answer on Stackoverflow
Solution 5 - Sql ServerPedroC88View Answer on Stackoverflow
Solution 6 - Sql ServerSQL PeteView Answer on Stackoverflow
Solution 7 - Sql ServeromarView Answer on Stackoverflow
Solution 8 - Sql ServertroutinatorView Answer on Stackoverflow
Solution 9 - Sql ServerArifView Answer on Stackoverflow
Solution 10 - Sql ServerMarimuthu ShanmugasundaramView Answer on Stackoverflow
Solution 11 - Sql ServerNitish SahooView Answer on Stackoverflow
Solution 12 - Sql ServerJohnny BView Answer on Stackoverflow
Solution 13 - Sql ServerSUKUMAR SView Answer on Stackoverflow