Postgresql, Copy data to a new table from a foreign table - sql

I'm trying to do a job that will copy data from a foreign table called "m_aduana" of the schema "nathalia" to my schema "publico" and my table "mae_aduana".
I need to do a query that copies all the values from the table "m_aduana" avoiding duplicates.
I got something like this for now but the result sends me an Insert 0 0, which means nothing is inserted.
insert into publico.mae_aduana(cod_aduana,nom_aduana,des_aduana,cod_aduana1,cod_aduana2,cod_aduana3,est_aduana)
select cod_aduana,nom_aduana,des_aduana,cod_aduana1,cod_aduana2,cod_aduana3,est_aduana
from nathalia.m_aduana
where not exists (
select * from publico.mae_aduana ma_ad, nathalia.m_aduana m_ad
where ma_ad.cod_aduana = m_ad.cod_aduana)

I think you have an error in the inner select. You don't need to use again the table nathalia.m_aduana. If should be something like:
insert into publico.mae_aduana(cod_aduana,nom_aduana,des_aduana,cod_aduana1,cod_aduana2,cod_aduana3,est_aduana)
select cod_aduana,nom_aduana,des_aduana,cod_aduana1,cod_aduana2,cod_aduana3,est_aduana
from nathalia.m_aduana
where not exists (
select * from publico.mae_aduana ma_ad
where ma_ad.cod_aduana = nathalia.m_aduana.cod_aduana)

You might want to change the where exists part like below
from nathalia.m_aduana m
where not exists (
select 1 from publico.mae_aduana
where cod_aduana = m.cod_aduana)

Related

Abbreviate a list in PostgreSQL

How can I abbreviate a list so that
WHERE id IN ('8893171511',
'8891227609',
'8884577292',
'886790275X',
.
.
.)
becomes
WHERE id IN (name of a group/list)
The list really would have to appear somewhere. From the point of view of your code being maintainable and reusable, you could represent the list in a CTE:
WITH id_list AS (
SELECT '8893171511' AS id UNION ALL
SELECT '8891227609' UNION ALL
SELECT '8884577292' UNION ALL
SELECT '886790275X'
)
SELECT *
FROM yourTable
WHERE id IN (SELECT id FROM cte);
If you have a persistent need to do this, then maybe the CTE should become a bona fide table somewhere in your database.
Edit: Using the Horse's suggestion, we can tidy up the CTE to the following:
WITH id_list (id) AS (
VALUES
('8893171511'),
('8891227609'),
('8884577292'),
('886790275X')
)
If the list is large, I would create a temporary table and store the list there.
That way you can ANALYZE the temporary table and get accurate estimates.
The temp table and CTE answers suggested will do.
Just wanted to bring another approach, that will work if you use PGAdmin for querying (not sure about workbench) and represent your data in a "stringy" way.
set setting.my_ids = '8893171511,8891227609';
select current_setting('setting.my_ids');
drop table if exists t;
create table t ( x text);
insert into t select 'some value';
insert into t select '8891227609';
select *
from t
where x = any( string_to_array(current_setting('setting.my_ids'), ',')::text[]);

Multi-Part Identifier in SQL Temp Table

I am trying to insert values from an exisiting table into a temp table. However, I just want the values from one table which equal the value of another table to be inserted into my temp table (sorry if that was confusing)
Here is my code:
select * into
#gl_chart_av
from glchart where glchart.account_code = AdaptInvalidAccts.account_code
I'd like just the data from the glchart table inserted into my temp table who's account code in the glchart table matches that of the account codes found in the AdaptInvalidAccts table. But, I get a multi-part identifier error. Does anyone know how I can fix this?
This sounds like a case where you would use an EXISTS. It will only return records from glchart that are present in AdaptInvalidAccts based on the account_code matching.
select *
into #gl_chart_av
from glchart
where EXISTS (
SELECT 1
FROM AdaptInvalidAccts
WHERE glchart.account_code = AdaptInvalidAccts.account_code)
As for the identifier error, nowhere in your code are you declaring the AdaptInvalidAccts alias on AdaptInvalidAccts.account_code
select glchart.*
into #gl_chart_av
from
glchart
inner join AdaptInvalidAccts on glchart.account_code=AdaptInvalidAccts.account_code
or
select *
into #gl_chart_av
from glchart
where account_code in (
select account_code from AdaptInvalidAccts
)

