Missing right parenthesis error using order by on a subquery - sql

I have this query:
select exp.X,
exp.Y,
exp.Z,
expe.A
from table_exp exp
inner join table_expe expe on exp.B = expe.B
where expe.X =
(select X
from table_expe
where A = expe.A
order by C desc limit 1)
And i have this error: ORA-00907: missing right parenthesis.
I receive the error when I try to add "order by C desc limit 1" , if I don't use the order by, it works, but I need it with the order by. Anyone can helps me? Thanks!

Oracle does not support limit. You can use:
where expe.X = (select e2.X
from table_expe e2
where e2.A = expe.A
order by e2.C desc
fetch first 1 row only
)
Here is a db<>fiddle showing that the syntax does work.

Related

Query fail Oracle but working in SQL Server

I have a query that runs perfectly n SQL server but in Oracle, I get the following error:
ORA-00923: FROM keyword not found where expected
SELECT
MyApps.AppConfigurationId,
MyApps.GUID,
MyApps.AppName,
MyApps.BinaryData,
MyApps.Color1,
MyApps.UserCreatorName,
MyApps.CreatedAt,
MyApps.UserUpdaterName,
MyApps.UpdatedAt,
MyApps.UserPublisherName,
MyApps.LastPublishedAt,
MyApps.AppConfigStateId,
MyApps.AppConfigStateLabel
FROM
(
SELECT
ROW_NUMBER() OVER (
PARTITION BY "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID"
ORDER BY LastestPublishInfo."PUBLISHEDAT" DESC
) AS RowNum,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID" AS AppConfigurationId,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."GUID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."NAME" AS AppName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"."BINARYDATA",
"OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"."COLOR1",
UserCreator."NAME" AS UserCreatorName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."CREATEDAT",
UserUpdater."NAME" AS UserUpdaterName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."UPDATEDAT",
LastestPublishInfo.PublisherName AS UserPublisherName ,
LastestPublishInfo."PUBLISHEDAT" AS LastPublishedAt,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."APPCONFIGSTATEID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"."LABEL" AS AppConfigStateLabel
FROM "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266" JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."APPCONFIGSTATEID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"."ID" AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."PARENTAPPCONFIGID" IS NULL AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ISACTIVE" = 1
)
JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserCreator
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."CREATEDBY" = UserCreator."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserUpdater
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."UPDATEDBY" = UserCreator."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"
ON( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."COLORPALETTEID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"."ID")
LEFT JOIN
(
SELECT "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."APPCONFIGURATIONID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDAT",
UserPublisher."NAME" AS PublisherName
FROM "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16" LEFT JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserPublisher
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDBY" = UserPublisher."ID")
WHERE "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDAT" IS NOT NULL
AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."ISACTIVE" = 0
) LastestPublishInfo
ON (LastestPublishInfo.AppConfigurationId = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"
ON( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"."APPCONFIGURATIONID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID" )
) MyApps
WHERE MyApps.RowNum = 1
ORDER BY
MyApps.UpdatedAt DESC
I already tried to check syntax follow some articles but I'm not able to solve it.
Added " around de the aliases, tried to replace attribute names in the subquery not to match the parent but nothing worked.
Hope you could help me with this since I went to the point I have no clue about what to try next!
Cheers
Using the following web: https://rextester.com/l/oracle_online_compiler
I tryed your SQL and got the same error as you.
I then started simplifing it up to:
SELECT 1 AS RowNum
FROM blah
and still getting the same error.
I think RowNum is a reserved word in Oracle, you can not use it as a field alias.

Oracle Nested Select doesn't allow order by clause

I have a requirement to fetch latest record from LEAD_REASSIGNMENT_TABLE LRD based on KSA.LEAD_ID = LRD.LEAD_ID.
Query is as follows ,
SELECT
KSA.LEAD_ID AS "leadId",
KSA.CREATED_ON AS "createdOn",
KSA.FIRST_NAME AS "firstName",
KSA.LAST_NAME AS "lastName",
KSA.CRN AS "crn" ,
KSA.MOBILE_NO AS "mobileNumber",
KSA.BRANCH_CODE AS "lpdBranchCode",
( SELECT T.DESCRIPTION FROM TERRITORY T WHERE T.ID IN
(
SELECT STM.TERRITORY_ID FROM SPOKE_TERRITORY_MAPPING STM
WHERE STM.SPOKE_CODE IN (SELECT SM.ID FROM SPOKE_MASTER SM WHERE SM.SPOKE_CODE=KSA.BRANCH_CODE AND SM.DELETE_FLAG='F') AND STM.DELETE_FLAG='F'
) AND T.DELETE_FLAG='F' AND ROWNUM<=1
) AS "lpdSubLocation" ,
(select lrd.LEAD_NEW_ASSIGNED_USER from LEAD_REASSIGNMENT_DETAILS lrd
where lrd.LEAD_ID = KSA.LEAD_ID and rownum <=1
) AS "NEW_ASSIGNED_USER"
FROM KSA_LEAD_DATA KSA
LEFT JOIN LEAD_PROSPECT_DETAILS LPD
ON KSA.LEAD_ID = LPD.KSA_LEAD_ID
LEFT JOIN ENTITY_TYPE_MAPPING ETM
ON LPD.LOAN_STATUS = ETM.ENTITY_CODE
WHERE KSA.DELETE_FLAG IS NOT NULL
ORDER BY KSA.LEAD_ID desc ;
Now when in the select i do
(select lrd.LEAD_NEW_ASSIGNED_USER from LEAD_REASSIGNMENT_DETAILS lrd
where lrd.LEAD_ID = KSA.LEAD_ID and rownum <=1
ORDER BY LRD.MODIFIED_ON) AS "NEW_ASSIGNED_USER"
I get ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis" Error
But when i use select without order by clause it works fine
(select lrd.LEAD_NEW_ASSIGNED_USER from LEAD_REASSIGNMENT_DETAILS lrd
where lrd.LEAD_ID = KSA.LEAD_ID and rownum <=1
) AS "NEW_ASSIGNED_USER"
If i use select without order by clause it wont guarantee me that i ll fetch the latest record from LEAD_REASSIGNMENT_TABLE.
NOTE
LEAD_REASSIGNMENT_TABLE contains 3-4 records with same lead_id
so i am selecting the latest record based on modified_on timestamp.
How should i use Order by clause inside nested select.?
I don't like correlated subqueries, not within the SELECT statement.
Anyway: if you rewrite that piece of code as below, I guess it should work.
(SELECT lrd.LEAD_NEW_ASSIGNED_USER
FROM LEAD_REASSIGNMENT_DETAILS lrd
WHERE lrd.LEAD_ID = KSA.LEAD_ID
AND lrd.modified_on = (SELECT MAX (lrd1.modified_on)
FROM LEAD_REASSIGNMENT_DETAILS lrd1
WHERE lrd1.lead_id = lrd.lead_id)) AS "NEW_ASSIGNED_USER"

Using Aliased column in a join

I have an aliased colum:
FIRST_VALUE(SUBSTR(ba.CREATED,1,18))
OVER (PARTITION BY bsh.STRUCTURE_ELEMENT_ID, bal.BUDGET_CYCLE_ID
ORDER BY ba.CREATED DESC NULLS FIRST) AS UPDATED_DATE
I need to use this new field in a join, how can I do it? I've tried copying the same syntax but I get an error message stating window functions are not allowed here and also tried using the UPDATED_DATE alias name but that says the field does not exist.
Can anyone advise please?
Edited 24/10/15:
I've tried the suggestions I've been given but they don't seem to be working. I'm not sure if it's because it's already a complex statement with other joins in so this is the full code as it currently is
SELECT /* State Change 2 to 3 */
bal.BUDGET_CYCLE_ID AS BUDGET_CYCLE_ID,
bal.STRUCTURE_ELEMENT_ID AS COST_CENTRE,
FIRST_VALUE(SUBSTR(ba.CREATED,1,15))
OVER (PARTITION BY bsh.STRUCTURE_ELEMENT_ID,
bal.BUDGET_CYCLE_ID,
bsh.PREVIOUS_STATE || bsh.NEW_STATE
ORDER BY ba.CREATED DESC NULLS FIRST) AS UPDATED_DATE,
bsh.PREVIOUS_STATE AS PREVIOUS_STATE,
bsh.NEW_STATE AS NEW_STATE,
ba.USER_ID AS USER_ID
FROM BUDGET_ACTIVITY ba
LEFT JOIN BUDGET_ACTIVITY_LINK bal
ON ba.BUDGET_ACTIVITY_ID = bal.BUDGET_ACTIVITY_ID
AND ba.ACTIVITY_TYPE = 5
LEFT JOIN BUDGET_STATE_HISTORY bsh
ON bal.STRUCTURE_ELEMENT_ID = bsh.STRUCTURE_ELEMENT_ID
AND bal.BUDGET_CYCLE_ID = bsh.BUDGET_CYCLE_ID
AND SUBSTR(ba.CREATED,1,15) = SUBSTR(bsh.CHANGED_TIME,1,15)
WHERE PREVIOUS_STATE || NEW_STATE = 23
AND bal.budget_cycle_ID = '227565'
AND bal.structure_element_ID = '418'
I need to change the SUBSTR(ba.created,1,15) to the UPDATED_DATE field derived above. I'm relatively new to SQL and this one is beyond me.
You'll need to put your aliased column in a subquery:
SELECT
...
FROM
(
SELECT
...
FIRST_VALUE(SUBSTR(ba.CREATED,1,18))
OVER (PARTITION BY bsh.STRUCTURE_ELEMENT_ID, bal.BUDGET_CYCLE_ID
ORDER BY ba.CREATED DESC NULLS FIRST) AS UPDATED_DATE
FROM
...
) T1
JOIN TABLE2 T2 ON T1.UPDATED_DATE = T2.DATE_FIELD;

How can I limit the results extracted by a query in Acces?

I have this query in my Access database:
SELECT t_Campioni_CAMPIONE, t_Campioni.[DATA ARRIVO], t_Campioni.PRODUTTORE, t_Campioni.CodF, t_Fornitori.[Nome Fornitore]
FROM t_Campioni INNER JOIN t_Fornitori ON t_Campioni.CodF = t_Fornitori.CodF
WHERE (((t_Campioni.CAMPIONE)=[Forms]![m_Campioni_modifica]![CAMPIONE]))
ORDER BY t_Campioni.[DATA ARRIVO] DESC;
It works but I need it to extract only the first record (with the last date). How can I do it?
Just replace your initial SELECT by SELECT TOP 1
SELECT TOP 1 t_Campioni_CAMPIONE, t_Campioni.[DATA ARRIVO], t_Campioni.PRODUTTORE, t_Campioni.CodF, t_Fornitori.[Nome Fornitore]
FROM t_Campioni INNER JOIN t_Fornitori ON t_Campioni.CodF = t_Fornitori.CodF
WHERE (((t_Campioni.CAMPIONE)=[Forms]![m_Campioni_modifica]![CAMPIONE]))
ORDER BY t_Campioni.[DATA ARRIVO] DESC;
SELECT t_Campioni_CAMPIONE, t_Campioni.[DATA ARRIVO], t_Campioni.PRODUTTORE,
t_Campioni.CodF, t_Fornitori.[Nome Fornitore]
FROM t_Campioni INNER JOIN t_Fornitori ON t_Campioni.CodF = t_Fornitori.CodF
WHERE (((t_Campioni.CAMPIONE)=[Forms]![m_Campioni_modifica]![CAMPIONE]))
ORDER BY t_Campioni.[DATA ARRIVO] DESC LIMIT 1;
You should use TOP/LIMIT/ROWNUM depends on your RDBM.
For more info: http://www.w3schools.com/sql/sql_top.asp

Yes another - The multi-part identifier "xxx" could not be bound

Ok, here is my Query:
SELECT DISTINCT
CS.CPL_Schedule_Id AS CplScheduleId
,S.Schedule_Status_Id AS ScheduleStatusId
,S.Record_Created AS Created
FROM CPL_Schedule CS
JOIN (
SELECT TOP 1 CSL.CPL_Schedule_ID, CSL.Record_Created, CSL.Schedule_Status_Id
FROM CPL_Schedule_Audit_Log CSL
WHERE CSL.CPL_Schedule_ID = CS.CPL_Schedule_ID
ORDER BY Record_Created DESC
) S
ON S.CPL_Schedule_ID = CS.CPL_Schedule_ID
WHERE
CS.Exhibitor_Id = 1
And I am getting the error on this line WHERE CSL.CPL_Schedule_ID = CS.CPL_Schedule_ID
The multi-part identifier "CS.CPL_Schedule_ID" could not be bound.
To me it looks like this is saying that nested query does not know about CS, is that true? What am I missing here? Help is much appreciated, thank you!
It seems, you need cross apply instead of join, and don't need ON hence.
SELECT DISTINCT
CS.CPL_Schedule_Id AS CplScheduleId
,S.Schedule_Status_Id AS ScheduleStatusId
,S.Record_Created AS Created
FROM CPL_Schedule CS
CROSS APPLY (
SELECT TOP 1 CSL.CPL_Schedule_ID, CSL.Record_Created, CSL.Schedule_Status_Id
FROM CPL_Schedule_Audit_Log CSL
WHERE CSL.CPL_Schedule_ID = CS.CPL_Schedule_ID
ORDER BY Record_Created DESC
) S
WHERE
CS.Exhibitor_Id = 1