[SOLVED] PostgreSQL Function – Return a single row from multiple queries


I would like to write a Postgres function which takes various stats from other tables and combines the results into a single row.


select count(*) as acount from tableA
select count(*) as bcount from tableB
select count(*) as ccount from tableC


acount, bcount, ccount   as a single record

Obviously the "count" functions listed above are a lot more complex than that, and may even be different amounts of counts for each table.

So ideally is it possible to store them as variables and then build a record from the variables ?


You can use cross join

select * from
(select count(*) as acount from tableA) as a
,(select count(*) as bcount from tableB) as b
,(select count(*) as ccount from tableC) as c

Answered By – RF1991

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *