SQL JOIN two tables with two equally named columns - sql

I have two tables that I'd like to join
Right now I just do :
SELECT * FROM (default_insurance)
JOIN default_profiles ON uid = default_profiles.id
WHERE `uid` = '1
problem is that both default_insurance and default_profiles contains a column named company, and I only want the one from default_insurance, but is there a way to make a join that will automatically prefer columns from one of the tables WITHOUT having to SELECT (all columns that I want)

You can always specify what exactly do you need (+use table aliases):
SELECT t1.*, t2.company AS default_insurance_company
FROM default_profiles t1 LEFT JOIN default_insurance t2
ON t1.uid = t2.id
WHERE t1.uid = 1
(MySQL example)
Above will return all columns from t1 and additionally column company from t2, but on your result it will be named default_insurance_company.

Automatically no. You need to use like this:
SELECT [all columns that you want], di.company
FROM default_insurance di
JOIN default_profiles dp ON di.uid = dp.id
WHERE di.uid = '1

Related

SQL - join multiplying data

I need to produce a synthetic table from 2 tables but one of them is tricky.
First table (sample):
Second table (sample also) :
I want to use it to find the 'store' of each 'pers'
What i want at the end :
(and i want to keep the 'zero' if sum_of_duration is null)
My first code :
SELECT
QRT,
STORE,
sum(DURATION) as SUM_of_DURATION
FROM T1
LEFT JOIN T2
on T1.PERS=T2.PERS
group by QRT, STORE
But my 'Sum_of_durations' are out of the roof because of multiple joins induced by table 2 i guess.
Any help ?
Thanks
Remove duplicates before joining:
SELECT T1.QRT, T2.STORE,
SUM(T1.DURATION) as SUM_of_DURATION
FROM T1 LEFT JOIN
(SELECT DISTINCT QRT, PERS
FROM t2
) t2
ON T1.PERS = T2.PERS
GROUP BY T1.QRT, T2.STORE;

Join table in oracle database

I have 2 tables that showing data Item master and BOM. I would like to join the tables between Item master as T1 and BOM as T2 and the additional table for table BOM as T3. Item master table containing ITM_CD, ITM_TYP (1,2,3,4) where each ITM_TYP represents a code for the first digit on ITM_CD. The thing that I want is like the picture below
CHILD_CD2 value replace to CHILD_CD1 value. So the data should be like this. What query should I fix ? I am very new using oracle query.
Here is mycode;
SELECT DISTINCT
T1.ITM_CD,
T2.C_ITM_CD AS CHILD_CD1,
T3.C_ITM_CD AS CHILD_CD2
FROM CM_HINMO_ALL T1
INNER JOIN (SELECT P_ITM_CD, C_ITM_CD, BOM_PTN FROM SM_BOM_ALL) T2
ON T1.ITM_CD = T2.P_ITM_CD
LEFT JOIN (SELECT P_ITM_CD, C_ITM_CD, BOM_PTN FROM SM_BOM_ALL) T3
ON T2.C_ITM_CD = t3.P_ITM_CD
WHERE 0=0
AND T2.BOM_PTN IN (1)
AND T1.ITM_TYP IN (1,2)
AND T1.ITM_CD = '110100370'
ORDER BY 2
Just use Case expression to replace the values.
SELECT ITM_CD, CASE WHEN CHILD_CD2 IS NULL THEN CHILD_CD2 ELSE CHILD_CD1 END AS CHILD_CD1
FROM TABLE1
If I understood, you want child_cd2 value should taken precedence over child_cd1 if available. If this assumption is right then we can use coalesce which returns the fist non null expression to achieve the same.
SELECT DISTINCT
T1.ITM_CD,
COALESCE(T3.C_ITM_CD,T2.C_ITM_CD) AS CHILD_CD1
FROM CM_HINMO_ALL T1
INNER JOIN SM_BOM_ALL T2
ON T1.ITM_CD = T2.P_ITM_CD
LEFT JOIN SM_BOM_ALL T3
ON T2.C_ITM_CD = t3.P_ITM_CD
WHERE T2.BOM_PTN IN (1)
AND T1.ITM_TYP IN (1,2)
AND T1.ITM_CD = '110100370'
ORDER BY 2

Joining tables using concat column

So I have two tables I want to join using SQL.
Since they did not have a common column I used
SELECT NEW_ID = CONCAT ('0',table1.ID)
Now that I have the new column with matching data in both tables, how do I join both tables? Is there any way to use the NEW_ID column as a temporary column so that I do not have to alter table 1?
In your case, suitable. of course in terms of performance it is not the best solution ( compare fields with diffrent types)
Select *
From Table1 As t1 inner join Table2 as t2
ON t1.ID = CAST(t2.ID AS INT)
Different ways, here's one:
;WITH CTE AS
(
SELECT NEW_ID = CONCAT ('0',table1.ID) FROM TableA
)
SELECT * FROM CTE AS C INNER JOIN TableB AS B ON C.New_ID = B.ID
You Can Just give the Concrete Expression in the Join.You Don't have to add a new Column For that. Like this
SELECT
*
FROM Table1 A
INNER JOIN Table2 B
ON RIGHT('00'+A.Id,2) = RIGHT('00'+B.Id,2)

Join SQL Tables with Unique Data (Not same number of columns!)

How can I join three or four SQL tables that DO NOT have an equal amount of rows while ensuring that there are no duplicates of a primary/foreign key?
Structure:
Table1: id, first_name, last_name, email
Table2: id (independent of id in table 1), name, location, table1_id, table2_id
Table3: id, name, location
I want all of the data from table 1, then all of the data from table 2 corresponding with the table1_id without duplicates.
Kind of tricky for a new guy...
Not sure what do you want to do with Table3.
A LEFT JOIN returns all records from the LEFT table, and the matched records from the right table. If there is no match (from the right side), then the result is NULL.
So per example:
SELECT * FROM Table1 AS t
LEFT JOIN Table2 AS tt
ON t.id = tt.id
The LEFT table refers to the table statement before the LEFT JOIN, and the RIGHT table refers to the table statement after the LEFT JOIN. If you want to add in Table3 as well, use the same logic:
SELECT * FROM Table1 AS t
LEFT JOIN Table2 AS tt
ON t.id = tt.id
LEFT JOIN Table3 AS ttt
ON t.id = ttt.id
Note, that I use alias names for the tables (by using AS), so that I can more easily refer to a specific table. For example, t refers to Table1, tt refers to Table2, and ttt refers to Table3.
Joins are often used in SQL, therefore it is useful to look into: INNER JOIN, RIGHT JOIN, FULL JOIN, and SELF JOIN, as well.
Hope this helps.
Good luck with learning!
You will want to use an LEFT JOIN
SELECT * FROM table1 LEFT JOIN table2 ON Table1.ID = Table2.table1_id

combine multiple tables based on commonalities of a single column

How do i make one main table that has one instance of the column 'Instructor' and shows 'formsreturned' and 'availability_avg' next to the main 'Instructor' column?
Just create a standard INNER JOIN:
SELECT T1.Instructor, T1.Availability_Avg, T2.FormsReturned
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Instructor = T2.Instructor