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 1kPmM1-00025g-Ro for pgsql-general@arkaria.postgresql.org; Tue, 06 Oct 2020 12:46:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kPmLz-0008B9-1b for pgsql-general@arkaria.postgresql.org; Tue, 06 Oct 2020 12:46:03 +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 1kPmLy-0008B2-JD for pgsql-general@lists.postgresql.org; Tue, 06 Oct 2020 12:46:02 +0000 Received: from mail-ed1-x542.google.com ([2a00:1450:4864:20::542]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kPmLs-0006uz-2U for pgsql-general@postgresql.org; Tue, 06 Oct 2020 12:46:01 +0000 Received: by mail-ed1-x542.google.com with SMTP id j2so13383535eds.9 for ; Tue, 06 Oct 2020 05:45:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=/2HjvN2CjQfI3HukWjaI1aGgPqudYVrsdEUiFXxQ1g0=; b=g9X5MPO3vhMXApsDuPTIImq/cElAs0HgltjVbMxXOMc+fUThVDw/kIHEQO31AeTPSp 9X2702OQxdznxjBSP+TFRxzpiEcGzRDzjQX9/6bmwfieXBfiCqhYiguzMAxfRRialY6N 5dGo7vbulA1BYLpuHx83MCaR7G8nBPCCs1Lty3j/xVWKXeyeRynT9+HydFvq0Q4corSJ CFtgMiFx5iguHF7kzRAqbWd0ps2gq7ReDdO8/WdRpbHOJQHbTsYPZ2sI9LW6i9ohonQC IXuOnOVgVMDXbZm36RiLvUkZX0LR5FlML3qPajJ5z+a47v4MwLdV0RlgDa7G0ihU4xM/ Y7oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=/2HjvN2CjQfI3HukWjaI1aGgPqudYVrsdEUiFXxQ1g0=; b=F/Moqjs0tFPFfkvnd1aQ+0xd+xLFWBdp/cvIVbooBlOTD+o+VWIHfAPrBvYBIl8n+v 5OSqTS1Pm3X5Hoe6xvqc7HphArvfATcjbzm11AmImT0WbmDFwtIeiz40hhhclwM3KZMp UZGyBTW6Jvu1+hMH1dd2J7JYGdBSvqC7sZjLrJs0x5wBd+5e1w+tq4Byy1PwtXerBMon XdDopvLn3uNfYC7A55uZ2XHya6aEdhLEOHPgL0jRKR62RV+DXlFzrFg3rbWWrwpK0Wl/ 44m/1YksEapvJ3vwLO2Y6Depw9shpdrvHofNkDRolNqYwcntD6hLsoBeYF6WGoV+WpQa hGRA== X-Gm-Message-State: AOAM5323Ws1Y9juJfHb3dccwdSpWfEeYvbMSXg95Y5m+H5O4Lm8HNd2A eXjWcSGjTAFaF5SK2MNNIgU= X-Google-Smtp-Source: ABdhPJxy0X9OGGh/Hi6xsHF13FYxuabDKmYoKOfEPzsqMyXfpViD3jCKMPfHdk7lyGjmH698XEmdAw== X-Received: by 2002:aa7:d7ca:: with SMTP id e10mr5203567eds.269.1601988354533; Tue, 06 Oct 2020 05:45:54 -0700 (PDT) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id p3sm2272962edp.28.2020.10.06.05.45.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Oct 2020 05:45:53 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.15\)) Subject: Re: How to update a table with the result of deleting rows in another table From: Alban Hertroys In-Reply-To: Date: Tue, 6 Oct 2020 14:45:52 +0200 Cc: pgsql-general@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Hemil Ruparel X-Mailer: Apple Mail (2.3445.104.15) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On 6 Oct 2020, at 7:37, Hemil Ruparel = wrote: >=20 > I am trying to delete orders for a given customer on a given date and = add the cost of those orders to credit for the customer.=20 >=20 > So far, I came up with this: > ``` > with data as ( > delete from orders > where customer_id =3D > and date =3D '2020-10-05' returning price > ), total as ( > select sum(price) from data > ) > update paymentdetail > set temp_credit =3D temp_credit + (select * from total) > where customer_id =3D > ``` >=20 > which works. but is there a better way to update one table using the = result of deleting rows from another table given that I only want the = aggregate of the result? Adding the customer id to your returning clause and using update..from = could help: with data as ( delete from orders where customer_id =3D returning customer_id, price ), total as ( select customer_id, sum(price) as total_price from data group by customer_id ) update paymentdetail set temp_credit =3D temp_credit + total.total_price from total where customer_id =3D total.customer_id You could also do this using subqueries instead of CTE=E2=80=99s, that = may perform better as CTE=E2=80=99s act as optimisation fences. Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.