pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Pankaj Jangid <pankaj@codeisgreat.org>
To: Hemil Ruparel <hemilruparel2002@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: How to update a table with the result of deleting rows in another table
Date: Tue, 06 Oct 2020 12:15:43 +0530
Message-ID: <m28scjkg2w.fsf@codeisgreat.org> (raw)
In-Reply-To: <CANW1aT9p5_5Pv20-YGKfyo3ybQd2CVuBfDV6YHaeF34ZW2qovQ@mail.gmail.com>
References: <CANW1aT9p5_5Pv20-YGKfyo3ybQd2CVuBfDV6YHaeF34ZW2qovQ@mail.gmail.com>

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






view thread (4+ messages)  latest in thread

Message-ID: <m28scjkg2w.fsf@codeisgreat.org>
Permalink:  ../m28scjkg2w.fsf@codeisgreat.org/
Also on:    postgresql.org/message-id/m28scjkg2w.fsf@codeisgreat.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: pankaj@codeisgreat.org, hemilruparel2002@gmail.com
  Subject: Re: How to update a table with the result of deleting rows in another table
  In-Reply-To: <m28scjkg2w.fsf@codeisgreat.org>
  References: <CANW1aT9p5_5Pv20-YGKfyo3ybQd2CVuBfDV6YHaeF34ZW2qovQ@mail.gmail.com> <m28scjkg2w.fsf@codeisgreat.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox