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 1kPgjW-0005qz-VB for pgsql-general@arkaria.postgresql.org; Tue, 06 Oct 2020 06:45:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kPgjV-0001Dt-T1 for pgsql-general@arkaria.postgresql.org; Tue, 06 Oct 2020 06:45:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kPgjV-0001Dm-It for pgsql-general@lists.postgresql.org; Tue, 06 Oct 2020 06:45:57 +0000 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kPgjT-0004lB-Ed for pgsql-general@postgresql.org; Tue, 06 Oct 2020 06:45:57 +0000 Received: by mail-pg1-x52c.google.com with SMTP id g9so6929617pgh.8 for ; Mon, 05 Oct 2020 23:45:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=codeisgreat-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:organization:references:date:in-reply-to :message-id:user-agent:mime-version; bh=DeKLsIy/+q23w97CA9nT+rGTffvGeMellNF6KTLFIQM=; b=JCDUtEnqXBi1kKsd0eKlScaL2d2hbgOKbt/xIHn1yFlYlgw7YbS+FKS2fSZ4TNWbT+ jUjukn9CAZV9sT0kIdmRbkdQLfn6qMU03xWgFRyQzQvJqJ2a965qYtCEYPBgYgjwFvJB wG9TT5dCTbjgFVaTHI7sqGXgr9MYimxy5laGLmUEOWlnfehhBJvrH02u50vVaIR1QyF+ AvjUeZy89Uadtc3Fj9VV+omS2banX1EhL/Ff0kmWhKljGoJuSJ3LJ9YKImnW1vCafRk4 D+LbA1bYxsYZ3tKl/EyFQ5g7gEo6P3CyZXnDGuzUXukcyD0BV9nh5WljFJsbCIG+1msP ruDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:organization:references:date :in-reply-to:message-id:user-agent:mime-version; bh=DeKLsIy/+q23w97CA9nT+rGTffvGeMellNF6KTLFIQM=; b=YHfhImuNy25BFj787tvcuUrJQ6/yWNKJwo4lwZIZ29XrhQRn4bj1+mXFy5Gkx5506g +Nk/2UoFClZVf4M6cdYciou6/NYEHckluWYbsWLIWV10GEJcaCReAtrRvQboV0cvEHSc XxqTKuSyV5EdGuRXUd9fnm241SkWIRB+35JZ2cw7JucehWzEZl4C6xXwWq02nrjm2zy3 SPt1h2n87CQ90yeeaqzXu4Rks34XGXySQkjYqOWl67Zu+SQTvKQhTmzGbiu1TC3IfcMR wpavn3CcjGdu4nvpj2c9B/tuLwcXWv9xGuf/0gKnDmT57Lg7hGHe7FxxgDRpaHskaKVN IvTw== X-Gm-Message-State: AOAM531+NKbzOiFIN2qB6ljVzUZ1Azw8nSODdswd276nT5N3WoPUQ564 c6Cz289OqMnLkinAZFytsNNk3eGQP4XtlQ== X-Google-Smtp-Source: ABdhPJzFP1erXw/lJm8Tf/WXL+CmD6zvg3bPQrQ40XEdmq8ZFMlnVg7i8hdS3NTI4hs9aG1oakvVBw== X-Received: by 2002:a63:5102:: with SMTP id f2mr2870576pgb.15.1601966752350; Mon, 05 Oct 2020 23:45:52 -0700 (PDT) Received: from mb2.local ([2405:201:5c06:7893:ddde:1721:751:11f6]) by smtp.gmail.com with ESMTPSA id l10sm1712408pgp.25.2020.10.05.23.45.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Oct 2020 23:45:51 -0700 (PDT) From: Pankaj Jangid To: Hemil Ruparel Cc: pgsql-general@postgresql.org Subject: Re: How to update a table with the result of deleting rows in another table Organization: Code Is Great Foundation References: Date: Tue, 06 Oct 2020 12:15:43 +0530 In-Reply-To: (Hemil Ruparel's message of "Tue, 6 Oct 2020 11:07:47 +0530") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (darwin) MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Oct 06 2020, Hemil Ruparel wrote: > with data as ( > delete from orders > where customer_id = > and date = '2020-10-05' returning price > ), total as ( > select sum(price) from data > ) > update paymentdetail > set temp_credit = temp_credit + (select * from total) > where customer_id = Not sure about better way but will this also not work? I just removed the second clause. #+BEGIN_SRC sql with data as ( delete from orders where customer_id = and date = '2020-10-05' returning price ) update paymentdetail set temp_credit = temp_credit + (select sum(price) from data) where customer_id = #+END_SRC