Count on GROUP BY and also on total number of results

sql count group by multiple columns
group by count
sql group by count greater than 1
sql count group by having
sql count(distinct group by)
group by having sql
sum of grouped count in sql query
sql group by multiple columns

I have three tables:

users which have just two columns: id: INT, name: TEXT

houses which have three columns: id: INT, user_id: INT, sold_at: DATE

users_with_house_permissions which have two columns: id: INT, user_id: INT

I have this query that founds the number of houses sold by a user (as long as it has house_permissions):

SELECT users.id as user_id, COUNT(*) as houses_sold
FROM users
JOIN users_with_house_permissions hp ON hp.user_id = users.id
LEFT JOIN houses on houses.user_id = users.id AND houses.sold_at IS NOT NULL
GROUP BY users.id

Now, how can I get the total number of rows that this return? I am trying to do pagination and I need to know the total number of rows that this query will return.

So for example, if I have the following content:

users (id, name):

1, John
2, Carla
3, Bula

users_with_house_permissions (id, user_id):

1, 1
2, 2

houses (id, user_id, sold_at):

1, 1, '2018-10-10'

I would like the result of the query to be:

user_id: 1
houses_sold: 1
total_count: 2

user_id: 2
houses_sold: 0
total_count: 2

So, total_count would represent the number of rows that this query will return.


You can use an analytic/window function count without any partition:

SELECT users.id as user_id, COUNT(*) as houses_sold,

  COUNT(*) OVER() as total_count -- count of rows returned by query

FROM users
JOIN users_with_house_permissions hp ON hp.user_id = users.id
LEFT JOIN houses on houses.user_id = users.id AND houses.sold_at IS NOT NULL
GROUP BY users.id

It works like any other analytic function; it counts over a partition, but if there is no partition specified, then it counts over the entire dataset. In this case the countover is done after the grouping is done, so while count(*) counts the number of items in the group, count(*) over() counts the number of groups in the data set

Someone else posted a sum(count(*)) over() which is the effective equivalent of counting the rows before they are grouped. If you had a data set of "who sold the house" and it went thus:

john
john
john
mary

4 houses have been sold, john sold 3, mary sold 1. There are 2 sales reps working for the agency.

COUNT(*) FROM ... GROUP BY name gives the "john sold 3, mary sold 1" and results in a dataset of:

john, 3
mary, 1

If we were to SUM that count, we'd have 4, i.e. 3+1. This is effectively the count of houses before the grouping was done. SUM(COUNT(*)) OVER() is hence the count of rows we had before we did the group. It's important to bear in mind that the COUNT(*) belongs to the GROUP BY and will become an integer count that is then later SUMmed by the SUM OVER. It would probably be easier to see if we used a subquery:

SELECT name, the_count, SUM(the_count) OVER()
FROM (SELECT name, count(*) as the_count FROM sales GROUP BY name) subquery

But because analytics are calculated after grouping is done, there isn't really any need to present it like this; the db would do this in the same way as it would do:

SELECT name, count(*), sum(count(*)) over() FROM sales GROUP BY name

Thus we get to the point where you appreciate that analytics are applied after a grouping is performed, it means that where COUNT(*) OVER() is a count of the number of rows in the dataset after the grouping operation is finished. The grouping produced john,3|mary,1 so COUNT(*) OVER() this produces 2 - the number of rows in the dataset

The documentation, if you want to read more, is titled "Windows Functions" and can be found somewhere like here: https://www.postgresql.org/docs/9.1/tutorial-window.html

This is for PG 9.1; remember to change the view to your specific version of PG

Summarize Your SQL Results with the GROUP BY clause, In today's lesson, you're going to learn about grouping results returned from From previous lessons we know how to calculate the total amount for each detail as: You can also use Distinct with COUNT to find the number of unique values​  SQL COUNT ( ) with group by and order by . In this page, we are going to discuss the usage of GROUP BY and ORDER BY along with the SQL COUNT() function. The GROUP BY makes the result set in summary rows by the value of one or more columns. Each same value on the specific column will be treated as an individual group.


You can try below - scalar subquery

SELECT users.id as user_id, COUNT(*) as houses_sold,(select count(*) from users_with_house_permissions a) as totalcount
FROM users
JOIN users_with_house_permissions hp ON hp.user_id = users.id
LEFT JOIN houses on houses.user_id = users.id AND houses.sold_at IS NOT NULL
GROUP BY users.id

How to Group and Summarize your Results (simple explanation , The SQL GROUP BY Clause is used to output a row across specified column From previous lessons, we know how to calculate the total amount for each detail as: You can also use Distinct with COUNT to find the number of unique values​  Like the group who show up to the state house every day- You just have to be creative. June runoff election results bringing the statewide total number of cases to 26,572. DHEC also


I believe you want:

select uhp.user_id, count(h.id) as houses_sold,
       sum(count(h.id)) over () as total_count
from users_with_house_permissions left join
     houses h
     on h.user_id = uhp.user_id
group by uhp.user_id;

You don't appear to need the users table, because you only want users in users_with_house_permissions.

The count() counts the number of matching rows, so you can get the rows with a 0 count. The window function then gets the total over all rows.

SQL GROUP BY | COUNT, GROUP BY typically also involves aggregates: COUNT, MAX, SUM, AVG, etc. GROUP BY can group by one or more Customer; GROUP BY Country. Results: 21 records. Problem: List the total amount ordered for each customer. SELECT​  The GROUP BY clause groups records into summary rows. GROUP BY returns one records for each group. GROUP BY typically also involves aggregates: COUNT, MAX, SUM, AVG, etc. GROUP BY can group by one or more columns.


SQL COUNT() with GROUP by, SQL COUNT() with GROUP by: The use of COUNT() function in To get data of '​working_area' and number of agents for this The GROUP BY makes the result set in summary rows by the value of one or more columns. tally () is a convenient wrapper for summarise that will either call n () or sum (n) depending on whether you're tallying for the first time, or re-tallying. count () is similar but calls group_by () before and ungroup () after. If the data is already grouped, count () adds an additional group that is removed afterwards.


SQL: COUNT Function, The SQL COUNT function is used to count the number of rows returned in a OR the syntax for the COUNT function when grouping the results by one or more We've aliased the COUNT(*) as total to make our query results more readable. You can also use COUNT() with the HAVING clause to limit a result set based on the number of rows that would be returned. Example: USE Music; SELECT ar.ArtistName, COUNT(al.AlbumName) 'Album Count' FROM Artists ar INNER JOIN Albums al ON ar.ArtistId = al.ArtistId GROUP BY ar.ArtistName HAVING COUNT(al.AlbumName) > 1; Result:


Count data by using a query - Access, Understand ways to count data, count data by using a Total row or by using a totals query You can also calculate average values, and find the smallest, largest, earliest, and You can count the number of items in a field (a column of values) by using the Count function. On the Design tab, in the Results group, click Run. SQL COUNT. The COUNT function is used when you need to know how many records exist in a table or within a group. COUNT(*) will count every record in the grouping; whereas COUNT(expression) counts every record where expression’s result isn’t null. You can also use Distinct with COUNT to find the number of unique values within a group.