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
Related
I have the below query, and for some reason the Left Outer Join to PO_HEADERS_ALL POH is causing the error ORA-00918: column ambiguously defined on Left Outer Join. I don't have any columns selected from this table that would be causing this error from what I see. If I comment out this join then the query validates successfully. Appreciate any assistance on this, thanks!
SELECT
ESIB.ITEM_NUMBER,
ESIT.LONG_DESCRIPTION,
IOP.ORGANIZATION_CODE,
IOQD.SUBINVENTORY_CODE,
SUM(
IOQD.PRIMARY_TRANSACTION_QUANTITY
) ONHAND_QTY,
ESIB.PRIMARY_UOM_CODE ONHAND_UOM,
UOM.UOM_CODE TO_UOM,
INTRACONV.CONVERSION_RATE
, SEGMENT1||'-'||SEGMENT2||'-'||SEGMENT3||'-'||SEGMENT4||'-'||SEGMENT5 LOCATOR
,hp.party_name MFG
,etp.tp_item_number MFG_PART_NUM
FROM
EGP_SYSTEM_ITEMS_B ESIB
LEFT OUTER JOIN EGP_SYSTEM_ITEMS_TL ESIT ON ESIB.INVENTORY_ITEM_ID = ESIT.INVENTORY_ITEM_ID
AND ESIB.ORGANIZATION_ID = ESIT.ORGANIZATION_ID --Ended
INNER JOIN INV_ONHAND_QUANTITIES_DETAIL IOQD ON ESIB.INVENTORY_ITEM_ID = IOQD.INVENTORY_ITEM_ID
AND ESIB.ORGANIZATION_ID = IOQD.ORGANIZATION_ID
LEFT OUTER JOIN inv_item_locations LOC ON inventory_location_id= IOQD.locator_id
LEFT OUTER JOIN egp_item_relationships_b eir ON eir.inventory_item_id = ESIB.inventory_item_id and eir.item_relationship_type = 'MFG_PART_NUM'
LEFT OUTER JOIN egp_trading_partner_items etp ON etp.tp_item_id = eir.tp_item_id
and etp.tp_type = 'MANUFACTURER'
LEFT OUTER JOIN hz_parties hp ON hp.party_id = etp.trading_partner_id
LEFT OUTER JOIN PO_LINES_ALL POL ON POL.ITEM_ID = ESIB.INVENTORY_ITEM_ID
--ERROR IS OCCURING WITH THE FOLLOWING JOIN:
LEFT OUTER JOIN PO_HEADERS_ALL POH ON POH.PO_HEADER_ID = POL.PO_HEADER_ID
INNER JOIN INV_UNITS_OF_MEASURE_B UOM ON UOM.BASE_UOM_FLAG = 'Y'
LEFT OUTER JOIN INV_UOM_CONVERSIONS INTRACONV ON --INTRACONV.UOM_CODE = IOQD.TRANSACTION_UOM_CODE
INTRACONV.UOM_CODE = ESIB.PRIMARY_UOM_CODE
AND UOM.UOM_CLASS = INTRACONV.UOM_CLASS
AND INTRACONV.INVENTORY_ITEM_ID = IOQD.INVENTORY_ITEM_ID
--AND INTRACONV.UOM_CODE = UOM.UOM_CODE
INNER JOIN INV_ORG_PARAMETERS IOP ON ESIB.ORGANIZATION_ID = IOP.ORGANIZATION_ID
WHERE
(
INTRACONV.UOM_CODE = IOQD.TRANSACTION_UOM_CODE
OR ESIB.PRIMARY_UOM_CODE = UOM.UOM_CODE
)
GROUP BY
ESIB.ITEM_NUMBER,
ESIT.LONG_DESCRIPTION,
IOP.ORGANIZATION_CODE,
IOQD.SUBINVENTORY_CODE,
ESIB.PRIMARY_UOM_CODE,
UOM.UOM_CODE,
INTRACONV.CONVERSION_RATE
, SEGMENT1||'-'||SEGMENT2||'-'||SEGMENT3||'-'||SEGMENT4||'-'||SEGMENT5
,hp.party_name
,etp.tp_item_number
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'
SELECT Count(TRK.REACTIE_ID),
S.UITGEVER,
S.NAAM,
S.BESCHRIJVING,
S.AFBEELDING_BESTANDSPAD,
S.SUBTITLE,
C.NAAM
FROM CATEGORIEEN_KOPPELTABEL CK
LEFT OUTER JOIN CATEGORIE C
ON C.CATEGORIE_ID = CK.CATEGORIE_ID
LEFT OUTER JOIN TV_SHOW S
ON S.TV_SHOW_ID = CK.TV_SHOW_ID
AND S.NAAM = 'South Park'
FULL OUTER JOIN TVSHOW_REACTIES_KOPPELTABEL TRK
ON TRK.TV_SHOW_ID = S.TV_SHOW_ID
GROUP BY TRK.TV_SHOW_ID,
S.UITGEVER,
S.NAAM,
S.BESCHRIJVING,
S.AFBEELDING_BESTANDSPAD,
S.SUBTITLE,
C.NAAM;
I can't figure out why I can't get only the first row back.
Now I get three rows back with null values and I want only to get the first row. Please help!
rows without nulls use the following FROM statement and keep the rest of query the same.:
FROM CATEGORIEEN_KOPPELTABEL CK
JOIN CATEGORIE C ON C.CATEGORIE_ID = CK.CATEGORIE_ID
JOIN TV_SHOW S ON S.TV_SHOW_ID = CK.TV_SHOW_ID AND S.NAAM = 'South Park'
JOIN TVSHOW_REACTIES_KOPPELTABEL TRK ON TRK.TV_SHOW_ID = S.TV_SHOW_ID
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
SELECT
dbo.pi_employee.emp_firstname, dbo.pi_employee.emp_lastname,
dbo.pi_employee.emp_no, dbo.pi_employee.emp_cnic,
dbo.pi_employee.emp_currentadd, dbo.pi_employee.emp_cellph,
dbo.pi_employee.emp_birthday, pi_jobtitle_1.jobtitle_name,
dbo.pi_employee.emp_joindate, dbo.pi_education.edu_degree,
dbo.pi_education.edu_year, dbo.pi_employee.emp_pension,
dbo.pi_employee.emp_age, dbo.pi_employee.emp_service,
dbo.pi_employee.emp_terminate, dbo.pi_employee.emp_termdate,
dbo.pi_employee.emp_basicofpay, dbo.pi_employee.emp_terminationreason,
dbo.pi_employee.emp_terminationdate, dbo.pi_employee.emp_status,
dbo.pi_employee.emp_gender, dbo.pi_employee.emp_maritalstatus,
dbo.pi_employee.emp_paymethod, dbo.pi_employee.emp_leaveentitle,
dbo.pi_employee.emp_confirmation, dbo.pi_employee.emp_title,
dbo.pi_employee.emp_basicamount, dbo.pi_salgrade.salgrade_name,
dbo.tbl_emp_status.StatusName, dbo.pi_skills.skill_type,
dbo.pi_location.loc_name, pi_location_1.loc_name AS wcity,
dbo.pi_jobtitlehist.jthSaleGradetype, dbo.pi_workexp.exp_serperiod,
dbo.pi_employee.emp_domicile, dbo.pi_skills.skill_type AS Skill,
dbo.pi_skills.skill_exp, dbo.pi_education.edu_degree AS Degree,
dbo.pi_education.edu_uni, dbo.pi_education.edu_distinction,
dbo.pi_lochistory.lhstart_date, dbo.pi_lochistory.lhend_date
FROM
dbo.pi_location
RIGHT OUTER JOIN
dbo.pi_workexp
RIGHT OUTER JOIN
dbo.pi_employee ON dbo.pi_workexp.emp_no = dbo.pi_employee.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitlehist ON dbo.pi_employee.emp_no = dbo.pi_jobtitlehist.emp_no ON
dbo.pi_location.loc_id = dbo.pi_employee.emp_location_id
LEFT OUTER JOIN
dbo.pi_salgrade ON dbo.pi_employee.emp_salgrade_id = dbo.pi_salgrade.salgrade_id
LEFT OUTER JOIN
dbo.tbl_emp_status ON dbo.pi_employee.emp_status = dbo.tbl_emp_status.StatusID
LEFT OUTER JOIN
dbo.pi_skills ON dbo.pi_employee.emp_no = dbo.pi_skills.emp_no
LEFT OUTER JOIN
dbo.pi_location AS pi_location_1
INNER JOIN
dbo.pi_lochistory ON pi_location_1.loc_id = dbo.pi_lochistory.loc_id ON dbo.pi_employee.emp_no = dbo.pi_lochistory.emp_no
LEFT OUTER JOIN
dbo.pi_education ON dbo.pi_employee.emp_no = dbo.pi_education.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitle AS pi_jobtitle_1 ON dbo.pi_employee.emp_jobtitle_id = pi_jobtitle_1.jobtitle_id
I am writing sql query to implement different scenario, but problem is that it gives repeated values. I write distinct and order by too but result was same can any one help me to solve this issue.
EDIT – The same query with table names aliased:
SELECT
em.emp_firstname, em.emp_lastname,
em.emp_no, em.emp_cnic,
em.emp_currentadd, em.emp_cellph,
em.emp_birthday, jt.jobtitle_name,
em.emp_joindate, ed.edu_degree,
ed.edu_year, em.emp_pension,
em.emp_age, em.emp_service,
em.emp_terminate, em.emp_termdate,
em.emp_basicofpay, em.emp_terminationreason,
em.emp_terminationdate, em.emp_status,
em.emp_gender, em.emp_maritalstatus,
em.emp_paymethod, em.emp_leaveentitle,
em.emp_confirmation, em.emp_title,
em.emp_basicamount, sg.salgrade_name,
es.StatusName, s.skill_type,
L.loc_name, L1.loc_name AS wcity,
jh.jthSaleGradetype, we.exp_serperiod,
em.emp_domicile, s.skill_type AS Skill,
s.skill_exp, ed.edu_degree AS Degree,
ed.edu_uni, ed.edu_distinction,
Lh.lhstart_date, Lh.lhend_date
FROM
dbo.pi_location AS L
RIGHT OUTER JOIN
dbo.pi_workexp AS we
RIGHT OUTER JOIN
dbo.pi_employee AS em ON we.emp_no = em.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitlehist jh ON em.emp_no = jh.emp_no ON L.loc_id = em.emp_location_id
LEFT OUTER JOIN
dbo.pi_salgrade AS sg ON em.emp_salgrade_id = sg.salgrade_id
LEFT OUTER JOIN
dbo.tbl_emp_status AS es ON em.emp_status = es.StatusID
LEFT OUTER JOIN
dbo.pi_skills AS s ON em.emp_no = s.emp_no
LEFT OUTER JOIN
dbo.pi_location AS L1
INNER JOIN
dbo.pi_lochistory AS Lh ON L1.loc_id = Lh.loc_id ON em.emp_no = Lh.emp_no
LEFT OUTER JOIN
dbo.pi_education ed ON em.emp_no = ed.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitle AS j ON em.emp_jobtitle_id = j.jobtitle_id
Yeah, since you're doing a crap ton of Cartesian products with those joins without on clauses, that will cause plenty of what seem like 'repeated values', but are really just the product of the two tables combining sets.