pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to update a table with the result of deleting rows in another table
4+ messages / 4 participants
[nested] [flat]

* How to update a table with the result of deleting rows in another table
@ 2020-10-06 05:37  Hemil Ruparel <hemilruparel2002@gmail.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Hemil Ruparel @ 2020-10-06 05:37 UTC (permalink / raw)
  To: 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?

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How to update a table with the result of deleting rows in another table
@ 2020-10-06 06:45  Pankaj Jangid <pankaj@codeisgreat.org>
  parent: Hemil Ruparel <hemilruparel2002@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Pankaj Jangid @ 2020-10-06 06:45 UTC (permalink / raw)
  To: Hemil Ruparel <hemilruparel2002@gmail.com>; +Cc: 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






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How to update a table with the result of deleting rows in another table
@ 2020-10-06 12:45  Alban Hertroys <haramrae@gmail.com>
  parent: Hemil Ruparel <hemilruparel2002@gmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Alban Hertroys @ 2020-10-06 12:45 UTC (permalink / raw)
  To: Hemil Ruparel <hemilruparel2002@gmail.com>; +Cc: pgsql-general


> On 6 Oct 2020, at 7:37, Hemil Ruparel <hemilruparel2002@gmail.com> wrote:
> 
> 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?

Adding the customer id to your returning clause and using update..from could help:

with data as (
	delete from orders
	where customer_id = <customer id>
	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 = temp_credit + total.total_price
from total
where customer_id = total.customer_id


You could also do this using subqueries instead of CTE’s, that may perform better as CTE’s 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.






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How to update a table with the result of deleting rows in another table
@ 2020-10-06 20:02  Michael Lewis <mlewis@entrata.com>
  parent: Alban Hertroys <haramrae@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Michael Lewis @ 2020-10-06 20:02 UTC (permalink / raw)
  To: Alban Hertroys <haramrae@gmail.com>; +Cc: Hemil Ruparel <hemilruparel2002@gmail.com>; pgsql-general

>
> Adding the customer id to your returning clause and using update..from
> could help:
>
> with data as (
>         delete from orders
>         where customer_id = <customer id>
>         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 = temp_credit + total.total_price
> from total
> where customer_id = total.customer_id
>

You could skip the "total" cte and just update the same rows repeatedly.
I'm not sure if the same row being repeatedly updated in the same statement
creates additional row versions or just updates the existing one.


> ...CTE’s act as optimisation fences.
>

It might be worth noting PG12 changes that behavior in simple cases where
the CTE is not recursive, not referenced more than once, and is side-effect
free.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2020-10-06 20:02 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-10-06 05:37 How to update a table with the result of deleting rows in another table Hemil Ruparel <hemilruparel2002@gmail.com>
2020-10-06 06:45 ` Pankaj Jangid <pankaj@codeisgreat.org>
2020-10-06 12:45 ` Alban Hertroys <haramrae@gmail.com>
2020-10-06 20:02   ` Michael Lewis <mlewis@entrata.com>

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