SQL inner join 3 tables with same key - sql

I want to try to join 3 tables together they all use the same key called ITEM_NO item number. I might actually need to add one more table as well called "USR_MAG_ITEM_EXP" would that also be a inner join?
Tables are dbo.IM_PRC, dbo.IM_ITEM, dbo.IM_INV_CELL sorry for being a noob!
SELECT dbo.IM_PRC.ITEM_NO, dbo.IM_PRC.LOC_ID, dbo.IM_PRC.DIM_1_UPR,
dbo.IM_PRC.DIM_2_UPR,dbo.IM_PRC.DIM_3_UPR, dbo.IM_PRC.REG_PRC,dbo.IM_PRC.PRC_1, dbo.IM_PRC.PRC_2, dbo.IM_ITEM.CATEG_COD, dbo.IM_ITEM.SUBCAT_COD, dbo.IM_ITEM.STAT, dbo.IM_ITEM.ITEM_VEND_NO, dbo.IM_ITEM.IS_ECOMM_ITEM, dbo.IM_ITEM.VEND_ITEM_NO,dbo.IM_ITEM.LST_COST,dbo.IM_ITEM.USER_MAG_NAME, dbo.IM_INV_CELL.LOC_ID,dbo.IM_INV_CELL.DIM_1_UPR, dbo.IM_INV_CELL.DIM_2_UPR, dbo.IM_INV_CELL.DIM_3_UPR, dbo.IM_INV_CELL.MIN_QTY,dbo.IM_INV_CELL.MAX_QTY, dbo.IM_INV_CELL.QTY_ON_HND,
FROM dbo.IM_PRC
INNER JOIN dbo.IM_ITEM ON dbo.IM_PRC.ITEM_NO = dbo.IM_ITEM.ITEM_NO
INNER JOIN dbo.IM_INV_CELL ON dbo.IM_INV_CELL.ITEM_NO = dbo.IM_PRC.ITEM_NO
WHERE (dbo.IM_ITEM.ITEM_VEND_NO = 'tum')
Thanks!!!

You have to repeat JOIN for each separate table:
SELECT *
FROM dbo.IM_PRC
INNER JOIN dbo.IM_ITEM ON dbo.IM_PRC.ITEM_NO = dbo.IM_ITEM.ITEM_NO
INNER JOIN dbo.IM_INV_CELL ON dbo.IM_INV_CELL.ITEM_NO = dbo.IM_PRC.ITEM_NO
WHERE (dbo.IM_ITEM.ITEM_VEND_NO = 'X')
I recommend using aliases and removing unnecessary parentheses too:
SELECT *
FROM dbo.IM_PRC P
INNER JOIN dbo.IM_ITEM I ON P.ITEM_NO = I.ITEM_NO
INNER JOIN dbo.IM_INV_CELL IC ON IC.ITEM_NO = P.ITEM_NO
WHERE I.ITEM_VEND_NO = 'X'

SELECT *
FROM IM_PRC ip
INNER JOIN IM_ITEM ii ON ip.ITEM_NO = ii.ITEM_NO
INNER JOIN IM_INV_CELL iic ON ip.ITEM_NO = iic.ITEM_NO
WHERE ii.ITEM_VEND_NO = 'X'
You can use LEFT or RIGHT joins if you need all details from either of tables instead of INNER.

You should join the 3 table explicitally
SELECT *
FROM dbo.IM_PRC
INNER JOIN dbo.IM_ITEM ON dbo.IM_PRC.ITEM_NO = dbo.IM_ITEM.ITEM_NO
INNER JOIN dbo.IM_INV_CELL ON dbo.IM_INV_CELL.ITEM_NO = dbo.IM_PRC.ITEM_NO
AND dbodbo.IM_INV_CELL.M_VEND_NO = 'X'

See the below
Select *
From dbo.IM_PRC PRC
Inner join dbo.IM_ITEM ITEM ON PRC. ITEM_NO = ITEM. ITEM_NO
Inner join dbo.IM_INV_CELL CELL ON PRC. ITEM_NO = CELL. ITEM_NO
WHERE ITEM.ITEM_VEND_NO = 'X'

Related

I need to group (tbl_types.name AS type) in a same row (postgres 14)

