Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nAZMh-0007JB-6z for pgsql-general@arkaria.postgresql.org; Thu, 20 Jan 2022 15:28:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nAZMg-0001Ru-3Y for pgsql-general@arkaria.postgresql.org; Thu, 20 Jan 2022 15:28:42 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nAZMf-0001Rk-Nf for pgsql-general@lists.postgresql.org; Thu, 20 Jan 2022 15:28:41 +0000 Received: from mail-1.server.selfnet.de ([2001:7c7:2100:400::37]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nAZMb-0000Ya-DZ for pgsql-general@lists.postgresql.org; Thu, 20 Jan 2022 15:28:40 +0000 Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 7C4A0409D2 for ; Thu, 20 Jan 2022 16:28:31 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=selfnet.de; s=selfnet; t=1642692511; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XvXOEWy798R10kJaZQ6lT2iPqEgCxZDOWnWJFgTaaa8=; b=lUeX3953GV2qTydYvIaNfbCShsKH71nNpF1CJHYhbN/Pr8N0C7xVFWaxxCd6mz3bf+M4J2 iYD2YWGDQ1yz0FWBcLBvb7o/YGVCkjPuIB0TqYN1X04kFa33g+ErqNre6ozj9Ow9vhH55k 0yNgXxiqGYCY63OoUvcg4sE6GKMCiECiNejaH32WJ2natMHrLIEFunbwMTwdjqcp+TX7f3 a8xmpWic6upUPz34/WOIZL9beEneOqAB29oen7vQILgR7cdTJShldKWWgNhRBZXBtwBUlK 6TkCWZfJSVOPQI+sPvf/ucgPxL7oOtSc9Q9/RfGTv3DOVPMxmRaf5I+29BpiXw== Message-ID: <0fe21602-be12-6914-4f0d-53f116f969b5@selfnet.de> Date: Thu, 20 Jan 2022 16:28:29 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: Multiple SELECT statements Using One WITH statement Content-Language: en-US To: pgsql-general@lists.postgresql.org References: From: =?UTF-8?Q?Johannes_Gra=c3=abn?= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Authentication-Results: mail-1.server.selfnet.de; auth=pass smtp.auth=johannes smtp.mailfrom=johannes@selfnet.de List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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.