update several columns of a table at once - postgresql

In postgresql, I want to update several rows of a table according to their id. This doesn't work:
UPDATE table SET othertable_id = 5 WHERE id = 2, 45, 22, 75
What is the correct syntax for this?

Use an IN operator:
update the_table
set othertable_id = 5
where id in (2,45,22,75);

Related

copy and insert rows in postgresql

hey guys I've some data in event_name with a community_code = 1 .
now I need to add rows to the same data to the table with community_code = 2. instead of adding each row, is it possible to add all the rows with different community_code. please help me out thank you.
You can insert by querying the table and replacing values as needed. For example:
insert into community_comms (id, community_id, event_name)
select id, 2, event_name
from community_comms
where community_id = 1

Does spark supports the below cascaded query?

I have one requirement to run some queries against some tables in the postgresql database to populate a dataframe. Tables are as following.
table 1 has the below data.
QueryID, WhereClauseID, Enabled
1 1 true
2 2 true
3 3 true
...
table 2 has the below data.
WhereClauseID, WhereClauseString
1 a>b
2 a>c
3 a>b && a<c
...
table 3 has the below data.
a, b, c, value
30, 20, 30, 100
20, 10, 40, 200
...
I want to query in the following way. For table 1, I want to pick up the rows when Enabled is true. Based on the WhereClauseID in each row, I want to pick up the rows in table 2. Based on the WhereClause condition picked up from table 2, I want to run the query using Where Clause to query table 3 to get the Value. Finally, I want to get all records in table 3 meeting the WhereClauses enabled in table 1.
I know I can go through table 1 row by row, and use the parameterized string to build sql query to query table 3. But I think the efficiency is very low to query row by row, especially if table 1 is big. Are there some better way to organize the query to improve the efficiency? Thanks a lot!
Depending on you usecase, but for pyspark databases, you'd might be able to solve it using the .when statement in pyspark.
Here is a suggestion.
import pyspark.sql.functions as F
tbl1 = spark.table("table1")
tbl3 = spark.table("table3")
tbl3 = (
tbl3
.withColumn("WhereClauseID",
## You can do some fancy parsing of your tbl2
## here if you want this to be evaluated programatically from your table2.
(
F.when( F.col("a") > F.col("b"), 1)
.when( F.col("a") > F.col("b"), 2)
.otherwise(-1)
)
)
)
tbl1_with_tbl_3 = tbl1.join(tbl3, "WhereClauseID", "left")

Why am I getting an ambiguous column in my PG on conflict insert?

Here is my query:
insert into zoning_algorithm_value (algorithm_value_id, value, zoning_id)
values (
61,
21,
7321
)
on conflict(algorithm_value_id)
DO
update set value = 21 where zoning_id = 7321 and algorithm_value_id = 61;
I am only referencing one table. Here is the error I am getting.
[42702] ERROR: column reference "zoning_id" is ambiguous
How can it be ambiguous when there is only one table and one column with that name? How do I make this upsert work?
You either need to specify the table name or EXCLUDED to precede the fields in the WHERE clause.
For example, if you only want to update value when the "new" zoning_id and algorithm_value_id are 7321 and 61, respectively:
insert into zoning_algorithm_value (algorithm_value_id, value, zoning_id)
values (61, 21, 7321)
on conflict(algorithm_value_id)
DO
update set value = 21 where EXCLUDED.zoning_id = 7321 and EXCLUDED.algorithm_value_id = 61;
If you instead want the WHERE to reference the "existing" record values, use the table name.

postgresql postgis : defining a new subzone index consistent with the old one

Here is my problem: I had a polygon layer with an index which looks like this:
id, population
100, 26
200, 12
300, 45
...
I edited the polygon layer and divided some of the polygons into smaller polygons (approximately 3-7 subpolygons). I already took care of having my data splitted between subzones (according to population density). So now I have this:
id, population
100, 22
100, 1
100, 3
200, 6
200, 6
I would like to create a new index that reflects the old one. For instance:
oldId, newId, population
100, 100, 22
100, 101, 1
100, 102, 3
200, 200, 6
200, 201, 6
Things I tried:
Defining a sequence:
DROP SEQUENCE IF EXISTS increment_id;
CREATE TEMP SEQUENCE increment_id INCREMENT BY 1 MINVALUE 0;
SELECT
id,
id+nextval('increment_id') AS new_id
FROM polygon_mapping WHERE id = 100;
THis works well for a single id to rename (the WHERE clause), but I don't know how to restart the sequence for every id.
I made some thinking around using the 'lag' function to compare current id with previous id. But I don't manage make it work.
Any suggestions?
Thank you
ps: I went through
Reset auto increment counter in postgres
where they reset the SEQUENCE but I don't manage to make it work in a SELECT clause.
Maybe using generate_series()?
SELECT id, generate_series(id,id + count(*)) AS newid
FROM polygon_mapping GROUP BY id;
If you want to select additional attributes, use a subquery and group the attributes using array_agg, than select the values from the array in the primary query:
SELECT id,
generate - 1 + id AS newid,
population_array[generate]
FROM (
SELECT id,
generate_series(1,count(*)) AS generate,
array_agg(population) AS population_array
FROM polygon_mapping GROUP BY id
) AS foo ORDER BY newid,id;

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;