Left Outer Join Error - oracle

I get this error for the below query when I am trying to make a left outer join
ERROR at line 7:
ORA-00936: missing expression
select s.FINAL_BSAL,s.EMP_No,p.ERN_DDCT_CATNO,p.AMOUNT,n.NO_PAY_AMOUNT,
p.Pay_month,a.ARREARS_AMOUNT from salary_details s,pay_details p,Arrears a,No_Pay n
where s.emp_no=p.emp_no
and
s.SAL_NO IN (SELECT MAX(SAL_NO) FROM SALARY_DETAILS group by EMP_NO)
AND
to_char(P.PAY_MONTH,'MM-YYYY') =to_char(n.NO_PAY_MONTH,'MM-YYYY') (+)
AND
to_char(P.PAY_MONTH,'MM-YYYY')=to_char(a.ARREARS_MONTH,'MM-YYYY') ;
Please help.

The issue lies with the placement of (+):
Instead of:
to_char(P.PAY_MONTH,'MM-YYYY') =to_char(n.NO_PAY_MONTH,'MM-YYYY') (+)
you should do:
to_char(P.PAY_MONTH,'MM-YYYY') =to_char(n.NO_PAY_MONTH (+),'MM-YYYY')
However, if I were you, I'd go with #Walter_Ritzel's approach and use ANSI JOIN syntax instead. That and properly format the SQL so that it's readable...

Try this:
select s.FINAL_BSAL
,s.EMP_No
,p.ERN_DDCT_CATNO
,p.AMOUNT
,n.NO_PAY_AMOUNT
,p.Pay_month
,a.ARREARS_AMOUNT
from salary_details s inner join pay_details p on s.emp_no = p.emp_no
inner join Arrears a on to_char(P.PAY_MONTH,'MM-YYYY')= to_char(a.ARREARS_MONTH,'MM-YYYY')
left outer join No_Pay n on to_char(P.PAY_MONTH,'MM-YYYY') = to_char(n.NO_PAY_MONTH,'MM-YYYY')
where s.SAL_NO IN (SELECT MAX(SAL_NO) FROM SALARY_DETAILS group by EMP_NO);

Related

how to use full outer join writing query on multiple tables?

I have a below query
How to use full outer join for TABLE T4 for getting all records?
WHERE
(DB.T4.AUTH_REV_NO=DB.T2.AUTH_REV_NO
AND DB.T4.AUTH_NO=DB.T2.AUTH_NO)
AND (DB.T2.AUTH_CURR_IN='Y' )
AND (DB.T3.AUTH_NO=DB.T2.AUTH_NO)
AND (DB.T3.AUTH_REV_NO=DB.T2.AUTH_REV_NO )
AND (DB.T6.FNC_ID=DB.T4.FNC_ID)
AND (DB.T7.FNC_SEG_ID=DB.T6.FNC_SEG_ID)
AND (DB.T1.SCT_ID(+)=DB.T7.SCT_ID
AND DB.T1.FNC_SEG_ID(+)=DB.T7.FNC_SEG_ID)
AND (DB.T8.NDE_ID=DB.T12.NDE_ID)
AND (DB.T7.FNC_SEG_ID=DB.T8.FNC_SEG_ID)
AND (DB.T7.SCT_ID=DB.T8.SCT_ID)
AND ((DB.T12.NDE_ID=DB.T6.NDE_STRT_ID)
OR (DB.T12.NDE_ID=DB.T6.NDE_END_ID))
AND (DB.T5.FNC_ID(+)=DB.T4.FNC_ID)
AND (T13_A4.REF_ID(+)=DB.T5.REF_TONE_TYP_ID)
AND (fne.FNC_SEG_ID=DB.T8.FNC_SEG_ID)
AND (fne.NDE_ID=DB.T8.NDE_ID)
AND (fne.SCT_ID=DB.T8.SCT_ID)
AND fnode.NDE_ID=DB.T6.NDE_STRT_ID
AND tnode.NDE_ID=DB.T6.NDE_END_ID
AND (DB.T4.REF_FNC_TYP_ID=T13_A1.REF_ID)
AND (ne_port.NDE_EQP_ID=fne.NDE_EQP_ID)
AND (ne_port.NDE_EQP_PRN_ID=ne_card.NDE_EQP_ID)
AND (ne_card.NDE_EQP_PRN_ID=ne_shelf.NDE_EQP_ID)
AND (ne_shelf.NDE_EQP_PRN_ID=ne_rack.NDE_EQP_ID)
AND (eq.EQP_ID=ne_card.EQP_ID)
AND (eq.REF_EQP_CLS_ID=T13_A2.REF_ID)
AND (DB.T3.REF_AUTH_STS_ID=T13_A3.REF_ID)
AND (DB.T3.AUTH_STS_ID
IN (SELECT MAX(DB.T3.AUTH_STS_ID) FROMDB.T3
WHERE (DB.T3.AUTH_NO,DB.T3.AUTH_REV_NO)
IN
(SELECT
DB.T3.AUTH_NO,
MAX(DB.T3.AUTH_REV_NO)
FROM
DB.T3
GROUP BY
DB.T3.AUTH_NO)
GROUP BY
DB.T3.AUTH_NO))
How to use full outer join for TABLE T4 and for COLUMN FNC_TONE_LVL_QT to get all records.
Please help.
You posted a whole lot of "joins". I'm not going to rewrite it for you, but - I'd suggest you to switch to a more recent explicit JOIN syntax which makes things somewhat simpler and easier to understand as you'd separate joins from conditions. Moreover, it allows you to outer join the same table to more than just one another table, which is impossible with the old (+) Oracle's outer join operator.
Something like this
select ...
from table_1 a left join table_2 b on a.id = b.id
full outer join table_3 c on c.id = a.id
...

