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

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.

Related

MS Access duplication

I got my query in MS Access duplicating its errors. The data is being entered from a Windows Forms app, and the data is in two separate tables. I'm using a basic query to show each row in the table. Name table and score table.
Now the issue is that the data is duplicating itself.
Image of the access table
This is the SQL query:
SELECT DISTINCT score.score, name.Name
FROM name, score;
Table Schema
What SQL code could I use to stop the duplication error?
Thanks in advance
Need to add a long integer number field StudID in Scores table then do data entry to add the appropriate student identifier value into each Scores record. This will establish a primary/foreign key relationship between tables that can be used in query with a LEFT or RIGHT JOIN on key fields.
This assumes a student can have multiple scores (for what?). If there can be multiple scores, then probably also need a date field ScoreDate and then possibly should be a Subject field.
If student can have only 1 score, then don't need Scores table - have a field Score in Students table.
Strongly advise not to use reserved words as names for anything. Name is a reserved word.

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.

Retrive unique records from an access table which does not have a fixed structure

I want only the unique records in same or new table. And I want to do this with different tables (having duplicate records) in access database through same code.
The flow should be like:
input table ------VBA MODULE------> table with unique records
I am able to do this group by function but for that i have to use field names in query. But field names will differ from table to table.
Please help!
Just use query
SELECT DISTINCT * FROM MyAnyTable

Adding a column to existing table in access without using any relationship

I am working on a project in ACCESS 2010 that requires me to give a rank to 30000 products based on their sales. I have tried using a query to do the ranking and it takes a long time. (Please find codes at Making the ranking query efficient)
I figured out that instead, I can just sort the table based on the Sales column and add a field with numbers 1 to 30000.
So is there a way to add such a column, i.e. a column without any relationship to the existing table.
Add a field to the actual table? If that's the case, make a table and run this query:
ALTER TABLE yourTableName
ADD COLUMN yourColumnName AUTOINCREMENT(1, 1)

Updating one SQL table based on data in another table

I am running Microsoft SQL Server 2008 R2, and pulling information from two tables to create one new table.
Table A has leads with a unique lead number and other information.
Table B has sales with a unique sales number, and the lead number associated with it.
Data from both tables are pulled into temp tables in SQL Server so I can change and update whatever I need, and the output of this will go into a new table.
One lead from Table A can have multiple sales associated with it in table B.
I want to update the Number of Sales column in Table A (Leads) based on how many times that lead number appears in Table B (sales). So if Table B (sales) has a lead number tied to seven (7) sales, the Number of Sales column in Table A (leads) will be updated to 7.
I have tried a few variations using the COUNT function but with no success. Any help would be appreciated.
This should work for you assuming the field name is leadNo:
update tablea
set sales = (select count(*)
from tableb
where tableb.leadNo = tablea.leadNo)
SQL Fiddle Demo