add a primary key to an existing union query in access 2010 - sql

I have linked two tables from excel two different excel files that input data into my union query in ms access 2010, my question is can I add a primary key to the already existing union query?
Both tables from excel have very similar information and in access their field names are identical. I need the first column called MRN to become the primary key, so I won't have duplicate MRN's from the same station(FYI the two excel files with the linked tables are stations in which products are scanned and their data is then transmitted into access through the linked tables) this is what I have in sql already:
SELECT [OvenImportTable].[F1] As MRN,[OvenImportTable].[F2] As Description,[OvenImportTable].[F3] As [TWI Part #],
[OvenImportTable].[F4] As [PO #], [OvenImportTable].[F5] As REC,[OvenImportTable].[F6] As EXP, [OvenImportTable].[F7] As Station
FROM [OvenImportTable]
UNION SELECT
[DiePunchImportTable].[F1], [DiePunchImportTable].[F2], [DiePunchImportTable].[F3],
[DiePunchImportTable].[F4],[DiePunchImportTable].[F5],
[DiePunchImportTable].[F6], [DiePunchImportTable].[F7]
FROM [DiePunchImportTable,
alter table MRN add primary key;
This is what one record in the first table looks like:
MRN Description TWI Part # PO# REC EXP Station
1234-1 312HTG 400-134004 123 08/15/14 08/15/15 Oven
this is what one record in the second table looks like:
MRN Description TWI Part # PO# REC EXP Station
1234-1 312HTG 400-134004 123 08/15/14 08/15/15 Punch
The result I would like to see is making the MRN a unique number, so it is not duplicated by human error. I need to add a primary key to both tables so the MRN isn't accidentally duplicated in those tables. I am wondering if it is possible to add this operation to SQL? Is it even possible in a union query?
Any help or suggestions would be appreciated.

Related

Excluding data pairs from a query based on a table?

I have a massive and messy database of facilities where there are many duplicates. Addresses have been entered in such a haphazard way that I will be making many queries to identify possible duplicates. My objective is for each query to identify the possible duplicates, and then a person actually goes through the list and marks each pairing as either "not a duplicate" or "possible duplicate."
When someone marks a facility pair as not a duplicate, I want to record that data pair in a table so when that when one of the queries would otherwise return that pairing, it is instead excluded. I am at a loss for how to do this. I'm currently using MS Access for SQL queries, and have rudimentary visual basic knowledge.
Sample of how it should work
Query 1 is run to find duplicates based on city and company name. It brings back that facilities 1 and 2, 3 and 4, 5 and 6 are possible duplicates. The first two pairings are duplicates I need to go fix, but that 5 and 6 are indeed separate facilities. I click to record that facilities 5 and 6 are not duplicates, which records the data in a table. When query 1 is run again it does not return that 5 and 6 are possible duplicates.
For reference, the address duplicates look something like this, which is why there need to be multiple queries
Frank's Garage, 123 2nd St
Frank's Garage LLC, LLC, 123 Second st
Frank's Garage and muffler, 123 2nd Street
Frank's, 12 2nd st
The only way I know to fix this is to create a master table of company names and associate this table PK with records in original table. It will be a difficult and tedious process to review records and eliminate duplicates from master and associate remaining PK of a duplicate group to the original records (as you have discovered).
Create a master table of DISTINCT company and address data from original table. Include autonumber field to generate key. Join tables on company/address fields and UPDATE a field in original table with this key. Have another field in original table to receive a replacement foreign key.
Have a number field (ReplacementPK) in master table. Sort and review records and enter the key you want to retain for company/address duplicates group. Build a query joining tables on original key fields, update NewFK field in original table with selected ReplacementPK from master.
When all looks good:
Delete company and address and original FK fields from original table.
Delete records from master where PK does not match ReplacementPK.

Assign unique ID to duplicates in Access

I had a very big excel spreadsheet that I moved into Access to try to deal with it easier. I'm very much a novice. I'm trying to use SQL via Access.
I need to assign a unique identifier to duplicates. I've seen people use DENSE_RANK in SQL but I can't get it to work in Access.
Here's what I'm trying to do: I have a large amount of patient and sample data (20k rows). My columns are called FULL_NAME, SAMPLE_NUM, and DATE_REC. Some patients have come in more than once and have multiple samples. I want to give each patient a unique ID that I want to call PATIENT_ID.
I can't figure out how to do this, aside from typing it out on each row. I would greatly appreciate help as I really don't know what I'm doing and there is no one at my work who can help.
To illustrate the previous answers' textual explanation, consider the following SQL action queries which can be run in an Access query window one by one or as VBA string queries with DAO's CurrentDb.Execute or DoCmd.RunSQL. The ALTER statements can be done in MSAcecss.exe.
Create a Patients table (make-table query)
SELECT DISTINCT s.FULL_NAME INTO myPatientsTable
FROM mySamplesTable s
WHERE s.FULL_NAME IS NOT NULL;
Add an autonumber field to new Patients table as a Primary Key
ALTER TABLE myPatientsTable ADD COLUMN PATIENT_ID AUTOINCREMENT NOT NULL PRIMARY KEY;
Add a blank Patient_ID column to Samples table
ALTER TABLE mySamplesTable ADD COLUMN PATIENT_ID INTEGER;
Update Patient_ID Column in Samples table using FULL_NAME field
UPDATE mySamplesTable s
INNER JOIN myPatientsTable p
ON s.[FULL_NAME] = p.[FULL_NAME]
SET s.PATIENT_ID = p.PATIENT_ID;
Maintain third-norm principles of relational databases and remove FULL_NAME field from Samples table
ALTER TABLE mySamplesTable DROP COLUMN FULL_NAME;
Then in a separate query, add a foreign key constraint on PATIENT_ID
ALTER TABLE mySamplesTable
ADD CONSTRAINT PatientRelationship
FOREIGN KEY (PATIENT_ID)
REFERENCES myPatientsTable (PATIENT_ID);
Sounds like FULL_NAME is currently the unique identifier. However, names make very poor unique identifiers and name parts should be in separate fields. Are you sure you don't have multiple patients with same name, e.g. John Smith?
You need a PatientInfo table and then the SampleData table. Do a query that pulls DISTINCT patient info (apparently this is only one field - FULL_NAME) and create a table that generates unique ID with autonumber field. Then build a query that joins tables on the two FULL_Name fields and updates a new field in SampleData called PatientID. Delete the FULL_Name field from SampleData.
The command to number rows in your table is [1]
ALTER TABLE MyTable ADD COLUMN ID AUTOINCREMENT;
Anyway as June7 pointed out it might not be a good idea to combine records just based on patient name as there might be duplicates. Better way will be treat each record as unique patient for now and have a way to fix patient ID when patient comes back. I would suggest to go this way:
create two new columns in your samples table
ID with autoincrement as per query above
patientID where you will copy values from ID column - for now they will be same. But in future they will diverge
copy columns patientID and patientName into separate table patients
now you can delete patientName column from samples table
add column imported to patients table to indicate, that there might be some other records that belong to this patient.
when patients come back you open his record, update all other info like address, phone, ... and look for all possible samples record that belong to him. If so, then fix patient id in those records.
Now you can switch imported indicator because this patient data are up to date.
After fixing patientID for samples records. You will end up with patients with no record in samples table. So you can go and delete them.
Unless you already have a natural key you will be corrupting this data when you run the distinct query and build a key from it. From your posting I would guess a natural key would be SAMPLE_NUM. Another problem is that if you roll up by last name you will almost certainly be combining different patients into one.

MS Access - create JOIN table to store value for every combination

I am working on an MS Access 2013 database. I have two tables:
Customers (28 records)
Chemicals (34 records)
I need to create a table for usage rates for each customer for each chemical.
The rates will be entered manually (at user's request). I am trying to determine how to create a new table where the customer-chemical fields will combine to be primary key.
The resulting table should have 28x34=952 unique records.
The goal is to then have a form wherein the user can select the customer, then the chemical, and edit the rate.
For any table/query creation I am comfortable using either the Access interface or SQL.
I will advise to create a new table containing 4 columns. The first column will be an 'id' it is going to be your primary key (auto-increment if you want), second column is the customer, then the chemical, and finally the rating. Then if you format your query to select 'rating' where customer='customer name' and chemical='chemical name', you should get the desired result you want.
Thank you for the reply. Did a little more wrestling with it and used the following SQL to create the table:
SELECT customers.customer, chemicals.chemical
INTO UsageRates
FROM Chemicals, Customers
Then adding a blank 'rate' field to the table.

Updating id references into additional table

I'm having a little trouble (possibly codeblind currently) when it comes to migrating some data.
I have 2 tables, one is an appliance table, the other lists manufacturers. The original database stores all the data in a single table, which I'm splitting into multiple tables. I've managed to extract the manufacturers fine, as with the rest of the appliance details to the relevant tables. What I'm failing to do is link the id of the manufacturer to the appliance.
So what I want is for the id in the appliance table to be the corresponding id relative to the manufacturer name in the other table, but done in a single query from the original source material.
My original insert code as follows:
insert into c_appliance (app_serial, property_id, app_location,
app_installdate, app_warrantyexp, app_nextservice)
select [Serial No#], [Customer Number], location,
installed, [Expiry Date], [Service Due]
from dbo.[Customer Table]
This doesn't add the manufacturer into the appliance table - which I'm aware of. The manufacturer column currently remains as null while i attempt to figure out what I'm missing.
Any help would be greatly appreciated!
First of all create a unique key column in original table (temporarily) if not there.
Then insert in first table, as in the above query you are inserting in c_appliance. Also add the temporary unique key column.
Similarly insert into Manufacturer table in the same way, with temporary unique key column.
Now update you can set primary key and foreign keys on the basis of this unique key column.

How to use an expression in a join between two tables?

I have two tables. In the 1st table (transaction) there are 2 columns called supplier_code and local_commodity_code. In the 2nd table (local_feed_commodity_map) there are two columns called local_commodity_code and local_commodity_desc. In 1st table, the local_commodity_code field is made by concatenating the supplier_code from 1st table and local_commodity_code from the 2nd table.
I split the concatenated column by using the following code:
SELECT
SUBSTR(T.LOCAL_COMMODITY_CODE, 1, INSTR(T.LOCAL_COMMODITY_CODE,'~')-1) LOCAL_COM_CODE
FROM OYSTER_WEB3.TRANSACTION T
So, I have the column named local_com_code after splitting.
Now I want to join these two tables using the newly generated column (local_com_code) and the local_commodity_code column from the 2nd table. How can I do this only using SELECT statement because I don't have permission for create, insert or update table.
SELECT L.*, T.*
FROM (SELECT Supplier_Code,
Local_Commodity_Code,
SUBSTR(LOCAL_COMMODITY_CODE, 1, INSTR(LOCAL_COMMODITY_CODE,'~')-1)
LOCAL_COM_CODE
FROM OYSTER_WEB3.TRANSACTION
) T
JOIN Local_Feed_Commodity_Map L
ON L.Local_Commodity_Code = T.Local_Com_Code
Oracle has an aversion to the SQL standard 'AS' keyword in some locations, so I've not used it anywhere to maximize the chances of the code working.
However, as I noted in a comment to the question, this is an appalling piece of schema design and should be fixed. It is ludicrous to pessimize all queries that have to work between these two tables by requiring the use of SUBSTR and INSTR like that. The Local_Commodity_Code in the Transaction table should be identical to the Local_Commodity_Code in the Local_Feed_Commodity_Map table so that both the primary key and the foreign key columns can be properly indexed (and referential integrity enforced).