Nested query issue, Invalid Identifier error - sql

I am running following query in Oracle 10 XE and getting [Err] ORA-00904: "Q": invalid identifier
SELECT questions.sb_title Q,questions.sb_baab_id,questions.sb_fasal_id,QUESTIONS.SB_CONTENT answer,IST.SB_PREFERENCE PREF
FROM USER_QUESTIONS questions
INNER JOIN USER
ON QUESTIONS.SB_USER_ID = USER.SB_ID
INNER JOIN IST
ON IST.SB_ID = USER.SB_IST_ID
AND(Q LIKE '%where is%')
AND USER.SB_IST_ID =
(
Select issued.SB_IST_ID
FROM USER_REGISTER register_number
INNER JOIN USER_ISSUED issued
ON register_number.SB_REGISTER_NUMBER = ISSUED.SB_REGISTER_NUMBER
AND REGISTER_NUMBER.SB_REGISTER_NUMBER IN(1240)
)

You cannot reference an alias in the JOIN condition - write the actual column name: sb_title.

I don't think you can use the alias "Q" like that. You're aliasing it in your select clause, don't think you can use that alias in your ON clause. Try using the full column name (sb_title).

Related

Outer join for Alias name and column name -Oracle

I had a working sample query earlier in my code as mentioned below.
SELECT DISTINCT
nombre_aplicacion,
APLICACION,
NOMBRE_APLCODE,
DESCRIPCION,
AREAFUNC
FROM (
select **CODAPLICATION nombre_aplicacion**,
APLICACION,
NOMBRE_APLCODE,
DESCRPTION,
AREAFUNC
from admin.VW_APLICACIONES#dblink,
admin.VW_PRODUCTOS#dblink
where **nombre_aplicacion (+) = CODAPLICATION**
)
WHERE 1=1
ORDER BY nombre_aplicacion ASC;
When I try similar type of query with different tables I was getting error as invalid ORA-00904: "NOMBRE_APLICACION": invalid identifier.
If I remove nombre_aplicacion (+) = CODAPLICATION in where condition query is fetching the result. Can any one suggest why I was facing error as its working earlier with sample query and I was getting error? Is this join is valid?
The query is not valid as:
In the inner sub-query you select areafunc and in the outer query you use area which does not appear in the inner sub-query so will not be available.
In the inner sub-query, you define CODAPLICATION to have the alias nombre_aplicacion and then you try to use that alias in the WHERE clause as a join condition; that will not work.
You have not described which column belongs to which table but you want something like:
SELECT DISTINCT
a.codaplication AS nombre_aplicacion,
a.aplicacion,
a.nombre_aplcode,
p.descrption,
p.areafunc
from APLICACIONES a
LEFT OUTER JOIN PRODUCTOS p
ON (a.primary_key_column = p.foreign_key_column)
ORDER BY nombre_aplicacion ASC;
Note: you are going to have to correct the code to give the correct table aliases for each column and give the correct columns for the join condition.

Oracle subquery invalid identifier

I can not figure it out what is wrong with this query:
select * from inv_srv_inst inst,
(select srvc.li_srv_cat_id from inv_li_srv_cat_srv srvc where srvc.srv_id = inst.service_id) li_srv_cat_id
where id in (37336558,37343286)
Error message:
ORA-00904: "INST"."SERVICE_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
ORA-00904: "INST"."SERVICE_ID": invalid identifier
You get this error because INV_SRV_INST is not in scope of the inline view. If you're using 12c or later you can work around this using the LATERAL keyword, which allows us to push predicates into subqueries:
select *
from inv_srv_inst inst,
lateral (select srvc.li_srv_cat_id
from inv_li_srv_cat_srv srvc
where srvc.srv_id = inst.service_id) li_srv_cat_id
where id in (37336558,37343286)
Although it must be asked, why did you choose an inline view rather than just joining the two tables?
select inst.*
,srvc.li_srv_cat_id
from inv_srv_inst inst
inner join inv_li_srv_cat_srv srvc
on srvc.srv_id = inst.service_id
where inst.id in (37336558,37343286)
Here is a demo on db<>fiddle.
Never use commas in the FROM clause.
In your case, you can express this using a lateral join or a correlated subquery -- assuming there is only one match.
select *
from inv_srv_inst inst cross join lateral
(select srvc.li_srv_cat_id
from inv_li_srv_cat_srv srvc
where srvc.srv_id = inst.service_id
) li_srv_cat_id
where inst.id in (37336558, 37343286);
Lateral joins are very handy. They are like correlated subqueries but they can return multiple columns and multiple rows.