Oracle's OUTER JOIN (+) on string - Migration PostgreSQL

I'm migrating a client's software database from Oracle to PostgreSQL, and I have some trouble understanding a query, what it does, and consequently how to migrate it.
The query is:
SELECT *
FROM TBL1, TBL2, TBL3, TBL4
WHERE TBL3.Project_ID = TBL1.Project_ID
AND TBL2.Type_ID = TBL1.Type_ID
AND TBL4.PROPERTY_NAME(+)='Id'
AND TBL4.Entity_ID(+)=TBL1.Entity_ID
And the part I don't get, is the outer join (+) on 'Id'.
A join on a table, OK, but on a string? I've no idea of what it does.
Do someone has an idea?
Thanks.
TBL4.PROPERTY_NAME(+)='Id' means when the line was inner joined, then the value has to be 'Id', but when the line was outer joined, the condition is evaluated as true
however you should rewrite the statement to the standard as:
SELECT *
FROM TBL1
JOIN TBL2 ON TBL2.Type_ID = TBL1.Type_ID
JOIN TBL3 ON TBL3.Project_ID = TBL1.Project_ID
LEFT JOIN TBL4 ON TBL4.Entity_ID=TBL1.Entity_ID AND TBL4.PROPERTY_NAME='Id'
This is the equivalent of the following query using ANSI join syntax:
SELECT *
FROM TBL1 t1
INNER JOIN TBL2 t2 ON (t1.Type_ID = t2.Type_ID)
INNER JOIN TBL3 t3 ON (t3.Project_ID = t1.Project_ID)
LEFT JOIN TBL4 t4 ON (t4.Entity_ID = t1.Entity_ID AND t4.PROPERTY_NAME = 'Id')
You're not joining to a string, merely specifying a join condition that's based on one.

Oracle SQL Developer Error - ORA-00920: invalid relational operator

This is my code after check syntax this error appear
Error at line 15, column 6: ORA-00920: invalid relational operator
What problem on this code?
SELECT ahli.mshp_no,
branch.branch_code,
branch.branch_name,
l_mshp_type.mshp_type_desc,
ahli.name,
l_idtype.description,
ahli.ic_no,
ahli.birth_date,
ahli.addr_1,
ahli.addr_2,
ahli.addr_3,
ahli.postcode,
l_state.description,
ahli.notelru,
ahli.h_phone,
ahli.hubaddr_1,
ahli.hubaddr_2,
ahli.hubaddr_3,
ahli.hubpostcode,
ahli.emp_code,
ahli.offaddr_1,
ahli.offaddr_2,
ahli.offaddr_3,
ahli.offpostcode,
ahli.offaddr_4,
waris.nama,
waris.id_type,
waris.id_no,
waris.relation,
waris.addr_1,
waris.addr_2,
waris.addr_3,
waris.postcode,
waris.addr_4,
waris.no_tel1,
waris.no_tel2,
waris.mshp_no,
ahli.mshp_type,
ahli.branch_code,
ahli.id_type,
ahli.addr_4
FROM ahli
inner join waris
ON ahli.mshp_no = waris.mshp_no
AND inner
join branch
ON ahli.branch_code = branch.branch_code
AND inner
join l_mshp_type
ON ahli.mshp_code = l_mshp_type.mshp_type_desc
AND inner
join l_idtype
ON ahli.id_type = l_idtype.description
AND inner
join l_state
ON ahli.offaddr_4 = l_state.description
INNER JOIN WARIS ON AHLI.MSHP_NO = WARIS.MSHP_NO AND
INNER JOIN
You have an extra "AND" at the end.
Remove those, just do
INNER JOIN WARIS ON AHLI.MSHP_NO = WARIS.MSHP_NO
INNER JOIN

