pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Updating 3-table dataset
3+ messages / 2 participants
[nested] [flat]

* Updating 3-table dataset
@ 2019-08-09 12:29 Moreno Andreo <moreno.andreo@evolu-s.it>
  2019-08-09 14:50 ` Re: Updating 3-table dataset Luca Ferrari <fluca1978@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Moreno Andreo @ 2019-08-09 12:29 UTC (permalink / raw)
  To: pgsql-general

Hi all,
     I don't know if that's the heat burning my brain but I can't find a 
solution to what seemed a simple operation to me.

I have 3 tables
create table t_all
{
id uuid,
ref_id uuid     (FK to t_ana.id)
};
create table t_ana
{
id uuid,
code text
};
create table t_app
{
id uuid,
code text    (subset of t_ana.code)
}
I need to update t_all set t_all.id = t_app.id having t_ana.code in 
t_app.code (I wrote it in some kind of meta-sql but I hope it's clear)
I tried to create a view but I need an INSTEAD OF trigger, since it 
spreads among 3 tables so I hope there's some faster path to achieve the 
solution....

Thanks
Moreno








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

* Re: Updating 3-table dataset
  2019-08-09 12:29 Updating 3-table dataset Moreno Andreo <moreno.andreo@evolu-s.it>
@ 2019-08-09 14:50 ` Luca Ferrari <fluca1978@gmail.com>
  2019-08-09 17:05   ` Re: Updating 3-table dataset Moreno Andreo <moreno.andreo@evolu-s.it>
  0 siblings, 1 reply; 3+ messages in thread

From: Luca Ferrari @ 2019-08-09 14:50 UTC (permalink / raw)
  To: Moreno Andreo <moreno.andreo@evolu-s.it>; +Cc: pgsql-general

On Fri, Aug 9, 2019 at 2:29 PM Moreno Andreo <moreno.andreo@evolu-s.it> wrote:
>
> Hi all,
>      I don't know if that's the heat burning my brain but I can't find a
> solution to what seemed a simple operation to me.
>
> I have 3 tables
> create table t_all
> {
> id uuid,
> ref_id uuid     (FK to t_ana.id)
> };
> create table t_ana
> {
> id uuid,
> code text
> };
> create table t_app
> {
> id uuid,
> code text    (subset of t_ana.code)
> }
> I need to update t_all set t_all.id = t_app.id having t_ana.code in
> t_app.code (I wrote it in some kind of meta-sql but I hope it's clear)
> I tried to create a view but I need an INSTEAD OF trigger, since it
> spreads among 3 tables so I hope there's some faster path to achieve the
> solution....
>

Not sure I got what you need, and I've not tested, but something like
the following:

WITH must_update AS (
SELECT app.id AS app_id, ana.id AS ana_id
FROM t_app app, t_ana ana
WHERE app.code = ana.code
)

UPDATE t_all
SET id = ( SELECT app_id FROM must_update WHERE ref_id = must_update.ana_id );

I've written the CTE because it is a little clearer in my mind, but
you can push down as a subquery of course.

Luca





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

* Re: Updating 3-table dataset
  2019-08-09 12:29 Updating 3-table dataset Moreno Andreo <moreno.andreo@evolu-s.it>
  2019-08-09 14:50 ` Re: Updating 3-table dataset Luca Ferrari <fluca1978@gmail.com>
@ 2019-08-09 17:05   ` Moreno Andreo <moreno.andreo@evolu-s.it>
  0 siblings, 0 replies; 3+ messages in thread

From: Moreno Andreo @ 2019-08-09 17:05 UTC (permalink / raw)
  To: Luca Ferrari <fluca1978@gmail.com>; +Cc: pgsql-general

Il 09/08/19 16:50, Luca Ferrari ha scritto:
> On Fri, Aug 9, 2019 at 2:29 PM Moreno Andreo <moreno.andreo@evolu-s.it> wrote:
>> Hi all,
>>       I don't know if that's the heat burning my brain but I can't find a
>> solution to what seemed a simple operation to me.
>>
>> I have 3 tables
>> create table t_all
>> {
>> id uuid,
>> ref_id uuid     (FK to t_ana.id)
>> };
>> create table t_ana
>> {
>> id uuid,
>> code text
>> };
>> create table t_app
>> {
>> id uuid,
>> code text    (subset of t_ana.code)
>> }
>> I need to update t_all set t_all.id = t_app.id having t_ana.code in
>> t_app.code (I wrote it in some kind of meta-sql but I hope it's clear)
>> I tried to create a view but I need an INSTEAD OF trigger, since it
>> spreads among 3 tables so I hope there's some faster path to achieve the
>> solution....
>>
> Not sure I got what you need, and I've not tested, but something like
> the following:
>
> WITH must_update AS (
> SELECT app.id AS app_id, ana.id AS ana_id
> FROM t_app app, t_ana ana
> WHERE app.code = ana.code
> )
>
> UPDATE t_all
> SET id = ( SELECT app_id FROM must_update WHERE ref_id = must_update.ana_id );
>
> I've written the CTE because it is a little clearer in my mind, but
> you can push down as a subquery of course.

Thanks Luca,

that's the idea I needed... now some small trimming and I think I'll be 
there

(just for the logs... the UPDATE statement needs a WHERE clause, 
otherwise it will NULL the id field in all rows where ref_id is not 
present in must_update :-) )

I owe you a beer :-)

Cheers

Moreno.

>
> Luca
>







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


end of thread, other threads:[~2019-08-09 17:05 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-08-09 12:29 Updating 3-table dataset Moreno Andreo <moreno.andreo@evolu-s.it>
2019-08-09 14:50 ` Luca Ferrari <fluca1978@gmail.com>
2019-08-09 17:05   ` Moreno Andreo <moreno.andreo@evolu-s.it>

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