Relations between 3 levels and a result table - sql

I have 3 tables that work, let's say, as levels for this purpose. Everyone of them has 2 columns, id and name. And, they combined, result on posibilities that matches the table 4.
How can I create the relationships between the first set (3 tables), and the last one with the possible results after the combination?
I did this in the past just with 2 tables, back then I created a third one having 2 fields, 2 FKs against the original tables. But this time, I have a set of 3 tables to match with a fourth, and that's what's making me wonder.
Should I simply create a 5th table with 4 fields having 4 FKs or is there another way?

Use a 5th table as an assignment table, then using a nested query with joins you can access the data in your results table
note, the 5th table would have 3 FK columns linked to the 3 other tables, and a 4th for a row id.

Related

Join from multiple tables to one column

I have some problem with sql view. I have some element_ids in my based main table. That column is connected with another 3 tables, there ane unique el_id for that 3 tables. I want to joint that three tables in one column taking another id and write in another column which item it is.
Example:
I take el_id=2 from main table, check if it is in table_1, then I check table_2, there is el_id=2, so I get item_id to my view and write the information in type column in my view.

Search Multiple Tables And Return 1 Combined Column MSQL

I need to write a query, and I'm not sure where to start... A join won't work since I'm basically querying for everything from both tables (for one column).
I have 2 tables, Each table different fields. But each table has 1 field in common i.e. Domains, I need to pull a list of all domains between both of them and return it in 1 column.
So table 1's domain field, and table 2's domain field showing in 1 column below eachother.
Also removing duplicates where there are. So if there's 2 domains that both are the same, I only need to show 1.
Is something like this possible in a SQL Query?

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.

2 SQL Queries from 2 Different Databases, Create 1 Table merging Columns

Suppose that I have two queries from two databases as below.
LIST 1 from [Alphabet] table
ABCD
A
B
C
D
LIST 2 from [Integers] table
Numbers
101
201
301
401
I would like to merge them so that in Excel sheet, I want to see that alphabet table is in columns A and integers table is in column B.
Here my suggestion:
Create Table merged ( Select [ABCD] from [Alphabet] join with [Numbers] from [Integers])
How can I improve the quality to work?
And should the row numbers be equal in both tables? Say, 27 letters and 27 integers, or would work with 27 letters and integers [1,20]?
This Is My First Answer and i hope it would help ...Back to your question
How can I improve the quality to work?
You Must use constraints to enforce rules for the data in your tables.
You need two columns of the same type, one on each table [Alphabet table] and [Integers table], to JOIN on.
Whether they're primary and foreign keys or not doesn't matter.
And should the row numbers be equal in both tables?
the answer is No
Depending on what joins you will Use !
SQL joins are used to query data from two or more tables, based on a relationship between the corresponding rows in these tables.
• INNER JOIN: Returns all corresponding rows from multiple tables.
• LEFT OUTER JOIN: Returns all rows from the left table, corresponding rows in the right table & if there are no matches, returns NULL.
• RIGHT OUTER JOIN: Returns all rows from the right table, corresponding rows in the left table, & if there are no matches, returns NULL.

SQL query what to include in from statement

Say I have four tables.
Table 1:
PK_Column_a
Table 2
PK_Column_c
FK_Column_a
Table 3
FK_Column_c
FK_Column_e
PK_c,e
Table 4
PK_Column_e
If I now want write a SQL query that will select
table1.Column_a, table2.column_c, table4.Column_e
And I wish to connect them where their foreign keys are pointing (e.g. Where table1.Column_a = table2.Column_a).
Do I need to include table 3 in my "FROM" statement? or can I connect table 2 and table 4 without joining them through table 3?
I believe the answer is yes, you would need to join to Table-3, because otherwise you won't be able to bring in data from Table-4. (There's no other way to describe the relationship for the data in Table-4 to the data in Table-1 or Table-2.)
You have to join through the Table-3, otherwise you will generate a cross join and the data won't be valid. Simply every row of table 1&2 will merge with every row from Table 4 ...