Re: Updating jsonb rows

From: Michael Moore <michaeljmoore(at)gmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: postgres list <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Updating jsonb rows
Date: 2017-08-14 17:35:42
Message-ID: CACpWLjOaU-iJ-ADA9eu9sJ42jBC96oZuc92iSJuJtG1xm_h27Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi David, finally got a chance to look at this. I don't see how the SO
example applies to my case. Basically what I am doing is trying to simulate
a SQL UPDATE statement. currently I am doing this, and* it works, but it is
slow*, so I am looking for a better way.

*[In reality there are about 40 columns and up to 600 rows ]*
select jsonb_agg(row_to_json(alias)) from
(select
question_seq,
case select_type when 'EDUPROGRAM' then -10 else prompt_seq end as
prompt_seq,
jsonb_populate_recordset(null::ypxportal2__fgetquestions,j_final_rslt)
rt)alias into j_final_rslt;

The SQL equiv of this would be:
Update mytable, set prompt_seq = -10 where select_type = 'EDUPROGRAM'

On Fri, Aug 11, 2017 at 6:18 PM, David G. Johnston <
david(dot)g(dot)johnston(at)gmail(dot)com> wrote:

> On Fri, Aug 11, 2017 at 6:08 PM, Michael Moore <michaeljmoore(at)gmail(dot)com>
> wrote:
>
>> ​s​
>> elect jsonb_set(j_final_rslt, '{0,prompt_seq}','99',true) into
>> j_final_rslt;
>>
>> gives the result:
>> BEFORE [{"prompt_seq": *150*, "attribute_name": "InsuredTimeframe"},
>> {"prompt_seq": 1200, "attribute_name": "DriverOccupation"}]
>>
>> AFTER [{"prompt_seq": *99*, "attribute_name": "InsuredTimeframe"},
>> {"prompt_seq": 1200, "attribute_name": "DriverOccupation"}]
>>
>> How could I change this to ALSO change the attribute_name on the 2nd
>> record to "ABC". The AFTER result would ideally look like:
>>
>>
> ​This SO post seems like it should get you close. Basically you pull out
> the 0th element, concatenate in the values you want to change, and supply
> that result as the third jsonb_set argument.
>
> https://stackoverflow.com/questions/38883233/postgres-
> jsonb-set-multiple-keys-update
>
> David J.
>
>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message David G. Johnston 2017-08-14 18:12:54 Re: Updating jsonb rows
Previous Message Michael Moore 2017-08-14 17:13:03 Re: Always getting back a row, even with no results