Assuming pro1, criterium1 should be AA1, this produces the results you are looking for:
SELECT pos.id, pro.id, pos.criterium1, pos.criterium2
, pro.criterium1, pro.criterium2
FROM Positions pos
JOIN Products pro ON pos.criterium1 = pro.criterium1
OR (pos.criterium2 = pro.criterium2
AND pos.id NOT IN (
SELECT posx.id FROM Positions posx
JOIN Products prox ON posx.criterium1 = prox.criterium1
))
ORDER BY 1;
Test query (only tested in Oracle):
WITH Products AS (
SELECT 1 id, 'AA1' criterium1, '910' criterium2 FROM dual UNION ALL
SELECT 2 id, '106' criterium1, 'BB2' criterium2 FROM dual UNION ALL
SELECT 3 id, 'AB1' criterium1, 'XXY' criterium2 FROM dual
),
Positions AS (
SELECT 1 id, NULL criterium1, '910' criterium2 FROM dual UNION ALL
SELECT 2 id, '106' criterium1, 'CCC' criterium2 FROM dual UNION ALL
SELECT 3 id, 'XXX' criterium1, 'BB2' criterium2 FROM dual UNION ALL
SELECT 4 id, 'AA1' criterium1, 'XXY' criterium2 FROM dual UNION ALL
SELECT 5 id, NULL criterium1, '123' criterium2 FROM dual
)
SELECT pos.id, pro.id, pos.criterium1, pos.criterium2
, pro.criterium1, pro.criterium2
FROM Positions pos
JOIN Products pro ON pos.criterium1 = pro.criterium1
OR (pos.criterium2 = pro.criterium2
AND pos.id NOT IN (
SELECT posx.id FROM Positions posx
JOIN Products prox ON posx.criterium1 = prox.criterium1
))
ORDER BY 1;
You need a table to sit between Employees and Products. It will contain the emp_Id
and pro_id
, though you may discover other columns later. As an employee sells a product insert the corresponding combination of IDs into this table. Remove emp_products
and pro_employees
from your model.
Best Answer
This directly answers your question about "collating" records and "merging" fields. It is not exclusive to joanolo's answer, rather you could combine both techniques to provide a richer set of data. You mentioned a "third table", but you really only need queries to combine your existing tables. There are indeed times when you want additional tables if you need to store additional information or create necessary links as joanolo's answer details. Otherwise, extra tables for combining data can just lead to duplicate information which requires double the work to maintain and can develop discrepancies if not maintained properly (in other words, avoid copying data between tables).
A query, on the other hand, always draws form the current data and so is always up-to-date. The following set of example queries
Null As [Field Name]
AS
keyword.Saved query [Timeline Events]:
Saved query [Timeline People Births]:
Save query [Timeline People Deaths]:
Altogether now:
The reference to the source table requires not only the ID, but also something that indicates the source table. To present a related record, for example to show the Event details when the user selects a timeline event, the usual technique (based on proper, normalized table design) is to create a bound subform which can automatically show related records from another table or query. But this technique also works only when a foreign key (e.g. ID values) refers to a single table. There is no built-in behavior to switch between multiple related tables as in this case with the combined data. Eventually it will require more advanced Access programming techniques. Here is an outline of how I would proceed, but if you have difficultly, you may need to explore possibilities beyond this question and answer, perhaps posting new questions on Stack Overflow since this goes beyond Database Admin and design.
IDMixed
and the Link Child Fields toID
.In the main form's OnCurrent event, add the following VBA code. Note that this is only a template. At the least, exact names of controls will need to be updated.
There are other more advanced techniques, perhaps more elegant or efficient, but they require more advanced programming techniques and understanding of Access intricacies.
Besides the general structure which these queries outline, the specific fields which you include is completely of your design and choice. I assume you will want to include more types of timeline dates, but all you do is add another query and combine it by appending another UNION.
Notes about Union: It is not required to save the other queries separately--the SQL from the original queries could be included directly with the UNION statements, but saving them separately makes it easier to view, edit and verify each separate query individually. It is technically not required that the columns of each query have the same names, only that they have the same number of columns and are of compatible data types. The resultant query will take columns names from the first query in the union.