How to set values for one column based on another?
Goal: When in the DB table the column Remote = table SO in the column
Thrunode -> Set in the table DB the column customer = table SO
DB = tbl_db_collecting
SO = tb_systemshc
sql:
UPDATE tbl_db_collecting SET
tbl_db_collecting.customer = tb_systemshc.environment
FROM tb_systemshc
WHERE tbl_db_collecting.lower(remote) = tb_systemshc.lower(thrunode)
output:
SQL Error [3F000]: ERROR: schema "tbl_db_collecting" does not exist
Is this what you are looking for?
update tbl_db_collecting
set customer = tb_systemshc.environment
from tb_systemshc
where lower(tbl_db_collecting.remote) = lower(tb_systemshc.thrunode);
When you write tbl_db_collecting.lower(remote), PostgreSQL parses that as if you are looking for a lower() function defined in schema tbl_db_collecting.
Related
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?
I created a DataSet which points to a table in my database. The name of the table is set as dynamic content: #concat(dataset().db_prefix, '_Baseline_CIs'). This works when checking in the Dataset through 'Preview Data'. The table contents are shown.
BUT: When using the dataset in the Data Warngling Flow, the M-query fails with the following error:
Expression.Error: The key didn't match any rows in the table.
AdfDoc = Sql.Database("oedudigital.database.windows.net", "IntegratedEnvironments"),
InputTable = AdfDoc{[Schema = "dbo", Item = "undefined"]}[Data]
As you can see, the table name concatenation has returned 'undefined'. Is this a bug?
BR, Denis
If I understand it right you have the DataSet which is parameter , at least that was the case on my side . Under the AdFResouce you will see the dataset name . You will have to pass the table name as
AdfDoc{[Schema = "dbo", Item = "TableName"]}[Data]
and then it will bring in the records .
Sample data:
I am trying update a column with values from multiple columns in another table if two columns match.
Consider the following query:
UPDATE application_table
SET asset_list = asset_table.asset_name
FROM asset_table
WHERE application_table.application_name = asset_table.applications;
My table structure is:
application_table:
"asset_list"; "text[]"
"application_name"; "character varying"
asset_table:
"asset_name"; "character varying"
"applications"; "character varying"
I get the following error:
ERROR: column "asset_list" is of type text[] but expression is of type character varying
Line 12 SET asset_list = asset_table.asset_name
What you need to do is aggregate the asset_name per applications value and set asset_list to that aggregated value.
Problem is you can't do something like
UPDATE ..
SET asset_list = ARRAY_AGG(asset_name)
FROM ...
because aggregate functions are not allowed in updates like that.
So here's two other ways to do it:
UPDATE app_table
SET asset_list = _asset_list
FROM (
SELECT applications, ARRAY_AGG(asset_name ORDER BY asset_name) AS _asset_list
FROM asset_table
GROUP BY applications
) AS a
WHERE app_name = applications;
https://www.db-fiddle.com/f/pKB5k6Lexwzqv6ZbCCdJay/0
This first builds a result set of distinct application names and an array of all the asset_names for each of the app names. Then it updates the table as usual with that array value.
Another way is:
UPDATE app_table
SET asset_list = (SELECT ARRAY_AGG(asset_name ORDER BY asset_name)
FROM asset_table
WHERE applications = app_name)
;
https://www.db-fiddle.com/f/8oVWsubXW93n142gtZYLXB/0
This will update every record in app_table, and calculates the array value on the fly for every record.
I have a table xyz, with a metadata jsonb column in postgres.
Table : xyz
column : metadata, type = jsonb
metadata = {"exceptions": {"first_exception": "first_value"} }
I want to add a new sub_attribute
desired metadata = {"exceptions": {"first_exception": "123"},{"second_exception": "234"} }
I can use the
update xyz
SET metadata = jsonb_set(metadata->'exceptions', '{second_exception}', '"234"', true).
But I want to get the value 234 from a select query. I am not able to figure how to combine the select query with the update to do this.
You can do
UPDATE xyz
SET metadata = jsonb_set(metadata, '{exceptions, second_exception}', other.value::jsonb)
FROM other
WHERE other.column = xyz.column
Pay attention that {"exceptions": {"first_exception": "123"},{"second_exception": "234"}} is not a valid json and update will give you following result {"exceptions": {"first_exception": "123", "second_exception": "234"}}
I need to update a few thousand rows in my Postgres table using the result of a array_agg and spatial lookup.
The query needs to take the geometry of the parent table, and return an array of the matching row IDs in the other table. It may return no IDs or potentially 2-3 IDs.
I've tried to use an UPDATE FROM but I can't seem to pass into the subquery the parent table geom column for the SELECT. I can't see any way of doing a JOIN between the 2 tables.
Here is what I currently have:
UPDATE lrc_wales_data.records
SET lrc_array = subquery.lrc_array
FROM (
SELECT array_agg(wales_lrcs.gid) AS lrc_array
FROM layers.wales_lrcs
WHERE st_dwithin(records.geom_poly, wales_lrcs.geom, 0)
) AS subquery
WHERE records.lrc = 'nrw';
The error I get is:
ERROR: invalid reference to FROM-clause entry for table "records"
LINE 7: WHERE st_dwithin(records.geom_poly, wales_lrcs.geom, 0)
Is this even possible?
Many thanks,
Steve
Realised there was no need to use SET FROM. I could just use a sub query directly in the SET:
UPDATE lrc_wales_data.records
SET lrc_array = (
SELECT array_agg(wales_lrcs.gid) AS lrc
FROM layers.wales_lrcs
WHERE st_dwithin(records.geom_poly, wales_lrcs.geom, 0)
)
WHERE records.lrc = 'nrw';