MS Access Populating Data in Column with Data from another table (SQL) - sql

I'm needing help with a (probably very basic) command. On a test spreadsheet I have been asked to populate a column with data from another table in MS Access. However, to add the data from table 1 to table 2 it relies on another set of data to be matched with it.
In table 1 there is 2 Tables "Customer Number" and "CustomerID" - Both of which are full data sets 40,000 entries so I can't copy paste.
In table 2 the "CustomerID" is there but there are no entries.There are all entries for "Customer Number".
I need to add the customerid from table 1 to table 2 but only when the customer number is matching on both tables.
I have tried using Join function and insert but I only have been using access for about 3 days and need some help. Sorry for the longwinded explanation and hope the question is legible.
Thanks.

Does this work with JOIN?
UPDATE table2 t2 JOIN
table1 t1
ON t2.CustomerNumber = t1.CustomerNumber
SET t2.CustomerID = t1.CustomerID;

Related

Vba sql table join on matching field where 2nd field not matching

Good day everyone. I'm sure this is a fairly common issue but I trawled the internet and I cannot find an answer that works in my case. I have 2 tables which both have 2 fields and both field names are the same. I need to select all records in table 1 where the 1st field matches table 2 1st field and the 2nd field in table 2 does not match field 2 in table 1. Sorry if I'm not explaining this very well. Table 1 has around 10K records with client-name and country. table 2 is a unique list of client names (around 1K) with client-name and country fields. so I need to select all the records from table 1 with same client-name but where country in table 2 is not the same as in table 1. I've been on this for several hours now so any help would be most welcome.
The data is in 1 Access Database and I'm using ADO connection. What I have is:-
SELECT [client-name], [country] FROM [table1] LEFT JOIN [table2] ON [table1].[client-name]
= [table2].[client-name] WHERE NOT [table1].[country] = [table2].[country];

Merging 2 SQL tables with same table convention design without using update command

I have 2 tables in my SQL database:
And I want to merge them in a way the result will be:
This is just an example for 2 tables which need to be merged into one new table (The tables contain an example data, the statement should work for any amount of data inside the tables).
The ID which got different value in CSV should be updated into the new table for example:
ID 3's value is 'KKK' and in table T is 'CCC', then what should be updated is the CSV table.
You seem to want a left join and to match to the second table if available:
select t.id, coalesce(csv.value, t.value) as value
from t left join
csv
on t.id = csv.id;
If you want this in a new table, use the appropriate construct for your database, or use insert to insert into an existing table.

SQL data migration

I'm just going to use an illustration to explain my problem. SQL Migration:
In the attached image are 2 SQL tables, Table 2 is referencing a primary key of table 1. Table 1 however had many duplicates so I deleted all the duplicates using Excel and imported the data into a new table with a new set of IDs. I now have to import table 2 into a new table and reference table 1 again like before. Now in the image it looks fairly easy to do that but I am dealing with a database of over 2000 rows after eliminating around 700 duplicates. Besides manually editing each row and matching them is there any way of doing this quickly. This is the first time I doing database migration but guessing there a quick ways of doing this. Google searches did not really bear any results. I appreciate any help on this.
To preview the data you need to delete from 2nd table please run this:
select * from names
where place_id NOT in (select id from places)
If you are happy with results you can run
delete from names
where place_id NOT in (select id from places)
after copying table2 into table2New
update table2New
set PlaceNameID = (select t1N.ID
from table2 as t2
join table1 as t1
on t1.ID = t2.PlaceNameID
join table1new as t1N
on t1.PlaceName = t1N.PlaceName
where table2New.ID = t2.ID
);

SQL query what to include in from statement

Say I have four tables.
Table 1:
PK_Column_a
Table 2
PK_Column_c
FK_Column_a
Table 3
FK_Column_c
FK_Column_e
PK_c,e
Table 4
PK_Column_e
If I now want write a SQL query that will select
table1.Column_a, table2.column_c, table4.Column_e
And I wish to connect them where their foreign keys are pointing (e.g. Where table1.Column_a = table2.Column_a).
Do I need to include table 3 in my "FROM" statement? or can I connect table 2 and table 4 without joining them through table 3?
I believe the answer is yes, you would need to join to Table-3, because otherwise you won't be able to bring in data from Table-4. (There's no other way to describe the relationship for the data in Table-4 to the data in Table-1 or Table-2.)
You have to join through the Table-3, otherwise you will generate a cross join and the data won't be valid. Simply every row of table 1&2 will merge with every row from Table 4 ...

How do I only update rows in table A with the data which has changed in table B

I'm inserting data into table A. After a few days have past the data in the Table A might be out of date from the live system.
What I want to do is load the rows that would have changed from the live system into say a temp table and update only the rows which have to corresponding GUID to update with the correct data.
I need help with how the update statement should be formatted.
This would update only the column name Table A with column name of TableB with UID matches on both table: Give a try
update set a.name=b.name from TestA a INNER JOIN TestB b on a.UID=b.UID;