Relational Links in Access tables - ms-access

I have an Access database that contains 13 tables. The data in the tables are numbers. I would like to be able to relate specific fields in the tables and sum the totals for all of the tables together.
For instance, I have fields for patient information completed in several of the tables the number in the field is the number of patients who completed the form. Each table represents a different facility. The total would represent the number of patients from all of the facilities who completed the form so that I have a grand total.
I'm not sure about how to go about linking the tables.
Thanks,
Gale

These tables do not have relationships - they have the same kind of data, not related data (such as relating Customers to Orders). This is not correct data structure. Should not have a separate table for each facility. Should be one table with another field to ID the facility. Otherwise, UNION query these tables into a single dataset should be in first place.
SELECT ID, F1, F2, F3, "Fac01" AS Facility FROM Table1
UNION SELECT ID, F1, F2, F3, "Fac02" FROM Table2
...
UNION SELECT ID, F1, F2, F3, "Fac13" FROM Table13;
Then use that dataset in subsequent queries, such as GROUP BY or CROSSTAB.

Related

How do I query values from 1 table where there is a common value in both tables

I have the following 2 tables:
I'd like to create a query, equal to table 2 with all the "text"-values from table1 where the "number"-value is the same.
I am still new to access, and unfortunately unable to solve this.
The desired outcome is as follows:
The [number] fields are showing as different data types. These tables cannot be directly joined on those fields. Either change the data type of one of the fields or build an intermediate query that converts field and use that query in join.
Also, each table has duplicate [number] values - by simply joining, the records output will be compounded (a Cartesian relationship). For instance, Table1 has two 1315 records and Table2 has three 1315 records - this means the output will have six 1315 records. Will have to use DISTINCT keyword or GROUP BY clause.
SELECT DISTINCT T2.ID, T2.Num, Table1.text
FROM (SELECT ID, Val([number]) AS Num FROM Table2) AS T2
LEFT JOIN Table1 ON T2.Num = Table1.number;
Although, it appears that the ID fields are synchronized. If these are not both autonumber type fields and there is a relationship, joining on those fields would be possible. However, [text] field data will not display where there is no [text] data in Table1 (e.g, Table2 records 2, 4, 6, 24). This join will not produce the desired output because Table2 records 4 and 6 will not have associated [text] data.

Query/Relationship from multiple Fields to a single Field in Related Table/Query

Apologies if this is answered in SO. I searched and couldn't find an obvious duplicate. I am new to Access and inherited an old Access MDB (2000 File Format). [Note: I am using Office 365.]
Data is in 5 tables. However, only 4 are connected with Relationships. The fifth (unconnected) Table is a mashup of different data in 3 fields: Type, Code, and Description. Type has multiple rows with repeated values. Each Type row has a unique Code and Description. Each unique Type corresponds to a column in Table_1, and the Codes are values found in that column. (There are 3000 rows in Table_5 with over 250 unique Types, each with 1-500+ Codes.)
Here is a simplified version of Table_5:
Type Code Description
Atype A Atype_A_Description
Atype B Atype_B_Description
Atype X1 Atype_X1_Description
...
Class 1 Class_1_Description
Class 2 Class_2_Description
Class 9 Class_9_Description
...
Source A Source_A_Description
Source A1 Source_A1_Description
Source A2 Source_A2_Description
...
To complicate things, there isn't an exact match between the Field names in Table_1 and the Type entries in Table_5. (For example the Atype values in Table_5 correspond to a Field named ACC_TYPE1 in Table_1.)
I'm working on a method to get the Description from Table_5 based on a Field Name and Value from Table_1. I did this for 1 (hardcoded) Type. I created a Query for that Type in Table_5, and connected it to Table_1 with a Relationship.
Here's what I did:
Table_5_Atype_Query (as SQL, let me know if another format is preferred in SO)
SELECT Table_5.[Type], Table_5.[Code], Table_5.[Description]
FROM Table_5
WHERE (((Table_5.[Type])="Atype"));
The Relationship is:
Table/Query: Related Table/Query:
Table_1 Table_5_Atype_Query
ACC_TYPE1 Code
This works perfectly to get the Description field referencing Code from Table_5 based on values of ACC_TYPE1 in Table_1. It is NOT scalable to lookup Descriptions for other Type/Code pairs. (I would need 250 unique queries and relationships.) Put another way, I'd like to create a method to get the Description from Table_5 based on any Column Name and Value from Table_1. Are there better ways to do this?
A UNION query can rearrange fields to a normalized data structure.
UNION query with 5 fields:
SELECT ID, field1 as Code, "f1" as src from table1
UNION SELECT ID, field2, "f2" from table1
UNION SELECT ID, field3, "f3" from table1
UNION SELECT ID, field4, "f4" from table1
UNION SELECT ID, field5, "f5" from table1;
Of course, use your actual field and table names.
Now use that query in another query joining to table5.
There is a limit of 50 SELECT lines. There is no query designer or wizard for UNION - must use SQLView.

entries in multiple dates in access

