Gargoyle
Gargoyle

Reputation: 10294

Merge PostgreSQL queries with different columns

I have 4 distinct select statements, each of which returns a single column. Each select returns a different column name though. For example, the first select is 'TotalPeople' and the second select is 'Complete'.

Instead of returning two result sets, each with a single row, I'd rather return one result set, where I have as many columns as I have select statements.

Is that possible?

So lets say these are my two queries:

SELECT count(people) FROM someTable WHERE ...
SELECT count(complete) FROM someOtherTable WHERE ...

I would get back two result sets, like:

| people |  
|--------|  
| 123    |  

| complete |  
|----------|  
| 15       |  

Instead, I'd like to get back

| people | complete |  
|--------|----------|  
| 123    | 15       |  

Upvotes: 2

Views: 3511

Answers (2)

WaLinke
WaLinke

Reputation: 778

select count(table_people.id) AS PEOPLE, 
(select count(table_number.id) 
from table_number) AS NUMBER
from table_people;

Here you can use sub queries to group both queries together. Hope it helps.

Upvotes: 3

Gordon Linoff
Gordon Linoff

Reputation: 1269493

There are probably better ways to do what you want. But, you can combine the queries either in the select or the from. For instance:

select q1.TotalPeople, q2.Complete
from (<q1>) q1 cross join
     (<q2>) q2;

Upvotes: 1

Related Questions