I am trying to complete a project for school and the last step has me do a complex join between all of my tables. I have been going over this so many times, but I can't understand why it won't display the table when I run the SQL. Any help would be awesome.
you can use left join to test if system is able to find any record.
add left join instead of join so you can see where you are missing data it might possible that system was not able to find records from sub table that's why its empty.
Related
i wanted to compare and identify certain [transaction ids] that exist in one table which is located in server 1 but does not exist in a similar transformed table on a different server (server 2).
These two tables were supposed to be the same but we noticed some transaction ids were missing on the table that is located in server 2. My goal is to identify all the missing transaction ids.
The query that i am looking to write is as below
SELECT
server_1.table_1.[transaction_id] from server_1.table_1
JOIN
server_2.table_2 ON
Table_1.[transaction_id] = Table_2.[transaction_id]
WHERE
server_1.table_1.[transaction_id] NOT IN server_2.table_2.[transaction_id]
one recommendation I have received was
"Let's select the IDs from the source select the result, right click and in SQL complete select general temp table script
Then you can run the scripts on the target server to create a temptable and perform your analysis
Because of the size of the table perhaps you need to do its in batches"
if anyone can help me explain this process as well please?
Thank you very much for your help in advance
i am new for this , i apologize if I have not explained it enough or if i am not clear with my ask
You can do that with a simple OUTER join (LEFT JOIN):
SELECT table1.transaction_id as missing_in_table2
FROM table1 LEFT JOIN table2
ON table1.transaction_id=table2.transaction_id
WHERE table2.transaction_id IS NULL
I have applied a join query on two tables.
One table has data but the other one does not.
When i try to join table that does not have data, it is not returning anything.
I expected data to come at least from the table which has data.
$allTours = DB::table("virtual_tours")
->join('destinations','virtual_tours.destination_id','=','destinations.id')
->join('virtual_tour_comments','virtual_tours.id','=','virtual_tour_comments.virtual_tour_id')
->get();
Virtual tour comments doensn't have data
You're using a regular join, which becomes an inner join. Meaning data without a match on your conditions gets filtered out. Inner joins are meant to be used to find matches.
Join explanations
What you want is a left join, where data gets always joined but columns simply become NULL if they're not present and you'll get all results.
Since you're using laravel, here's the reference for it:
Laravel Joins
i have an issue with a sql query and how the output is being displayed, you see, i have 3 tables and have at least one field in common, the thing is when i join 2 tables together the information i need is displayed properly, but when i join the third the output goes insane and duplicates the results way too much and i need to figure out why it is happening, down below i'll show you all the tables and relations between each other
this is how the tables are related to each other
This is how the first table (dbo_predios) is made the first three fields are the only relevant in this case
This is how the second table (dbo_permisos_obras_mayores) is made the first three fields are the only relevant in this case as well, the second two can match the first table (dbo_predios)
And here is how the third table (dbo_recepciones_obras_mayores) is made, the fourth field is the only relevant in this case, it could relate to the second table (dbo_permisos_obras_mayores) to the same name field
okay, now that is structurewise, now the query i'm executing is the following:
SELECT
dbo_predios.codigo_unico_predio,
dbo_permisos_obras_mayores.numero_permiso_edificacion,
dbo_permisos_obras_mayores.fecha_permiso_edificacion
FROM dbo_predios
INNER JOIN dbo_permisos_obras_mayores ON dbo_predios.codigo_manzana_predio = dbo_permisos_obras_mayores.codigo_manzana_predio AND dbo_predios.codigo_lote_predio = dbo_permisos_obras_mayores.codigo_lote_predio
INNER JOIN dbo_recepciones_obras_mayores ON dbo_permisos_obras_mayores.numero_recepcion_permiso = dbo_recepciones_obras_mayores.numero_recepcion_permiso
WHERE dbo_permisos_obras_mayores.codigo_manzana_predio = 9402 AND dbo_permisos_obras_mayores.codigo_lote_predio = 30
And the result of executing the query in that way is this:
Later on i did some trial and error and removed the second inner join line, and the result surprised me, here is what happened:
Conclusion: in brief the third table is causing the cartesian product, why? i wish i knew why, what do you think of this particular case? i'd thank any help you could give me, thanks in advance.
Here's the solution - since you are saying that the numero_recepcion_permiso is blank, just add the condition to the inner join, to exclude empty ones:
SELECT
dbo_predios.codigo_unico_predio,
dbo_permisos_obras_mayores.numero_permiso_edificacion,
dbo_permisos_obras_mayores.fecha_permiso_edificacion
FROM dbo_predios
INNER JOIN dbo_permisos_obras_mayores ON dbo_predios.codigo_manzana_predio = dbo_permisos_obras_mayores.codigo_manzana_predio AND dbo_predios.codigo_lote_predio = dbo_permisos_obras_mayores.codigo_lote_predio
INNER JOIN dbo_recepciones_obras_mayores ON dbo_permisos_obras_mayores.numero_recepcion_permiso = dbo_recepciones_obras_mayores.numero_recepcion_permiso
AND dbo_recepciones_obras_mayores.numero_recepcion_permiso <>''
WHERE dbo_permisos_obras_mayores.codigo_manzana_predio = 9402 AND dbo_permisos_obras_mayores.codigo_lote_predio = 30
With that said, should that field allowed to be blank or NULL? Perhaps you need to add a constraint to your table to prevent that scenario. Another suggestion - why did you choose NUMERIC(18,0) as the data type on the primary key for those tables? I would prefer a simple INT or BIGINT and maybe let the database generate the sequence for me.
Okay, i did what Icarus told me and i figured out something that is useful, you see, i made a big mistake and the number combination i was trying out didn't have a numero_recepcion_permiso so the output column is completely blank, however when there is an actual numero_recepcion_permiso it shows correctly, anyway i still need that doesn't output that much amount of repeated rows, how can i fix that? thank y'all for your help so far
First of all, make sure that both values exist in both fields and they actually match or else could generate that amount of repeated rows, however the amount of rows repeated is something i can't tell since i don't know what your actual data is, but that may clear up a Little bit that issue
I have a table full of code IDs and their descriptions in access. And in another table is a field that has code IDs that correlate to the IDs in the Codes table. I am trying to design a macro that when executed will replace the code ID in the second table with the correct description but I am unsure a way to do this. I was thinking of using a SQL Insert query to do so but am unsure of what the statement would look like.
JOIN statement:
SELECT ShouldImportMetricsIDsTable.FORMULARYID, ReasonCodes.Description
FROM ShouldImportMetricsIDsTable,ReasonCodes
INNER JOIN ReasonCodes
ON ShouldImportMetricsIDsTable.ReasonCode=ReasonCodes.CodeID
Mention ReasonCodes only once in your query's FROM clause.
Change this ...
FROM ShouldImportMetricsIDsTable,ReasonCodes
INNER JOIN ReasonCodes
To this ...
FROM ShouldImportMetricsIDsTable
INNER JOIN ReasonCodes
As general advice, I suggest you begin your queries in the Access query designer. At least choose the data sources (tables or saved queries) and set up joins there.
With your original example, the designer would have applied an alias, ReasonCodes_1, for one of those duplicate ReasonCodes names. And that could be an early warning that the data sources aren't correct.
I'm always discouraged from using one, but is there a circumstance when it's the best approach?
It's rare, but I have a few cases where it's used. Typically in exception reports or ETL or other very peculiar situations where both sides have data you are trying to combine.
The alternative is to use an INNER JOIN, a LEFT JOIN (with right side IS NULL) and a RIGHT JOIN (with left side IS NULL) and do a UNION - sometimes this approach is better because you can customize each individual join more obviously (and add a derived column to indicate which side is found or whether it's found in both and which one is going to win).
I noticed that the wikipedia page provides an example.
For example, this allows us to see
each employee who is in a department
and each department that has an
employee, but also see each employee
who is not part of a department and
each department which doesn't have an
employee.
Note that I never encountered the need of a full outer join in practice...
I've used full outer joins when attempting to find mismatched, orphaned data, from both of my tables and wanted all of my result set, not just matches.
Just today I had to use Full Outer Join. It is handy in situations where you're comparing two tables. For example, the two tables I was comparing were from different systems so I wanted to get following information:
Table A has any rows that are not in Table B
Table B has any rows that are not in Table A
Duplicates in either Table A or Table B
For matching rows whether values are different (Example: The table A and Table B both have Acct# 12345, LoanID abc123, but Interest Rate or Loan Amount is different
In addition, I created an additional field in SELECT statement that uses a CASE statement to 'comment' why I am flagging this row. Example: Interest Rate does not match / The Acct doesn't exist in System A, etc.
Then saved it as a view. Now, I can use this view to either create a report and send it to users for data correction/entry or use it to pull specific population by 'comment' field I created using a CASE statement (example: all records with non-matching interest rates) in my stored procedure and automate correction, etc.
If you want to see an example, let me know.
The rare times i have used it has been around testing for NULLs on both sides of the join in case i think data is missing from the initial INNER JOIN used in the SQL i'm testing on.
They're handy for finding orphaned data but I rarely use then in production code. I wouldn't be "always discouraged from using one" but I think in the real world they are less frequently the best solution compared to inners and left/right outers.
In the rare times that I used Full Outer Join it was for data analysis and comparison purpose such as when comparing two customers tables from different databases to find out duplicates in each table or to compare the two tables structures, or to find out null values in one table compared to the other, or finding missing information in one tables compared to the other.
For example, suppose you have two tables: one containing customer data and another containing order data. A full outer join would allow you to see all customers and all orders, even if some customers have no orders or some orders have no corresponding customer. This can help you identify any gaps in the data and ensure that all relevant information is included in the result set.
It's important to note that a full outer join can produce a huge result set since it includes all rows from both tables. This can be inefficient in terms of performance, so it's best to use a full outer join only when it is necessary to include all rows from both tables.
SELECT *
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
This will return all rows from both table1 and table2, filling in NULL values for missing matches on either side.