I have a access table that has quarterly pricing data starting from 20100131 and goes on as 20100430, 20100731.... 20170131, 20170430. For each pricing date, there are many loans. Some loans stay in the portfolio, some loans are removed and some added for each pricing period. I would like to find the list of loans that exist in all periods and see their price for each period. So i have the "Loan_Number" field and "Price_Date" field. I would like to find the Loan Numbers that exist in all price date points. I appreciate the help.
Thanks!
Would have been nice to see some effort from you but I was intrigued with the challenge so here is what I accomplished.
1 - Need a dataset of all possible combinations of loan numbers and date values. So if you have a LoanNumbers table and a Periods table, create a Cartesian query called AllPairs:
SELECT LoanNumbers.Loan_Number, Periods.Price_Date FROM LoanNumbers, Periods;
If you don't have those tables, generate datasets with queries, assuming the data table has at least one record for every loan number and at least one record for every period:
SELECT DISTINCT Table1.Price_Date FROM Table1;
SELECT DISTINCT Table1.Loan_Number FROM Table1;
2 - Join AllPairs to data table for a 'find unmatched' query called LoanNoPeriod:
SELECT AllPairs.Loan_Number, AllPairs.Price_Date, Table1.Loan_Number, Table1.Price_Date
FROM AllPairs LEFT JOIN Table1 ON (AllPairs.Price_Date = Table1.Price_Date) AND (AllPairs.Loan_Number = Table1.Loan_Number)
WHERE (((Table1.Price_Date) Is Null));
3 - Final query:
SELECT * FROM Table1 WHERE Loan_Number NOT IN (SELECT AllPairs.Loan_Number FROM LoanNoPeriod);
Be aware these type of queries can perform very slowly and with very large datasets might not be practical.

In MySQL, how to return one value from multiple tables matched on multiple fields

I have a table of invoice line items. Each line item can originate from one of several possible sources, e.g., client_id, entity_id, vendor_id, etc. Each one of these id's would match the primary key of another table, in which we would find the name and other info. I am trying to run a report of invoice line items. I want that report to include a value called "Source Name" for each line item that would display the corresponding name. So, if the source was the client it would show the client's name, if the source was the vendor it would show the vendor's name, etc. I would also like to be able to concatenate text to indicate whether it was the client, entity, vendor, etc.
I believe you are looking for UNION. If you have 2 tables you can use union to create one table
SELECT *
FROM (
SELECT client_id AS 'ID_FIELD', client_name AS 'NAME_FIELD', 'client' as 'TABLE_TYPE' FROM client_table
UNION
SELECT vendor_id AS 'ID_FIELD', vendor_name AS 'NAME_FIELD', 'vendor' as 'TABLE_TYPE' FROM vendor_table
) AS t1
WHERE t1.ID_FIELD = 'YOUR_VALUE_HERE'

Combining data from multiple tables based on the first table

I have a table full of items, at the moment there is only one type and everything about that item is in the same table. Now I want to add two more types. A number of columns (>10) will be unique to each type, so I don't just want to have 30 columns in the table when 20 of them will be null values, so I'm thinking of splitting them up:
tbl_items_common
tbl_items_type1 [for data unique to this type]
tbl_items_type2 [for data unique to this type]
tbl_items_type3 [for data unique to this type]
All the tables will have a common field 'id', for matching up the items and data.
Now I'm thinking about the coding and thinking:
Should I bother splitting these up at all, or should I just have one big table. Makes inserting and querying easier, but the db is a bit more 'messy'.
Splitting the data up makes the db a bit more isolated, but makes code more complicated.
Or, I could just use 3 completely separate tables with the some of the columns the same between them, but then I'm not sure how to generate a unique id that is unique over all 3 tables.
If I do decide on splitting the data up, is it possible to get all the data in one query (embedded case/select maybe?), or would I have to do a SELECT first to find the item type and then another to grab the rest of the data? Not pretty when it comes to reporting.
So what do I want to know? (1) Which method would you choose and (2) If you choose the second, how would you query that data?
You have a few options. Personally, I'd keep all the important (and similar) data for your items in one table, and have 3 columns containing detail IDs, you could then create a couple of different tables containing the extra data for each type and JOIN that data to each query.
Another option is you could have three separate tables containing items, but I don't advise that as it's not good database design practice. If you did want to do that though, you could either select from all three tables in one query:
SELECT * FROM table1, table2, table3
... or manually match the column count using null values and perform a UNION query:
SELECT f1, f2, f3, f4, null as f5 FROM table1
UNION ALL
SELECT f1, f2, f2, null as f4, null as f5 FROM table2
UNION ALL
SELECT f1, f2, f3, f4, f5 FROM table3 -- table 3 contains 5 columns as an example
... but that could get messy as well if you have lots of columns unique to each type. I'd suggest going with three separate tables for each item type's details:
SELECT * FROM items I
LEFT JOIN item_type1 I1 ON I.item1_detail_id = I1.id
LEFT JOIN item_type2 I2 ON I.item2_detail_id = I2.id
LEFT JOIN item_type3 I3 ON I.item3_detail_id = I3.id