How to insert new records from one table into another table - sql

I want to insert into my tbl_Cumulative any new records that appear in tbl_Daily. My tbl_Cumulative is comprehensive of all historical tbl_Daily records. tbl_Daily is refreshed every day.
My INSERT INTO statement looks like below:
INSERT INTO tbl_Cumulative
SELECT *
FROM tbl_Daily
LEFT JOIN tbl_Cumulative
ON tbl_Cumulative.ID= tbl_Daily.ID
WHERE tbl_Cumulative.ID IS NULL
I first join the tables based on ID and where there is no match with tbl_Cumulative (aka a record is new), then append it to tbl_Cumulative.
However, I end up getting the below error:
Duplicate output destination 'ID'.
I know there is duplicate fields for ID because tbl_Cumulative and tbl_Daily have the exact same columns. How could I query my SQL so that I can still match new queries and append them to tbl_Cumulative?

SELECT the fields from only one table (tbl_Daily.*) instead of all the fields returned in the SELECT * result set.
INSERT INTO tbl_Cumulative
SELECT tbl_Daily.*
FROM tbl_Daily
LEFT JOIN tbl_Cumulative
ON tbl_Cumulative.ID= tbl_Daily.ID
WHERE tbl_Cumulative.ID IS NULL

Related

Suggestion to make a massive insert

I have the tables below:
tb_profile tb_mbx tb_profile_mbx tb_profile_cd
id id id_profile id_perfil
cod_mat mbx id_mbx id_cd (matches id_mbx)
concil bp
masc
I need to create a query that when validating that the id_cd 1,2,4,5
and 6 exists in tb_profile_cd, perform an insert in the
tb_profile_mbx table with the cod_matrix parameters of the tb_profile
table.
Remembering that each concil has its ID in the tb_mbx table and a
concil has many cod_mat.
Another point is that the concil id_mbx represents the id_cd of the
tb_profile_cd table.
One more point is that as I said above, that a concil has many
cod_mat. I have around 20 thousand records for each concil.
For my need, try to consult the query below, but Oracle returned an error:
insert into tb_profile_mbx values (seq_profile_mbx.nextval,
(select id from tb_profile where concil like '%NEXXERA%')
,(select id from tb_mbx where mbx like '%NEXXERA%')
,null
,null);
ORA-01427: single line subquery returns more than one line
Would there be another way to do this query?
Thanks in advance!
You can insert all matching combinations of matches using:
insert into tb_profile_mbx (id_profile, id_mbx)
select p.id, m.id
from tb_profile p join
tb_mbx m
on p.concil like '%NEXXERA%' and m.mbx like '%NEXXERA%';
I would recommend running the select to see if it returns the values you want.
You only show two columns for tb_profile_mbx, so I've only included two columns in the insert.

Creating a result set of data using data from one table joining on another table containing two key columns

I'm trying to create a summarised result set joining two tables.
The first table (main and multi row) contains say the following columns:
trans_id,
trans_type_id
The second table (one row only) contains:
from_trans_type_id,
to_trans_type_id
I'm trying to join the two tables so that from_trans_type_id = trans_type_id and to_trans_type_id = trans_type_id and get the relevant trans_id values
I've tried self joining and derived joins to no effect.
The end result is that I'm looking to get a result set that looks something like this:
trans_id as from_trans_id, from_trans_type_id, trans_id as to_trans_id, to_trans_type_id
data is:
you can use join with multiple instance of firsttable
select b.trans_id as from_trans_id, from_trans_type_id, c.trans_id as to_trans_id, to_trans_type_id
from secondtable a
inner join firsttable b on a.from_trans_type_id=b.trans_type_id
inner join firsttable c on a.to_trans_type_id=c.trans_type_id

update one table by inserting values from another table SQL server

I have two tables bottle and case that are filled out. They both have a column labeled case_id however in bottle table all the values are 0 while in case table they have the correct id.
How can I update the first table bottle values 0 with the new values from other table case? I believe I will need to use an UPDATE or INSERT or INNER JOIN.
If you give more detail for table struct will be better
UPDATE b SET b.case_id=c.case_id
FROM bottle AS b INNER JOIN [Case] AS c ON b.some_coloumn=c.some_cloumn

SQL INNER JOIN vs. WHERE ID IN(...) not the same results

I was surprised by the outcome of these two queries. I was expecting same from both. I have two tables that share a common field but there is not a relationship set up. The table (A) has a field EventID varchar(10) and table (B) has a field XXNumber varchar(15).
Values from table B column XXNumber are referenced in table A column EventID. Even though XXNumber can hold 15 chars, none of the 179K rows of data is longer than 10 chars.
So the requirement was:
"To avoid Duplicate table B and table A entries, if the XXNumber is contained in a table A >“Event ID” number, then it should not be counted."
To see how many common records I have I ran this query first - call it query alpha"
SELECT dbo.TableB.XXNumber FROM dbo.TableB WHERE dbo.TableB.XXNumber in
( select distinct dbo.TableA.EventId FROM dbo.TableA )
The result was 5322 rows.
The following query - call it query delta which looks like this:
SELECT DISTINCT dbo.TableB.XXNumber, dbo.TableB.EventId
FROM dbo.TableB INNER JOIN dbo.TableA ON dbo.TableB.XXNumber= dbo.TableB.EventId
haas returned 4308 rows.
Shouldn't the resulting number of rows be the same?
The WHERE ID IN () version will select all rows that match each distinct value in the list (regardless of whether you code DISTINCT indide the inner select or not - that's irrelevant). If a given value appears in the parent table more than once, you'll get multipke rows selected from the parent table for that single value found in the child table.
The INNER JOIN version will select each row from the parent table once for every successful join, so if there are 3 rows in the child table with the value, and 2 in the parent, then there will be 6 rows rows in the result for that value.
To make them "the same", add 'DISTINCT' to your main select.
To explain what you're seeing, we'd need to know more about your actual data.

SQL query that merges two tables together with a where clause

Hi I want to search through table 2 with a value (names CustID) from table 1, and if all the values are found in table 2 (CustID) that matches table 1 (CustID) the values must be shown together with all the values from table 1 that does not match table 2's values.
Is it possible to do it this way and if it is can you please show me how, I need this for a project.
Thanks in advance
It sounds like you want a "LEFT" JOIN, which will pull up all records in table1 plus the matching ones in table2.
SELECT A.,B. FROM table1 A LEFT JOIN table2 B ON A.CustID = B.CustID