SQL query a column based on two other columns in different tables (screenshot inside) - sql

I am trying to resolve this pb:
click to see the sketch of the tables for explanation
Basically I want to join the Name column from the Description table on the Unique_Number of the ID table. The issue is that there is not primary key in the Description table and unique values are only found when combining the two columns.
Thanks!

There is no point in joining these two tables as you can't retrieve any useful information after joining.
Also, going by your column names how can names be joined on unique_id.
If you want to join these two tables to get the unique_id and names for same codes you should do inner join on codes columns.
SELECT CONCAT(unique_number,name)
FROM table_id a,table_description b
WHERE a.code_a = b.code_a
AND a.code_b = b.code_b;

Related

How to get the differences between two - kind of - duplicated tables (sql)

Prolog:
I have two tables in two different databases, one is an updated version of the other. For example we could imagine that one year ago I duplicated table 1 in the new db (say, table 2), and from then I started working on table 2 never updating table 1.
I would like to compare the two tables, to get the differences that have grown in this period of time (the tables has preserved the structure, so that comparison has meaning)
My way of proceeding was to create a third table, in which I would like to copy both table 1 and table 2, and then count the number of repetitions of every entry.
In my opinion, this, added to a new attribute that specifies for every entry the table where he cames from would do the job.
Problem:
Copying the two tables into the third table I get the (obvious) error to have two duplicate key values in a unique or primary key costraint.
How could I bypass the error or how could do the same job better? Any idea is appreciated
Something like this should do what you want if A and B have the same structure, otherwise just select and rename the columns you want to confront....
SELECT
*
FROM
B
WHERE NOT EXISTS (SELECT * FROM A)
if NOT EXISTS doesn't work in your DBMS you could also use a left outer join comparing the rows columns values.
SELECT
A.*
from
A left outer join B
on A.col = B.col and ....

How to see pull up all relevant info from a table given a list of values SQL

Currently I have a list of manufacturing numbers in a table called "Mfr_Numbers_Table".
I also have a master table called Billings
I would like to generate a query where I see all line items that matches with what is on Mfr_Numbers_Tables from the Billings Table.
How do I do this? I can just write 50 queries with a where clause being the Mfr_numbers, but that would take too long.
You need put the tables in a relation in your query.
This is done by a join on the tables on a field which is in both of them.
For example if this field is named MfrNumber:
Select Billings.*, Mfr_Numbers_Table.*
From Mfr_Numbers_Table Inner Join Billings
On Mfr_Numbers_Table.MfrNumber = Billings.MfrNumber
Usually you don't select all fields from all joined tables, but select what you really need.
Also you can still filter (Where) and/or order (Order By) the result.
The name of the joined field isn't relevant, but its datatype and for sure the content.
For more please read this article: Join tables and queries

Joining on tables where one table includes collated columns

I'm trying to join two tables in Snowflake on a column called 'Name'. However, one table has names all in lowercase and the other tables has names with first character as uppercase followed by lowercase so the tables can't find a match.
Usually, you'd use an UPPER or LOWER function to find an exact match but in one table, the column has been collated and this function cannot be used here.
Does anyone have any ideas on how I can successfully join these two tables together?
Thanks
You might be able to join the two tables using the COLLATE function on the Name column of the table which was not collated:
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.Name = COLLATE(Name, 'upper');
This assumes that the first table's Name column has been collated to all uppercase.

Power Pivot relationships

Trying to create relationships (joins) between tables in power pivot.
Got 2 tables I wold like to join together, connected with a common column = CustomerID.
One is a Fact Table the other Dim table (look up).
I have run the "remove duplicates" on both tables without any problem.
But I still get an error saying : "the relationship cannot be created because each column contains duplicate values. Select at least one column that contains only unique values".
The Fact Table contains duplicates (as it should?) and the Dim Table do not, why do I get this error?
Help much appreciated
Created an appended table with both columns "CustomerID". After the columns where appended together I could "remove duplicates" and connect the tables together through the newly created appended table.
Don't know if this causes another problem later however.
You can also check for duplicate id values in a column by using the group by feature.
Remove all columns except ID, add a column that consists only of the number 1.
Group by ID, summing the content of the added column and filter out IDs whose total equals 1. What's left are duplicated IDs.

Merge or Union tables in Access

I have two tables that I would like to combine into an unduplicated list. I have a 'SUB' table that has a 1 column named 'ID' containing a unique identifier for 11,000 some records. I also have another table with 75,000 rows called 'MASTER'. It contains two columns, 'ID' which has the same unique identifier and 'CODE' which contains a unique code for each ID. I want to create a new table that has the 11,000 IDs from the 'SUB' table with the corresponding 'CODE's that match the 'SUB' IDs from the 'MASTER' table. I have used a basic UNION Query, but the results had duplication in the 'ID' column. I tried to consolidate the table produced by that query using Excel, but the list was too long to crunch. Any help? I know this is a basic, but I am not a database person... What would the SQL code look like to achieve this?
Thanks!
You should use JOIN instead of UNION to achieve what you want.
Something like that should work:
SELECT SUB.ID, MASTER.CODE
FROM SUB
JOIN MASTER
ON SUB.ID = MASTER.ID
In general, JOIN allows you to match some rows from one table to the rows from other table according to the value(s) in these rows (think of it as "gluing" rows together along the vertical axis to form longer rows), while what UNION does is just adding all rows from one table below other table (i.e. appends one table to the end of other table along the horizontal axis).