Multiple SELECT statements Using One WITH statement
Multiple SELECT statements Using One WITH statement
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 ….;
Re: Multiple SELECT statements Using One WITH statement
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 ….;
RE: Multiple SELECT statements Using One WITH statement
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> 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.
Re: Multiple SELECT statements Using One WITH statement
čt 20. 1. 2022 v 13:48 odesílatel Avi Weinberg 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.
Re: Multiple SELECT statements Using One WITH statement
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.
Re: Multiple SELECT statements Using One WITH statement
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?
Re: Multiple SELECT statements Using One WITH statement
You can always write:CREATE VIEW cte_view ASWITH cte AS (...)SELECT * FROM cte;And then incorporate that into any queries that require the results of said CTE.
Cheers,
Re: Multiple SELECT statements Using One WITH statement
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 ASWITH 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 justCREATE VIEW cte_view AS(...);Just curious, in case I'm missing something!
Re: Multiple SELECT statements Using One WITH statement
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 ASWITH 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 justCREATE 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.