Duplicate column with different name - postgresql

I have a column on my table called RecordId. I want to duplicate this column to a column named BookId. I know I can add column like this:
ALTER TABLE products ADD COLUMN BookId;
But how do I populate the value from the other column?

UPDATE products
SET BookId = RecordId
WHERE RecordId > 0

Related

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

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';

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;

How to set an object to null

How to set an object to null.
Ex:
my object samp contains three fileds
samp.field1,samp.field2.sampfield3
If i set samp:= null;
im getting errors is there a way to set the object value to null.
An sql database does not know about objects, it deals with rows in table.
To remove a row use DELETE :
e.g. :
DELETE FROM samp WHERE id = 12345;
DELETE FROM samp WHERE field1 = 'Delete Me';
The first example is typical to remove individual rows uing their primary key (id in this case)
The second example will remove a group of rows which have a speciic value for a field.