update one table by inserting values from another table SQL server - sql

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

Related

populating null rows in table column based on matching IDs via join or otherwise

Just to level set: i'm working within a Vertica database using SQL.
Let's say i have two tables: Table A and Table B. Let's also say that Table A is my final/master table used for data vis within Tableau (or something akin), and that Table B feeds certain columns into Table A based on matches within a tertiary table, Table C (which is not relevant to this conversation).
As is, Table A has columns:
ProgramName [varchar(50)]
CustomerName [varchar(50)]
Total_Cost [numeric(18,4)]
As is, Table B has columns:
CustomerCode [varchar(10)]
Total_Cost [numeric(18,4)]
What I would like to do is update Table A's CustomerName column to equal CustomerCode in Table B where the columns of total_cost_dollars equal each other across tables.
I've run this left join query to ensure that, when I do update Table A's CustomerName to equal CustomerCode, the total cost columns are exact/true matches for my entire data set.
SELECT
A.ProgramName,
A.CustomerName,
A.total_cost_dollars,
B.CustomerCode,
B.total_cost_dollars
FROM
TableA A
LEFT JOIN
TableB B
ON
B.total_cost_dollars = A.total_cost_dollars
WHERE
A.CustomerName IS NULL;
Any idea on how to solve this problem?
Since Vertica supports merge query, you can use merge statement:
merge into TableA A
using TableB B
ON (B.total_cost_dollars = A.total_cost_dollars)
when matched then
update
set
A.CustomerName = B.CustomerCode
where
A.CustomerName IS NULL;

Update column in table with value from another

I need to update one column called Ident in table a to be a value from table b, as shown below. Both the change and keep values in table b are valid Idents which will appear in table a. I need to find the Change value in table b in table a, and then change the value in table a to the Keep value in table b.
Statement i'm using is:
update p set p.ident=t.keep
from pclscvq p inner join #tmp t on (p.ident=t.change)
where t.change=p.ident
Table B:
Change Keep
0004P 0004R
0004X 0004Y
00055 00056
00057 00058
0005B 0005C
0005K 0005L
0005Z 00060
00065 00066
0006X 0006Y
00070 00071
Very much stuck.
Using Advantage SQL

How to insert new records from one table into another table

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

Insert columnA values of Table 1 into another table if match occur

I have two tables.Table A has 4 columns. And table B has two columns.I want to insert value of one column of tabel A from one column of table B based on condtion if id matches.
how i can do this ? For example if [Movieid] in 1st table =[IMDBid] in second table then insert [count] of table 1=[CB] in table 2.
i want to do it once for full table.
[column] -> these are colums
i m using sql server.
Tabel 1 : Movieid,count,
Tabel 2: IMDBid, CB
Results which i want: i want to insert values of CB column in count where Movieid=IMDBid
Tabel 1 :
(Nick Id,MovieId,Rating,MovId)-> (1,4972,6.25,?)(1,24216,7.25,?)
Tabel 2 :
(Imdbid,Title,ImdbPyId,Id)-> (4972,hello,32450,1)(24216,hi,62450,2)
Insert /fill value of MovId(tabel1) using values Id(tabel2)where MovieId(tabel1)==Imdbid(tabel2)
You can do it like,
UPDATE tabl1
SET count = (SELECT CB FROM tabl2 WHERE IMDBid = Movieid)
But it is gonna blow up if you have multiple values returning from the subquery.
So make sure to use the appropriate function to get the single value from that subquery whichever meets your requirements.
If your tables have 1:1 relationship then it should be fine. But if it is 1:n then you need to use either aggragate functions or the TOP 1 clause in that subqyery.
solution is UPDATE tabl1
SET count = (SELECT CB FROM tabl2 WHERE IMDBid = Movieid)

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