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

From: Hemil Ruparel <hemilruparel2002(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: How to update a table with the result of deleting rows in another table
Date: 2020-10-06 05:37:47
Message-ID: CANW1aT9p5_5Pv20-YGKfyo3ybQd2CVuBfDV6YHaeF34ZW2qovQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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.

So far, I came up with this:
```
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>
```

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?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Pankaj Jangid 2020-10-06 06:45:43 Re: How to update a table with the result of deleting rows in another table
Previous Message Han Parker 2020-10-06 03:13:06 回复: 回复: May "PostgreSQL server side GB18030 character set support" reconsidered?