I need help with a select in postgres, I need to group X types into a single line, for example: type: multiple, trully, I need help on the type column
SELECT tbl_questions.id AS id,
tbl_questions.question AS question,
tbl_questions.year AS year,
tbl_question_responses.response_id AS response_id,
tbl_responses.response AS response_content,
tbl_responses.response_type AS response,
tbl_subjects.name AS subject,
tbl_categories.name AS category,
tbl_types.name AS type,
tbl_institutions.name AS institution
FROM tbl_questions
INNER JOIN tbl_question_responses ON tbl_questions.id = tbl_question_responses.question_id
INNER JOIN tbl_responses ON tbl_question_responses.response_id = tbl_responses.id
INNER JOIN tbl_question_subjects ON tbl_questions.id = tbl_question_subjects.question_id
INNER JOIN tbl_subjects ON tbl_subjects.id = tbl_question_subjects.subject_id
INNER JOIN tbl_question_categories ON tbl_questions.id = tbl_question_categories.question_id
INNER JOIN tbl_categories ON tbl_categories.id = tbl_question_categories.category_id
INNER JOIN tbl_question_types ON tbl_questions.id = tbl_question_types.question_id
INNER JOIN tbl_types ON tbl_types.id = tbl_question_types.type_id
INNER JOIN tbl_question_institutions ON tbl_question_institutions.question_id = tbl_questions.id
INNER JOIN tbl_institutions ON tbl_institutions.id = tbl_question_institutions.institution_id
WHERE tbl_questions.id = 'c7aa15cb-27e5-4f28-9141-483f7cce8e56'
This is a select result

Joining multiple tables ORACLE

SELECT *
FROM STORE
,MATERIAL
,STOREBIN
,VENDOR
,MATERIALPRICE
,SD
WHERE STOREBIN.STOREBINID(+) = SD.STOREBINID
AND SD.VENDORID = VENDOR.VENDORID(+)
AND MATERIAL.MATERIALID = MATERIALPRICE.MATERIALID(+)
AND STORE.STOREID = SD.STOREID
I have this simple query here, but i want to change the (+) notations into joins, how would this be possible? I was thinking:
RIGHT JOIN SD ON STOREBIN.STOREBINID = SD.STOREBINID
LEFT JOIN VENDOR ON VENDOR.VENDORID = SD.VENDORID
LEFT JOIN MATERIALPRICE ON MATERIALPRICE.MATERIAID = MATERIAL.MATERIALID
WHERE STORE.STOREID = SD.STOREID
But that hardly seems right because i need to join each table onto different tables.
This would be equivalent (if we also add the additional criteria you mentioned):
FROM
SD
INNER JOIN STORE as S
on S.STOREID = SD.STOREID
INNER JOIN MATERIAL as M
ON M.MATERIALID = SD.MATERIALID
LEFT JOIN STOREBIN as SB
ON SB.STOREBINID = SD.STOREBINID
LEFT JOIN VENDOR as V
ON V.VENDORID = SD.VENDORID
LEFT JOIN MATERIALPRICE as MP
ON MP.MATERIALID = M.MATERIALID

INNER JOIN from another INNER JOIN

I have a database with 3 tables :
PM_FLX_ENTE
PM_ST
PM_ANO_SP
and I would like to get PM_ANO_SP.L_ANO_SP for certain values of PM_ANO_L.L_ANO which contains a code number with '/'.
SELECT COUNT(RF_INTRN),TRIM(pm_st.c_st),TRIM(pm_ano_l.L_ANO)
FROM PM_FLX_ENTE
INNER JOIN PM_ST ON PM_FLX_ENTE.C_ST = PM_ST.C_ST
INNER JOIN PM_ANO_L ON PM_FLX_ENTE.C_ANO = pm_ano_l.c_ano
WHERE pm_flx_ente.C_ANO <> '0000'
AND pm_ano_l.c_lang = 'FR'
group by TRIM(pm_st.c_st), TRIM(pm_ano_l.L_ANO)
ORDER BY COUNT(RF_INTRN) DESC
Can you help me please
Are you seeing an error of some sort?
Please note, that when you do multiple inner joins, the previous join conditions have to be true for the other joins to work.
I.E
INNER JOIN PM_ST ON PM_FLX_ENTE.C_ST = PM_ST.C_ST
Has to evaluate true in order for the below to work
INNER JOIN PM_ANO_L ON PM_FLX_ENTE.C_ANO = pm_ano_l.c_ano
To solve this, you may want to change your INNER JOINs to LEFT JOINs
SELECT TRIM(pm_st.c_st),TRIM(pm_ano_l.L_ANO),COUNT(RF_INTRN)
FROM PM_FLX_ENTE
LEFT JOIN PM_ST ON PM_FLX_ENTE.C_ST = PM_ST.C_ST
LEFT JOIN PM_ANO_L ON PM_FLX_ENTE.C_ANO = pm_ano_l.c_ano
LEFT JOIN PM_ANO_SP ON PM_ANO_L.L_ANO = pm_ano_sp.l_ano_sp
WHERE pm_flx_ente.C_ANO <> '0000'
AND C_LANG = 'FR'
and pm_ano_l.L_ANO like '%/%'
GROUP BY pm_st.c_st,pm_ano_l.L_ANO

