How can I merge 2 access tables, keeping all data from table a and updated data from table b - sql

Table A has a population of names and unique ids. Table B has the same unique ids and names. The majority of the names in table B are null, but some have an updated name. I want to merge the two tables so I get the old names from table A and new names from table B if they exist. Basically layer table B on top of table A to capture changes to the names.
I've done something like this in sas, but am having a problem in Access. merging via sas is no longer an option. can this be done in access?

You can do this in SQL using theIIFandISNULLfunctions to select the name from the correct table (from TableA if TableB is null, otherwise from TableB). If your tables has two fields:(id,the_name)a query could look like this:
SELECT a.id, IIF(ISNULL(b.the_name), a.the_name, b.the_name) AS the_name
INTO TableC
FROM TableA a
INNER JOIN TableB b ON a.id = b.id

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;

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

SQL update tableA.column where the column and value is in a separate table

I have tableA with the columns ID, ColumnHeader, Value.
I'm trying to update tableB where ID, and the value in tableB.ColumnHeader with tableB.Value.
Essentially, the column headers for tableB are in a column in Table A and not column headers themselves.
I'm stuck on specifying the table name. For example, how do I run this query when I only have tablename.____ where the blank is in a column in a separate table?
update tableB set table.____ .....
As seen in the screenshot below, in Table B, 4 should become 1, and 8 should become 2, and 12 should become 3. Thanks so much.
this is a example from Northwind database:
UPDATE dbo.Products
SET dbo.Products.CategoryID = c.CategoryID
FROM Products
INNER JOIN dbo.Categories c ON dbo.Products.CategoryID = c.CategoryID
I am not getting which table and column you want to update with which table and column.
also relationship is not clear.
This doesn't make sense to me. Are you trying to update table B or A? Are there like values in table B and A that you want updated?
The basic code is as shown below:
update tableb
set column id = 'value you're updating'
where column id = 'value you're looking for' --always make this unique like a key etc.

For MS Access SQL, want to use EXCEPT in Access for three columns in table1 to 1 column in table 2

I have 3 columns in table A. I am trying to design a query that will call out all the values (in the three columns) that do not apepar in the 1 column I have in table B. If it helps to make it more clear, table B is a list of currencies in ISO codes and table A is three columns of currencies being used, I am identifying all those values that are NOT using ISO codes to denote their currency.
Currently, I can't seem to get them all to match to the one column, so I made 2 more columns in table B so I can match them individually. My constraints are, I cannot change table A and I must do this in one query. What I got so far is below.
SELECT m.Currency1, i.ISO_Code, m.Currency2 , i.ISO_Code1, m.Currency3, i.ISO_Code2
FROM A AS m
LEFT JOIN B AS i
ON m.Currency=i.ISO_Code
AND m.Currency2=i.ISO_Code1
AND m.Currency3=i.ISO_Code2
WHERE i.ISO_Code is NULL
OR i.ISO_Code1 is NULL
OR i.ISO_Code2 is NULL;
I wouldn't bother making multiple columns in 'B'. I played with this in SQLFiddle and got it to work.
Something like this:
SELECT
m.Currency1, i.ISO_Code,
m.Currency2, j.ISO_Code AS ISO_Code1,
m.Currency3, k.ISO_Code AS ISO_Code2
FROM A AS m
LEFT JOIN B as i
ON m.Currency1 = i.ISO_Code
LEFT JOIN B as j
ON m.Currency2 = j.ISO_Code
LEFT JOIN B as k
ON m.Currency3 = k.ISO_Code
WHERE
i.ISO_Code IS NULL OR
j.ISO_Code IS NULL OR
k.ISO_Code IS NULL

Trying to delete when not exists is not working. Multiple columns in primary key

I am currently trying to delete from Table A where a corresponding record is not being used in Table B. Table A has Section, SubSection, Code, Text as fields, where the first three are the Primary Key. Table B has ID, Section, SubSection, Code as fields, where all four are the Primary Key. There are more columns, but they are irrelevant to this question...just wanted to point that out before I get questioned on why all columns are part of the Primary Key for Table B. Pretty much Table A is a repository of all possible data that can be assigned to a entity, Table B is where they are assigned. I want to delete all records from table A that are not in use in Table B. I have tried the following with no success:
DELETE FROM Table A
WHERE NOT EXISTS (SELECT * from Table B
WHERE A.section = B.section
AND A.subsection = B.subsection
AND A.code = b.code)
If I do a Select instead of a delete, I get the subset I am looking for, but when I do a delete, I get an error saying that there is a syntax error at Table A. I would use a NOT IN statement, but with multiple columns being part of the Primary Key, I just don't see how that would work. Any help would be greatly appreciated.
In sql server,when using not exists, you need to set an alias for the table to be connected, and in the delete statement, to specify the table to delete rows from.
DELETE a FROM Table_A a
WHERE NOT EXISTS (SELECT * from Table_B b
WHERE a.section = b.section
AND a.subsection = b.subsection
AND a.code = b.code)
Please try :
DELETE FROM Table A
WHERE NOT EXISTS (SELECT 1 from Table B
WHERE A.section = B.section
AND A.subsection = B.subsection
AND A.code = b.code)
1 is just a placeholder, any constant/single non-null column will work.
Try something like this:
delete from Table_A
where (section, subsection, code) not in (select section,
subsection,
code
from Table_B)