Join two datasets by two columns in PostgreSQL

I have a PostgreSQL query, where I create temp table by joining another temp table, and table from my database.
DROP TABLE IF EXISTS other_temp_table;
CREATE TEMP TABLE other_temp_table AS
SELECT *
FROM base.main_data
WHERE period_start_time::DATE >= '2017-06-20' AND period_start_time::DATE <= '2017-07-26';
------------------------------------------------------------------------------------------------------------------------
DROP TABLE IF EXISTS first_temp_table;
CREATE TEMP TABLE first_temp_table AS
SELECT *
FROM _temp_table
LEFT JOIN base."UL_parameters"
ON temp_table.base_col::INT = base."UL_parameters".base_col::INT
and temp_table.sec_col::INT= base."UL_parameters".sec_col::INT;
Now, the problem is `ERROR: column "sec_col" specified more than once.
But when I delete sec_col join condition and do just base_col, everything is ok. I think that I need to create an alias, but not sure how.
I think that the problem is that there is a sec_col column in both joined tables. Try to replace select * with select column1, column2, ....
It's a good practice to avoid select * in general, because it can cause errors when the table definition changes.

Insert follow by delete does not work in SQL Server

In SQL Server when I execute these two commands together
INSERT INTO TABLEB SELECT * FROM TABLEC WHERE TABLEC.COLUMNC = 'ABC'
DELETE TABLEC where TABLEC.columnC = 'ABC'
I only get the delete result. Insert did return a message saying x row affected but the content in the table remains empty.
Actual code
INSERT INTO STORECODE_BK SELECT * FROM STORE WHERE STOREID = '334'
DELETE STORE where STOREID = '334'
geomagas Had gotten the solution
Is due to the ON DELETE CASCADE attribute which delete the relevant PK
Thanks
Try the following.
INSERT INTO STORECODE_BK SELECT * FROM STORE WHERE STOREID = '334'
GO
DELETE FROM STORE where STOREID = '334'
GO
Select into a temporary table.
Then use that to insert into TableB and maybe delete from Table C
Then select from the temporary table.
Got to say all the hoops you are having to jump through indicates a less than great design.

SQL Insert/Update Issue

I am trying to update one table from another, im able to update fine as long as the customer record exists, but there are some entries that dont.
To solve this i've tried running the following insert
SELECT *
INTO SalBudgetCust
FROM SalBudgetCust_temp
WHERE NOT EXISTS (
SELECT Customer
FROM SalBudgetCust
WHERE Customer = SalBudgetCust_temp.Customer
)
but im prompted with
There is already an object named 'SalBudgetCust' in the database.
Im stuck at this point... could anyone offer a little guideance?
SELECT INTO implicitly creates the table you name. You should instead use INSERT INTO ... SELECT * FROM ..., so that the existing table is used.
It should be INSERT INTO instead of SELECT * INTO ... like
INSERT INTO SalBudgetCust SELECT * FROM SalBudgetCust_temp
WHERE NOT EXISTS
(
SELECT Customer FROM SalBudgetCust WHERE Customer = SalBudgetCust_temp.Customer
)
The general syntax to insert data of one table into another is :
INSERT INTO new_table
SELECT * FROM old_table
WHERE some_condition;
Where, new_table is the table where you want to insert data, old_table is table from where you are fetching data and some_condition is the expression / condition based upon which you want to fetch data from old table.
You may use other clauses like order by, group by, and even sub queries after where clause.
May refer this SQL INSERT INTO and it's subsequent pages.