pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedMultiple SELECT statements Using One WITH statement
9+ messages / 5 participants
[nested] [flat]
* Multiple SELECT statements Using One WITH statement
@ 2022-01-20 12:48 Avi Weinberg <AviW@gilat.com>
0 siblings, 2 replies; 9+ messages in thread
From: Avi Weinberg @ 2022-01-20 12:48 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Hi,
Can I have multiple select statements using one WITH statement?
WITH t AS (
Select A, B from ...
)
SELECT A into tableA FROM t where ....;
SELECT B into tableB FROM t where ....;
IMPORTANT - This email and any attachments is intended for the above named addressee(s), and may contain information which is confidential or privileged. If you are not the intended recipient, please inform the sender immediately and delete this email: you should not copy or use this e-mail for any purpose nor disclose its contents to any person.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Multiple SELECT statements Using One WITH statement
@ 2022-01-20 12:58 David G. Johnston <david.g.johnston@gmail.com>
parent: Avi Weinberg <AviW@gilat.com>
1 sibling, 1 reply; 9+ messages in thread
From: David G. Johnston @ 2022-01-20 12:58 UTC (permalink / raw)
To: Avi Weinberg <AviW@gilat.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thursday, January 20, 2022, Avi Weinberg <AviW@gilat.com> wrote:
> Hi,
>
>
>
> Can I have multiple select statements using one WITH statement?
>
>
>
> WITH t AS (
>
> Select A, B from …
>
> )
>
> SELECT A into tableA FROM t where ….;
>
>
>
> SELECT B into tableB FROM t where ….;
>
>
> With q1 as (), q2 as (), q3 as () main_query
David J.
^ permalink raw reply [nested|flat] 9+ messages in thread
* RE: Multiple SELECT statements Using One WITH statement
@ 2022-01-20 14:42 Avi Weinberg <AviW@gilat.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 2 replies; 9+ messages in thread
From: Avi Weinberg @ 2022-01-20 14:42 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Thanks David for the reply, but my question was a little different.
I know I can have multiple CTE queries like you showed, but I want to have one single WITH query, and use it in multiple queries, not just by one query the directly proceed the CTE.
Why do I need to execute the CTE query twice if I have two queries that wants to use it?
Thanks
From: David G. Johnston [mailto:david.g.johnston@gmail.com]
Sent: Thursday, January 20, 2022 2:58 PM
To: Avi Weinberg <AviW@gilat.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Multiple SELECT statements Using One WITH statement
On Thursday, January 20, 2022, Avi Weinberg <AviW@gilat.com<mailto:AviW@gilat.com>> wrote:
Hi,
Can I have multiple select statements using one WITH statement?
WITH t AS (
Select A, B from …
)
SELECT A into tableA FROM t where ….;
SELECT B into tableB FROM t where ….;
With q1 as (), q2 as (), q3 as () main_query
David J.
IMPORTANT - This email and any attachments is intended for the above named addressee(s), and may contain information which is confidential or privileged. If you are not the intended recipient, please inform the sender immediately and delete this email: you should not copy or use this e-mail for any purpose nor disclose its contents to any person.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Multiple SELECT statements Using One WITH statement
@ 2022-01-20 15:13 Josef Šimánek <josef.simanek@gmail.com>
parent: Avi Weinberg <AviW@gilat.com>
1 sibling, 0 replies; 9+ messages in thread
From: Josef Šimánek @ 2022-01-20 15:13 UTC (permalink / raw)
To: Avi Weinberg <AviW@gilat.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
čt 20. 1. 2022 v 13:48 odesílatel Avi Weinberg <AviW@gilat.com> napsal:
>
> Hi,
Hello!
>
>
> Can I have multiple select statements using one WITH statement?
>
>
>
> WITH t AS (
>
> Select A, B from …
>
> )
>
> SELECT A into tableA FROM t where ….;
>
>
>
> SELECT B into tableB FROM t where ….;
>
I think it is not possible that way. Instead you can "cache" the query
result into a temporary table. CREATE TEMPORARY TABLE ... ON COMMIT
DROP could be useful here. There is simple example mentioned in this
(https://stackoverflow.com/a/52384424/319233) stackoverflow answer.
>
> IMPORTANT - This email and any attachments is intended for the above named addressee(s), and may contain information which is confidential or privileged. If you are not the intended recipient, please inform the sender immediately and delete this email: you should not copy or use this e-mail for any purpose nor disclose its contents to any person.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Multiple SELECT statements Using One WITH statement
@ 2022-01-20 15:28 Johannes Graën <johannes@selfnet.de>
parent: Avi Weinberg <AviW@gilat.com>
1 sibling, 0 replies; 9+ messages in thread
From: Johannes Graën @ 2022-01-20 15:28 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 20/01/2022 15.42, Avi Weinberg wrote:
> Thanks David for the reply, but my question was a little different.
>
> I know I can have multiple CTE queries like you showed, but I want to
> have one single WITH query, and use it in multiple queries, not just by
> one query the directly proceed the CTE.
>
> Why do I need to execute the CTE query twice if I have two queries that
> wants to use it?
When data is going to be provided to an application via a REST API, I
find this pattern quite useful:
WITH list AS (
SELECT generate_series(1,10) AS n
)
SELECT json_build_object(
'even', (
SELECT json_agg(n)
FROM (
SELECT n
FROM list
WHERE n%2 = 0
) even
),
'odd', (
SELECT json_agg(n)
FROM (
SELECT n
FROM list
WHERE n%2 = 1
) odd
)
) obj;
If data is to be written to separate tables, writing the intermediate
result to a temporary table as explained by Josef might be the simplest
solution.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Multiple SELECT statements Using One WITH statement
@ 2022-01-20 16:45 David G. Johnston <david.g.johnston@gmail.com>
parent: Avi Weinberg <AviW@gilat.com>
1 sibling, 1 reply; 9+ messages in thread
From: David G. Johnston @ 2022-01-20 16:45 UTC (permalink / raw)
To: Avi Weinberg <AviW@gilat.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thu, Jan 20, 2022 at 7:42 AM Avi Weinberg <AviW@gilat.com> wrote:
>
>
> Thanks David for the reply, but my question was a little different.
>
> I know I can have multiple CTE queries like you showed, but I want to
> have one single WITH query, and use it in multiple queries, not just by one
> query the directly proceed the CTE.
>
> Why do I need to execute the CTE query twice if I have two queries that
> wants to use it?
>
>
>
Sorry, that was a bit of a drive-by for me. I figured you could easily
test whether your proposed query structure would work and figured maybe you
didn't realize that CTEs could be chained together.
The short answer is that a query can only output a single result set so
having two top-level select statements is simply prohibited. And result
sets are not cached between statements so it isn't like there would be any
place to store intermediate CTE results automatically. As you've been
told, you can do that with temporary tables (it's a much bigger pain if you
want something that isn't session-local).
You can always write:
CREATE VIEW cte_view AS
WITH cte AS (...)
SELECT * FROM cte;
And then incorporate that into any queries that require the results of said
CTE.
David J.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Multiple SELECT statements Using One WITH statement
@ 2022-01-20 23:32 Ken Tanzer <ken.tanzer@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: Ken Tanzer @ 2022-01-20 23:32 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Avi Weinberg <AviW@gilat.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thu, Jan 20, 2022 at 8:46 AM David G. Johnston <
david.g.johnston@gmail.com> wrote:
> You can always write:
>
> CREATE VIEW cte_view AS
> WITH cte AS (...)
> SELECT * FROM cte;
>
> And then incorporate that into any queries that require the results of
> said CTE.
>
>
Is there any advantage to still using a CTE inside the view definition, and
then selecting it? Instead of just
CREATE VIEW cte_view AS
(...);
Just curious, in case I'm missing something!
Cheers,
Ken
--
AGENCY Software
A Free Software data system
By and for non-profits
*http://agency-software.org/ <http://agency-software.org/>*
*https://demo.agency-software.org/client
<https://demo.agency-software.org/client>*
ken.tanzer@agency-software.org
(253) 245-3801
Subscribe to the mailing list
<agency-general-request@lists.sourceforge.net?body=subscribe> to
learn more about AGENCY or
follow the discussion.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Multiple SELECT statements Using One WITH statement
@ 2022-01-20 23:34 David G. Johnston <david.g.johnston@gmail.com>
parent: Ken Tanzer <ken.tanzer@gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: David G. Johnston @ 2022-01-20 23:34 UTC (permalink / raw)
To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: Avi Weinberg <AviW@gilat.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thu, Jan 20, 2022 at 4:32 PM Ken Tanzer <ken.tanzer@gmail.com> wrote:
>
>
> On Thu, Jan 20, 2022 at 8:46 AM David G. Johnston <
> david.g.johnston@gmail.com> wrote:
>
>> You can always write:
>>
>> CREATE VIEW cte_view AS
>> WITH cte AS (...)
>> SELECT * FROM cte;
>>
>> And then incorporate that into any queries that require the results of
>> said CTE.
>>
>>
> Is there any advantage to still using a CTE inside the view
> definition, and then selecting it? Instead of just
>
> CREATE VIEW cte_view AS
> (...);
>
> Just curious, in case I'm missing something!
>
>
No. That would be tunnel vision and early morning posting on my part.
David J.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Multiple SELECT statements Using One WITH statement
@ 2022-01-20 23:35 David G. Johnston <david.g.johnston@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 9+ messages in thread
From: David G. Johnston @ 2022-01-20 23:35 UTC (permalink / raw)
To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: Avi Weinberg <AviW@gilat.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thu, Jan 20, 2022 at 4:34 PM David G. Johnston <
david.g.johnston@gmail.com> wrote:
> On Thu, Jan 20, 2022 at 4:32 PM Ken Tanzer <ken.tanzer@gmail.com> wrote:
>
>>
>>
>> On Thu, Jan 20, 2022 at 8:46 AM David G. Johnston <
>> david.g.johnston@gmail.com> wrote:
>>
>>> You can always write:
>>>
>>> CREATE VIEW cte_view AS
>>> WITH cte AS (...)
>>> SELECT * FROM cte;
>>>
>>> And then incorporate that into any queries that require the results of
>>> said CTE.
>>>
>>>
>> Is there any advantage to still using a CTE inside the view
>> definition, and then selecting it? Instead of just
>>
>> CREATE VIEW cte_view AS
>> (...);
>>
>> Just curious, in case I'm missing something!
>>
>>
> No. That would be tunnel vision and early morning posting on my part.
>
>
Though I suspect if the CTE had a MATERIALIZED modifier there would be a
difference. You can force a view to be materialized.
David J.
^ permalink raw reply [nested|flat] 9+ messages in thread
end of thread, other threads:[~2022-01-20 23:35 UTC | newest]
Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-01-20 12:48 Multiple SELECT statements Using One WITH statement Avi Weinberg <AviW@gilat.com>
2022-01-20 12:58 ` David G. Johnston <david.g.johnston@gmail.com>
2022-01-20 14:42 ` Avi Weinberg <AviW@gilat.com>
2022-01-20 15:28 ` Johannes Graën <johannes@selfnet.de>
2022-01-20 16:45 ` David G. Johnston <david.g.johnston@gmail.com>
2022-01-20 23:32 ` Ken Tanzer <ken.tanzer@gmail.com>
2022-01-20 23:34 ` David G. Johnston <david.g.johnston@gmail.com>
2022-01-20 23:35 ` David G. Johnston <david.g.johnston@gmail.com>
2022-01-20 15:13 ` Josef Šimánek <josef.simanek@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox