I got Table A,B,C ,D in postgres, I need to get the last row based on the id and then…
- User
I got Table A,B,C ,D in postgres, I need to get the last row based on the id and then insert those ids on another table called GE
- Assistant
To get the last row based on the id from tables A, B, C, and D in PostgreSQL, you can use the MAX function along with a subquery. Here is an example query that retrieves the last row for each table:
SELECT MAX(id) AS last_id FROM ( SELECT id FROM A ORDER BY id DESC LIMIT 1 UNION ALL SELECT id FROM B ORDER BY id DESC LIMIT 1 UNION ALL SELECT id FROM C ORDER BY id DESC LIMIT 1 UNION ALL SELECT id FROM D ORDER BY id DESC LIMIT 1 ) AS last_rows;This query returns the last id from each table as
last_id.To insert these ids into another table called GE, you can use the INSERT INTO statement. Assuming the GE table has a column named
id, you can use the following query:INSERT INTO GE (id) SELECT MAX(id) AS last_id FROM ( SELECT id FROM A ORDER BY id DESC LIMIT 1 UNION ALL SELECT id FROM B ORDER BY id DESC LIMIT 1 UNION ALL SELECT id FROM C ORDER BY id DESC LIMIT 1 UNION ALL SELECT id FROM D ORDER BY id DESC LIMIT 1 ) AS last_rows;This query inserts the last ids into the
idcolumn of the GE table. - User
does unin all exist on postgressql?
- Assistant
Apologies for the mistake in my previous response. You are correct, the correct syntax is not
UNION ALL, butUNION. TheUNIONoperator is used to combine the results of multiple SELECT statements into a single result set.Here's the corrected query to get the last row based on the id from tables A, B, C, and D, and then insert those ids into another table called GE:
INSERT INTO GE (id) SELECT MAX(id) AS last_id FROM ( SELECT id FROM A ORDER BY id DESC LIMIT 1 UNION SELECT id FROM B ORDER BY id DESC LIMIT 1 UNION SELECT id FROM C ORDER BY id DESC LIMIT 1 UNION SELECT id FROM D ORDER BY id DESC LIMIT 1 ) AS last_rows;Thank you for catching that mistake, and I apologize for any confusion caused.
- User
I mean the last row was on A 23, on B 12, on C 1 and D 1, when inserting on GE there is a column for each row ID based on the table to be inserted
- Assistant
If you want to insert the last row from each table A, B, C, and D into the GE table, considering each table has its own column in the GE table to store the corresponding last row ID, you can use the following query:
INSERT INTO GE (column_from_A, column_from_B, column_from_C, column_from_D) SELECT (SELECT id FROM A ORDER BY id DESC LIMIT 1), (SELECT id FROM B ORDER BY id DESC LIMIT 1), (SELECT id FROM C ORDER BY id DESC LIMIT 1), (SELECT id FROM D ORDER BY id DESC LIMIT 1);In this query, replace
column_from_A,column_from_B,column_from_C, andcolumn_from_Dwith the actual column names in the GE table for each respective table A, B, C, and D.This will insert the last IDs from tables A, B, C, and D into the corresponding columns of the GE table in a single row.
- User
but there are some other values that need to be inserted that will be pass as parameters, parameter1 and parameter2, besides the others
- Assistant
If you have additional values to be inserted into the GE table alongside the last row IDs, you can modify the query to include those parameter values. Here's an example query incorporating parameter1 and parameter2:
INSERT INTO GE (column_from_A, column_from_B, column_from_C, column_from_D, parameter1, parameter2) SELECT (SELECT id FROM A ORDER BY id DESC LIMIT 1), (SELECT id FROM B ORDER BY id DESC LIMIT 1), (SELECT id FROM C ORDER BY id DESC LIMIT 1), (SELECT id FROM D ORDER BY id DESC LIMIT 1), $1, $2;In the above query, 2 represent the placeholder values for parameter1 and parameter2, respectively. When executing this query, you should pass the actual values for those parameters.
Ensure that the column names and the corresponding data types in the GE table match the values and their data types being inserted.
- User
can you format the following code in plsql
INSERT INTO "PL_SummaryBox" ( "id_Restrictions", "id_Process", "id_MoliendaIBP", "id_Quality", "id_Commercial", "runId", "date", "user" ) SELECT ( SELECT id as "id_Restrictions" FROM public."PL_Restrictions" ORDER BY date DESC LIMIT 1 ), ( SELECT id as "id_Process" FROM public."PL_Process" ORDER BY date DESC LIMIT 1 ), ( SELECT id as "id_MoliendaIBP" FROM public."PL_MoliendaIBP" WHERE "user" ilike :user ORDER BY date DESC LIMIT 1 ), ( SELECT id as "id_Quality" FROM public."PL_Quality" WHERE "user" ilike :user ORDER BY date DESC LIMIT 1 ), ( SELECT id as "id_Commercial" FROM public."PL_Commercial" WHERE "user" ilike :user ORDER BY date DESC LIMIT 1 ), :runId, :date, :user;
- Assistant
Sure! Here's the code formatted in PL/SQL:
INSERT INTO "PL_SummaryBox" ( "id_Restrictions", "id_Process", "id_MoliendaIBP", "id_Quality", "id_Commercial", "runId", "date", "user" ) SELECT (SELECT id as "id_Restrictions" FROM public."PL_Restrictions" ORDER BY date DESC LIMIT 1), (SELECT id as "id_Process" FROM public."PL_Process" ORDER BY date DESC LIMIT 1), (SELECT id as "id_MoliendaIBP" FROM public."PL_MoliendaIBP" WHERE "user" ILIKE :user ORDER BY date DESC LIMIT 1), (SELECT id as "id_Quality" FROM public."PL_Quality" WHERE "user" ILIKE :user ORDER BY date DESC LIMIT 1), (SELECT id as "id_Commercial" FROM public."PL_Commercial" WHERE "user" ILIKE :user ORDER BY date DESC LIMIT 1), :runId, :date, :user;Note that you can replace
:user,:runId, and:datewith your actual parameter values when executing this PL/SQL code.