Can I unite 2 selects?

Started by Sterpu Victorabout 11 years ago5 messagesgeneral
Jump to latest
#1Sterpu Victor
victor@caido.ro

Hello

Can I write a query where I receive a single result set from many
queries?
Something like this: SELECT (SELECT 1 AS t1, 2 AS t2), (SELECT 3 AS t3)
I tried exactly this but the error is: "ERROR: subquery must return
only one column"
But I don't see why it must have only one column.

Thank you

---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com

#2Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Sterpu Victor (#1)
Re: Can I unite 2 selects?

On 01/29/2015 10:58 AM, Sterpu Victor wrote:

Hello
Can I write a query where I receive a single result set from many queries?
Something like this: SELECT (SELECT 1 AS t1, 2 AS t2), (SELECT 3 AS t3)
I tried exactly this but the error is: "ERROR: subquery must return
only one column"
But I don't see why it must have only one column.

postgres@test=# SELECT * from (SELECT 1 AS t1, 2 AS t2) as t, (SELECT 3
AS t3) as s;
t1 | t2 | t3
----+----+----
1 | 2 | 3

Thank you

--
Adrian Klaver
adrian.klaver@aklaver.com

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3Edson Richter
edsonrichter@hotmail.com
In reply to: Sterpu Victor (#1)
Re: Can I unite 2 selects?

You mean union?

select 1 as t1
union
select 2 as t2
union
select 3 as t3

?

Atenciosamente,

Edson Carlos Ericksson Richter

Show quoted text

On 29-01-2015 16:58, Sterpu Victor wrote:

Hello
Can I write a query where I receive a single result set from many queries?
Something like this: SELECT (SELECT 1 AS t1, 2 AS t2), (SELECT 3 AS t3)
I tried exactly this but the error is: "ERROR: subquery must return
only one column"
But I don't see why it must have only one column.
Thank you

------------------------------------------------------------------------
<http://www.avast.com/&gt;

This email has been checked for viruses by Avast antivirus software.
www.avast.com <http://www.avast.com/&gt;

/*DISCLAIMER*:
Acest mesaj de posta electronica si documentele aferente sunt
confidentiale. Este interzisa distribuirea, dezvaluirea sau orice alt
mod de utilizare a lor. Daca nu sunteti destinatarul acestui mesaj,
este interzis sa actionati in baza acestor informatii. Citirea,
copierea, distribuirea, dezvaluirea sau utilizarea in alt mod a
informatiei continute in acest mesaj constituie o incalcare a legii.
Daca ati primit mesajul din greseala, va rugam sa il distrugeti,
anuntand expeditorul de eroarea comisa. Intrucat nu poate fi garantat
faptul ca posta electronica este un mod sigur si lipsit de erori de
transmitere a informatiilor, este responsabilitatea dvs. sa va
asigurati ca mesajul (inclusiv documentele alaturate lui) este validat
si autorizat spre a fi utilizat in mediul dvs./

#4John R Pierce
pierce@hogranch.com
In reply to: Sterpu Victor (#1)
Re: Can I unite 2 selects?

On 1/29/2015 10:58 AM, Sterpu Victor wrote:

Can I write a query where I receive a single result set from many queries?
Something like this: SELECT (SELECT 1 AS t1, 2 AS t2), (SELECT 3 AS t3)
I tried exactly this but the error is: "ERROR: subquery must return
only one column"
But I don't see why it must have only one column.

do you want

1 | 2 | 3

or

1
2
3

as your result? the first is a join, the 2nd is a union.

--
john r pierce 37N 122W
somewhere on the middle of the left coast

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#5Sterpu Victor
victor@caido.ro
In reply to: Adrian Klaver (#2)
Re: Can I unite 2 selects?

Thank you.
This is the syntax I was looking for.

------ Original Message ------
From: "Adrian Klaver" <adrian.klaver@aklaver.com>
To: "Sterpu Victor" <victor@caido.ro>; "PostgreSQL General"
<pgsql-general@postgresql.org>
Sent: 1/29/2015 9:09:31 PM
Subject: Re: [GENERAL] Can I unite 2 selects?

On 01/29/2015 10:58 AM, Sterpu Victor wrote:
Hello
Can I write a query where I receive a single result set from many
queries?
Something like this: SELECT (SELECT 1 AS t1, 2 AS t2), (SELECT 3 AS t3)
I tried exactly this but the error is: "ERROR: subquery must return
only one column"
But I don't see why it must have only one column.

postgres@test=# SELECT * from (SELECT 1 AS t1, 2 AS t2) as t, (SELECT 3
AS t3) as s;
t1 | t2 | t3
----+----+----
1 | 2 | 3

Thank you

-- Adrian Klaver
adrian.klaver@aklaver.com

---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general