Invalid Identifier SQL

So i have this:
SELECT p.plantnaam,o.levcode,o.offerteprijs
FROM plant p, offerte o
JOIN (SELECT plantcode , MIN(offerteprijs) AS offprijs
FROM offerte
GROUP BY plantcode) s
ON s.plantcode = p.plantcode
AND s.offprijs = o.offerteprijs
ORDER BY p.plantnaam,l.levcode
Appearently on the 6th row, p.plantcode is suddenly magically an invalid identifier. Why is this? and why are all the others from the exact same table perfectly fine before that point?
The problem is that you are mixing JOINs. You have both implicit and explicit joins. The explicit JOIN syntax with the ON clause has a higher precedence over the implicit join with the commas. As a result the alias for the plant and the offerte tables will not be available in the ON clause. Try using the same JOIN type throughout:
SELECT p.plantnaam, o.levcode, o.offerteprijs
FROM
(
SELECT plantcode , MIN(offerteprijs) AS offprijs
FROM offerte
GROUP BY plantcode
) s
INNER JOIN plant p
ON s.plantcode = p.plantcode
INNER JOIN offerte o
ON s.offprijs = o.offerteprijs
ORDER BY p.plantnaam, l.levcode

"Missing right parenthesis" error while using Sub-Query with INNER JOIN in Oracle

I have a Oracle command as
Select offers.OFR_STAT,OFFER_DETAILS.*,PrevData.*
From offers
INNER JOIN OFFER_DETAILS
ON OFFER_DETAILS.OFFER_ID=offers.OFFER_ID
INNER JOIN (
SELECT InnerOfrDtl.*
FROM OFFER_DETAILS as InnerOfrDtl
WHERE InnerOfrDtl.offer_id=offers.offer_id
) AS PrevData
ON PrevData.SCHEDULE_TYPE=OFFER_DETAILS.SCHEDULE_TYPE
While running I got an error message as "missing right parenthesis".
Here I wanted to use a sub-query for Inner join.
Try removing the AS after the close parenthesis and before PrevData. As I understand it, Oracle does not allow the keyword AS for table aliases.
The query does not require a sub-query. You could write:
SELECT o.OFR_STAT, d.*, p.*
FROM offers o
JOIN Offer_Details d ON d.Offer_ID = o.Offer_ID
JOIN Offer_Details p
ON p.Offer_ID = o.Offer_ID
AND p.Schedule_Type = d.Schedule_Type
I'm not wholly convinced that the query makes sense, but that's a different matter altogether.
Remove the AS keyword for the sub-query, this is not supported by Oracle:
Select offers.OFR_STAT,OFFER_DETAILS.*,PrevData.*
From offers
INNER JOIN OFFER_DETAILS
ON OFFER_DETAILS.OFFER_ID=offers.OFFER_ID
INNER JOIN (
SELECT InnerOfrDtl.*
FROM OFFER_DETAILS as InnerOfrDtl
WHERE InnerOfrDtl.offer_id=offers.offer_id
) PrevData -- <<<< here is the change
ON PrevData.SCHEDULE_TYPE=OFFER_DETAILS.SCHEDULE_TYPE
Older versions of Oracle (8i for sure, probably 9i too) have trouble with explicit INNER JOINs. Replacing them with implicit (non-ANSI?) inner joins can fix the problem.
https://forums.oracle.com/forums/thread.jspa?threadID=328028

Resources