Re: How to update a table with the result of deleting rows in another table

From: Pankaj Jangid <pankaj(at)codeisgreat(dot)org>
To: Hemil Ruparel <hemilruparel2002(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to update a table with the result of deleting rows in another table
Date: 2020-10-06 06:45:43
Message-ID: m28scjkg2w.fsf@codeisgreat.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Oct 06 2020, Hemil Ruparel wrote:

> with data as (
> delete from orders
> where customer_id = <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 = <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 = <customer id>
and date = '2020-10-05' returning price
)
update paymentdetail
set temp_credit = temp_credit + (select sum(price) from data)
where customer_id = <customer id>
#+END_SRC

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Mark 2020-10-06 08:23:28 Re: How to execute the sql file in PSQL
Previous Message Hemil Ruparel 2020-10-06 05:37:47 How to update a table with the result of deleting rows in another table