How to omit the table join based on some condition?

=======================
SELECT *
FROM PRODUCT P
JOIN PRODUCT_LOC PL
ON P.PRODUCT_ID = PL.PRODUCT_ID
JOIN PRODUCT_LOC_DEF PLD AND PL.LOC_ID = PLD.LOC_ID
JOIN PRODUCT_CURRENT_LOC PCL
ON PLD.LOC_ID = PCL.LOC_ID AND P.PRODUCT_ID = PCL.PRODUCT_ID
How should I modify the query to get expected result in case 1 and case 2 as given in attached snapshot?
Preferred:
I am looking for the modification in the same query instead of making two query and union them.
Just use a left join instead of an inner join:
SELECT *
FROM PRODUCT P
LEFT JOIN PRODUCT_LOC PL ON P.PRODUCT_ID = PL.PRODUCT_ID
LEFT JOIN PRODUCT_LOC_DEF PLD AND PL.PRODUCT_LOC_ID = PLD.LOC_ID
LEFT JOIN PRODUCT_CURRENT_LOC PCL ON PLD.LOC_ID = PCL.LOC_ID
You should try this:
SELECT * FROM
PRODUCT p
INNER JOIN
PRODUCT_CURRENT_LOC pcl
ON
p.PRODUCT_ID = pcl.PRODUCT_ID
INNER JOIN
PRODUCT_LOC pc
ON
pc.LOC_ID = pcl.LOC_ID
UNION
SELECT * FROM
PRODUCT p
LEFT OUTER JOIN
PRODUCT_CURRENT_LOC pcl
ON
p.PRODUCT_ID = pcl.PRODUCT_ID
AND
pcl.PRODUCT_ID IS NULL
LEFT OUTER JOIN
PRODUCT_LOC pc
ON
pc.LOC_ID = pcl.LOC_ID
AND
pc.LOC_ID IS NULL

Inner join more than 2 tables in a single database

i want to innerjoin more than 3 tables in one select query in one database all of them are having rm_id as a common column but i want to display the rows of all tables in one sql query is it possible if it is then i would hearly request u to provide some code
select * from
bk_det inner join
bk_rep inner join
bk_sec inner join
mut_det inner join
rm_det inner join
soil_det
on
bk_det.rm_id = bk_rep.rm_id = bk_sec.rm_id = mut_det.rm_id = rm_det.rm_id = soil_det.rm_id
You need an on clause for each inner join. e.g.
select * from a
inner join b on a.id = b.id
inner join c on b.id = c.id
You are missing the ON clause for each join:
select *
from bk_det
inner join bk_rep
on bk_det.rm_id = bk_rep.rm_id
inner join bk_sec
on bk_rep.rm_id = bk_sec.rm_id
inner join mut_det
on bk_sec.rm_id = mut_det.rm_id
inner join rm_det
on mut_det.rm_id = rm_det.rm_id
inner join soil_det
on rm_det.rm_id = soil_det.rm_id
Note: you will have to check the join condition column names since I do not know your table structure
If you need help with learning join syntax, here is a great visual explanation of joins.
Since you are using an INNER JOIN this will return records if the rm_id exists in each table.
You might need to use a LEFT JOIN:
select *
from bk_det
left join bk_rep
on bk_det.rm_id = bk_rep.rm_id
left join bk_sec
on bk_rep.rm_id = bk_sec.rm_id
left join mut_det
on bk_sec.rm_id = mut_det.rm_id
left join rm_det
on mut_det.rm_id = rm_det.rm_id
left join soil_det
on rm_det.rm_id = soil_det.rm_id