Oracle Invalid Identifier ORA-00904

I keep getting this error trying to run this simple Join.....
SELECT docregitem.reviewdate, docregitem.nclient, client.name
FROM docregitem, client
INNER JOIN client
ON docregitem.nclient = client.nclient
ORA-00904: "DOCREGITEM"."NCLIENT": invalid identifier
I can do a select and all the columns are present and correct...
I think the query you want is:
SELECT dr.reviewdate, dr.nclient, c.name
FROM docregitem dr INNER JOIN
client c
ON dr.nclient = c.nclient;
Your from clause has a comma in it. This is a lot like a cross join, but it affects the columns. These are not known in the on clause, which is what is causing the problem.
SELECT docregitem.reviewdate, docregitem.nclient, client.name
FROM docregitem
INNER JOIN client
ON docregitem.nclient = client.nclient
you try this one as you used client table twice one with simple join and other with inner join without giving the alias name to the table so the compiler is confused in selecting and comparing column from client table.

multipart Identifier cound not be bound

I reviewed several of the post on this subject and haven't been able to see resolve this error. I have linked server that I'm trying to included in my WHERE clause in SQL 2008. I was able to successfully execute the query with out the WHERE clause. I'm sure that I overlooked something just need some advice. The message for the multipart identifier is on this line in the WHERE clause.
LAWNURSEDB.NGDemo.dbo.EXPORT_DIRECTTIME.SocialSecurityNumber
SELECT dbo.VP_PUNCHORIGIN.PERSONNUM, EXPORT_DIRECTTIME_1.SocialSecurityNumber, dbo.VP_PUNCHORIGIN.PERSONFULLNAME
FROM dbo.VP_PUNCHORIGIN INNER JOIN
LAWNURSEDB.NGDemo.dbo.EXPORT_DIRECTTIME AS EXPORT_DIRECTTIME_1 ON
dbo.VP_PUNCHORIGIN.PERSONNUM = EXPORT_DIRECTTIME_1.SocialSecurityNumber
WHERE (dbo.VP_PUNCHORIGIN.PERSONNUM NOT IN
(SELECT LAWNURSEDB.NGDemo.dbo.EXPORT_DIRECTTIME.SocialSecurityNumber AS Expr1
FROM LAWNURSEDB.NGDemo.dbo.EXPORT_DIRECTTIME AS EXPORT_DIRECTTIME_1))
You are using an alias in the WHERE clause subquery but don't reference it:
SELECT p.PERSONNUM,
EXPORT_DIRECTTIME_1.SocialSecurityNumber,
p.PERSONFULLNAME
FROM dbo.VP_PUNCHORIGIN p
INNER JOIN LAWNURSEDB.NGDemo.dbo.EXPORT_DIRECTTIME AS EXPORT_DIRECTTIME_1
ON p.PERSONNUM = EXPORT_DIRECTTIME_1.SocialSecurityNumber
WHERE p.PERSONNUM
NOT IN (SELECT EXPORT_DIRECTTIME_2.SocialSecurityNumber
FROM LAWNURSEDB.NGDemo.dbo.EXPORT_DIRECTTIME AS EXPORT_DIRECTTIME_2)

SQL error: Not Unique table/alias

I'm getting
Error Number: 1066
Not unique table/alias: 'default_event_rel_type'
my query is:
SELECT `default_users`.`username` as user,
`default_event_rel_type`.`title` as title
FROM (`default_event_rel`, `default_event_rel_type`)
JOIN `default_users` ON `default_event_rel`.`uid` = `default_users`.`id`
JOIN `default_event_rel_type` ON `default_event_rel_type`.`id` = `default_event_rel`.`eid`
Looks like you have incorrect syntax in your FROM clause. You are calling default_event_rel_type twice. And you only need to call it once in your JOIN. You don't need to have it in the FROM clause.
SELECT `default_users`.`username` as user, `default_event_rel_type`.`title` as title
FROM `default_event_rel`
JOIN `default_users`
ON `default_event_rel`.`uid` = `default_users`.`id`
JOIN `default_event_rel_type`
ON `default_event_rel_type`.`id` = `default_event_rel`.`eid`
You have the table in the From statement
FROM (default_event_rel, default_event_rel_type)
and then join to it later in the query.