Skip to content
Advertisement

I need to get count of total count for the query I had in postgresql

I created a select query as following, now I need to get the total count of the “No.of Ideas generated” column in a separate row as total which will have a count of the individual count of particular idea_sector and idea_industry combination.

Query:

Output:

Required output:

Advertisement

Answer

You can accomplish this with grouping sets. That’s where we tell postgres, in the GROUP BY clause, all of the different ways we would like to see our result set grouped for the aggregated column(s)

This generates two grouping sets. One that groups by idea_sector, idea_industry granularity like in your existing sql and another that groups by nothing, essentially creating a full table Total.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement