I have the following table myTable :
id info
1 {'email': 'bob#bob.com', 'country': 'USA'}
2 {'email': 'test#test.com', 'country': 'DE'}
I would like to update the field country like this :
id info
1 {'email': 'bob#bob.com', 'country': 'country1'}
2 {'email': 'test#test.com', 'country': 'country2'}
I tried this :
UPDATE myTable set info->country : format('country%s', id);
but it does not work
Could you help me please ?
Thank you very much !
There's a whole chapter about that in the official documentation. You'll probably be interested in the json_set or jsonb_set functions.
You can use ||:
update mytable set info = info || jsonb_build_object('country', info ->> 'country' || id::text)
Or using jsonb_set():
update mytable set info = jsonb_set(info, '{country}', to_jsonb(info ->> 'country' || id::text))
Related
Query is
SELECT *
FROM companies c
where c.urls ->> 'Website' = '';
Here is the companies table.
urls
{'Website': '', 'Twitter': ''}
{'Website': 'www.google.com', 'Twitter: ''}
I'm querying the companies table for rows that have urls column with Website as empty string. However, this query is erroring out in dattabricks sql with:
mismatched input '->' expecting {<EOF>, ';'}(line 3, pos 13)
Does anyone know how to query the json column in databricks sql?
Take a look at the following page from the Databricks documentation: Query semi-structured data in SQL.
If the content of the column is JSON as a string, then you can make use of this syntax: <column-name>:<extraction-path>. For example:
select * from companies c
where c.urls:Website = ''
If the content of the column is a struct, then you can make use of this syntax: <column-name>:<nested-field>:
SELECT *
FROM companies c
where c.urls.Website = '';
In a table there is a column data(jsonb)
and json array like this
[
{"pid": "123", "percentage": "10"},
{"pid": "456", "percentage": "50"},
{"pid": "789", "percentage": "40"}
]
I want to update percentage 30 where pid is 789.
I used this query but not succeeded.
UPDATE table
SET data =
jsonb_set(data, '{pid}'::text[], data->'pid' || '{"percentage":"30"}'::jsonb)
WHERE (data->> 'pid') = '789' and id= '1'; [id is table's primary key]
There is no easy way to do this (except to change your data model to properly normalized model). You will have to unnest the array, and replace the percentage for the PID in question. Then aggregate the elements back into an array.
You also can't use ->> on an array as that operator doesn't work with arrays.
update the_table t
set data = (select jsonb_agg(case d.element ->> 'pid'
when '789' then d.element || '{"percentage": 30}'
else d.element
end)
from jsonb_array_elements(t.data) as d(element))
where id = 1
and data #> '[{"pid": "789"}]'
I have a column with string entries with the following format (for example)
"[ { 'state' : 'CA', 'tax_amount' : 3},{ 'state' : 'AZ', 'tax_amount' : 4}]"
I want to sum through the tax_amounts in each entry to get a total tax amount for each row. How can I do this in PostgreSQL?
I would use a scalar sub-query:
select t.*,
(select sum((item ->> 'tax_amount')::int)
from jsonb_array_elements(t.the_column) as x(item)) as total_tax
from the_table t
Online example
You can use JSONB_POPULATE_RECORDSET() function such as
WITH t(js) AS
(
SELECT '[ { "state" : "CA", "tax_amount" : 3},
{ "state" : "AZ", "tax_amount" : 4}]'::JSONB
)
SELECT SUM(tax_amount) AS total_tax_amount
FROM t
CROSS JOIN JSONB_POPULATE_RECORDSET(NULL::record,js )
AS tab(state VARCHAR(10), tax_amount INT)
total_tax_amount
----------------
7
after fixing the syntax of the JSON object by replacing single-quotes with double-quotes, and double-quotes wrapping up whole object with single-quotes.
Demo
I'm trying to read the column which type is json, values in column look like this
column1
---------------------------------------------
"[{'name': 'Kate', 'position': 'painter'}]"
Im using this query, but all I get is null, what can I do to get the values for each keys?
SELECT
column1 ->> 'name' AS name
FROM
table1;
Then you use jsonb_pretty that Returns from_json as indented JSON text.
select jsonb_pretty('[{"name": "Kate", "position": "painter"}]');
Output display you:
jsonb_pretty
-------------------------------
[ +
{ +
"name": "Kate", +
"position": "painter"+
} +
]
so in your case you use
SELECT jsonb_pretty(column1) AS name FROM table1;
Use json_array_elements function:
SELECT json_array_elements(t) -> 'name'
FROM table1;
I have a database in that a need to set different values in a column dependent upon values in a second column.
I wish to do this (descibed in not working code but I think you'll understand):
UPDATE new
(SET Domain = 'INFO'
WHERE new.node = 'ABC' or new.node = 'DEF')
or
(SET Domain = 'FOO'
WHERE new.node = 'GHI' or new.node = 'JKL')
but I can't figure out how. Can someone help me?
Regards // PS
Use the Switch statement.
UPDATE [new]
SET [Domain] = Switch(
[new].node = 'ABC' OR [new].node = 'DEF', 'INFO',
[new].node = 'GHI' OR [new].node = 'JKL', 'FOO',
);
Related link: Microsoft Access - Case Query