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
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
In PostgreSQL this works for me:
select count(count.counts)
from
(select count(*) as counts
from table
group by concept) as count;
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
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.
Try this query:
select top 1 TotalRows = count(*) over ()
from yourTable
group by column1, column2
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