Postgresql - set columns based on jsonb columns with the same name - postgresql

My table has a jsonb field report that has fields I want to copy to columns I already have in the same table. I don't want to create new records. I just want to update each row. I want to do something like this:
UPDATE project SET col1 = report->>'col1', col2 = report->>'col2', col3 = report->>'col3';

Related

is it possible to copy data from table to another based off if data in one table matches the data in the other table in a specific column

I have a table A with the following columns and data:
Tag_ = P-111
Description_ = Pump
HP_ = 100
RPM_ = 1,800
I have another table B with same columns:
Tag_ = P-111
Description_
HP_
RPM_
Is there a way when I enter data in the Tag_ column in Table B and there is matching data in the same Tag_ column in Table A that I can set up a trigger or automatic command that sends the data from the other columns in Table A to Table B?

Insert columns value into hstore data type in postgres

I have a table in postgres which has this three columns id_0,turnr and tags. tags column data type is hstore.
currently i am using this query which is not working
INSERT INTO relation_15_02_2020 (tags)
VALUES
(
'
"type"=>"restriction",
"restriction"=>"(select distinct(turnr) from relation_15_02_2020 ) "
'
);
How can i add
"type"=>"restriction",
"restriction"=>" turnr value for respective id
desired output for id_0 =1 tags
{"type"=>"restriction","restriction"=>"NoRightTurn"}
You want to update the rows, not insert new ones:
update relation_15_02_2020
set tags = hstore(array['type', 'restriction'], array['restriction', turnr])
where id_0 = 1;
alternatively
update relation_15_02_2020
set tags = hstore('type', 'restriction')||hstore('restriction', turnr)
where id_0 = 1;

Create column with aggregated value with calculation in PBI

Imagine you have two tables:
Table User:
ID, Name
Table Orders:
ID, UserID
I'm trying to create a new column in table User which should contain aggregated values of distinct count of Order.IDs.
Calculated column:
OrderCount = CALCULATE(DISTINCTCOUNT(Orders[Id]))
Alternatively if you don't/can't have a relationship between the two tables:
OrderCount2 = CALCULATE(DISTINCTCOUNT(Orders[Id]),FILTER(Orders, Orders[UserId] = User[Id]))
If all you need is to display it in some visualisation, you can use Orders[Id] directly by setting the aggregate option to Count (Distinct) in Values under Visualizations side pane.

How can I insert a a row with first ID in non empty table?

I have a table with autoincrement id. This table is non empty.
I need to update my table for insert a new row with id 1.
How can I move my entire table one row down ?
My table :
Name : rem_taux
Column : rtx_id | rtx_code | rtx_taux | rtx_date
Thanks.
three steps:
update rem_taux set rtx_id = rtx_id + 1; to move rows down
alter sequence restart with next_val (or just select nextval)
insert you row with not default rtx_id value
like this:
INSERT INTO rem_taux (rtx_id, rtx_code, rtx_taux, rtx_date)
VALUES (1, <some>, <some>, <some>)
this is with assumption you dont have FK or other dependant structure

Update from two differents table with postgresql

I want to update a table with postgresql.
In fact, I have a table (TABLE_ONE) with two column (old_id and new_id). I have a second table (TABLE_TWO) with colums (id,column1,column2,...).
I want to update the column id from TABLE_TWO. The wanted behavior is that when TABLE_ONE.id = TABLE_TWO.old_id, we set id to new_id.
How can i do that?
You want an UPDATE FROM statement:
UPDATE table_one
SET table_one.id = table_two.id
FROM table_two
WHERE table_one.id = table_two.old_id;