match the names - ms-access-2007

I have two tables. CustomersTable and BusinessDirectory
Both tables have a column called businessName.
my CustomerTable has a columns cid, customerID, businessName
my BusinessDirectory table has columns bdid, businessName, getID
I want to update the getID field in BusinessDirectory table by customerID in CustomerTable where the business name matches in CustomerTable. Hence I did this query
update BusinessDirectory INNER JOIN CustomerTable ON CustomerTable.businessName = BusinessDirectory.businessName set BusinessDirectory.getID = CustomerTable.customerID;
which updates the records fine as long as the records match 100%. There are some records where there is a little typo and stuff
like I have a business name General Contractors Inc in one table and the other table has it as General Contractors. As you can see its missing Inc, so it doesnt match. What can I do to get best possible matches.
Thanks

This should work, but it is not particularly safe:
UPDATE BusinessDirectory, CustomerTable
SET BusinessDirectory.getID = CustomerTable.customerID
WHERE BusinessDirectory.businessName Like
Left(CustomerTable.businessName,InstrRev(CustomerTable.businessName," ")) & "*"

Related

Overwrite values from one table to another using

I have two tables, customer and newCustomer. For specific columns, I want to overwrite the column values of customer table with the newCustomer table. For example :
customer.firstname = newCustomer.firstname
customer.lastname = newCustomer.lastname
** I do have matching Ids for the two tables.
I can think of how to do this in terms of coding, but am having a hard time when thinking of doing this in SQL.
I am using Microsoft SQL Server.
Would appreciate any examples or hints.
Presumably, you have some id that connects the two tables. If so, just use join and set:
update c
set firstname = nc.firstname,
lastname = nc.lastname
from customer c join
newcustomer nc
on c.customerid = nc.customerid;

Moving data between tables on a specific condition

I have two tables. In table Order, I have a column named customer_name. I am going to create a column named customer_id and customer_name is going to be dropped.
Before I drop that column, we have to look at the table Customer. Customer has one column name that matches the customer_name from Order. I want to find the SQL syntax that will let me move the id from Customer into the customer_id in Order, on the condition that Order.customer_name = Customer.name.
I am trying to use postgresql and have to learn how to use that, but even using regular SQL I have not found the solution yet.
I have tried:
ALTER TABLE Order
SELECT INTO customer_id
id from Customer
WHERE Order.customer_name = Customer.name;
and
INSERT INTO Order (customer_id)
SELECT id
FROM Customer
WHERE Order.customer_name = Customer.name;
I am getting a syntax error.
Is there another way I should write the condition? Or is it because of pgadmin and I need to write some other way?
You seem to want something like this:
ALTER TABLE Orders ADD customer_id INT;
UPDATE Orders
SET customer_id = c.id
FROM Customers c
WHERE o.customer_name = c.name;
Note: After doing this, you should check that all orders have a valid id. Then you may need to clean up records afterwards where the names do not match.

Join 2 tables based on column name present in a table row

I have a table called Target where I have 5 columns:
ProductLevel
ProductName
CustomerLevel
CustomerName
Target
In another table called Products I have 3 columns:
ProductId
ProductCategory
ProductBrand
In the 3rd table called Customer I have 3 columns:
CustomerID
CustomerName
SubCustomerName
Is there a way to do a dynamic join where I select the column I will use in the JOIN based on the value that I have in the 1st table?
Example: If in the first table I have Category in ProductLevel, I'll join the table product using ProductCategory field. If I have Brand, I'll join using ProductBrand... The same happens with Customer table.
PS: I'm looking for a way to create it dynamically in a way I can add new columns to that tables without changing my code, then in the future I can have ProductSegment column in Product Table and Segment as a value in ProductLevel in Target table.
Yes, like this:
SELECT * FROM
Target t
INNER JOIN
Product p
ON
(t.ProductLevel = 'Category' AND t.??? = p.ProductCategory)
OR
(t.ProductLevel = 'Brand' AND t.??? = p.ProductBrand)
You didn't say which column in Target holds your product category/brand hence the ??? in the query above. Replace ??? with something sensible
PS; you can't do as you ask in your PS, and even this structure above is an indicator that your data model is broken. You can at least put the code in for it now even if there are no rows with t.ProductLevel = 'Segment'
Don't expect this to perform well or scale.. You might be able to improve performance by doing it as a set of UNION queries rather than OR, but you may run into issues where indexes aren't used, crippling performance

PostgreSQL - copy column from related table

So I have three tables: companies, addresses and company_address.
For optimization reasons I need to copy city column from addresses table to companies table. Relation between companies and addresses is many to one (as many companies can occupy same address). They are connected through company_address table, consisting of address_id and company_id columns.
I found this solution for case without intermediate table: How to copy one column of a table into another table's column in PostgreSQL comparing same ID
Trying to modify query I came up with:
UPDATE company SET company.city=foo.city
FROM (
SELECT company_address.company_id, company_address.address_id, address.city
FROM address LEFT JOIN company_address
ON address.id=company_address.address_id
) foo
WHERE company.id=foo.company_id;
but it gives error:
ERROR: column "company" of relation "company" does not exist
I cant figure out what is going on. I'll be grateful for any ideas.
You don't need a subquery for that. Also, refer in the SET clause to your table columns without preceding with table name.
I believe that since your WHERE condition includes joined table, it should be INNER JOIN instead of a LEFT JOIN.
UPDATE company c
SET city = a.city
FROM address a
INNER JOIN company_address ca ON a.id = ca.address_id
WHERE c.id = ca.company_id
Note how using aliases for table names shortens the code and makes it readable at the very first glance.
You're right syntactically, you just don't need the table name at the beginning of the update statement:
UPDATE company SET city=foo.city
FROM (
SELECT company_address.company_id, company_address.address_id, address.city
FROM address LEFT JOIN company_address
ON address.id=company_address.address_id
) foo
WHERE company.id=foo.company_id;

Postgres Update multiple rows

I need to update multiple rows in a table.
i have table customer and contact
I need to update customer where the linked contact in the contact table has the column city at a certain value.
I can get the rows i need with this query
Select cus.id, con.city
from customer cus, contact con
where cus.contacts_id=con.id
and con.city="MyValue"
I know how to update one table but do not understand how to update the table when the rows are looked up from a different table.
Firstly, please do not use the old JOINs (FROM comma separated tables).
Secondly, here you go:
UPDATE customer SET whatever = 'whatever value'
WHERE contacts_id IN (
SELECT id FROM contact WHERE city="MyValue"
)