Oracle ora-00907 missing right parenthesis - sql

What's wrong with the code?
I got error ora-00907 missing right parenthesis
SELECT
(SELECT AMOUNT FROM LATERAL (SELECT cro.AMOUNT
FROM some_schema.cred cro
WHERE cro.cr_id = co.cr_id) WHERE ROWNUM = 1)
FROM some_schema.cred_oper co
ORACLE version 11.2.0.4.0

LATERAL is only available in 12c and later versions of Oracle. I'm not sure what you're attempting to do here, but it appears your query can be simplified to:
SELECT cro.AMOUNT
FROM some_schema.cred cro
INNER JOIN some_schema.cred_oper co
ON cro.cr_id = co.cr_id
WHERE ROWNUM = 1
Best of luck.

i think your query will be like below
SELECT
(
SELECT sum(cro.AMOUNT)
FROM some_schema.cred cro
WHERE cro.cr_id = co.cr_id
) as amount
FROM some_schema.cred_oper co

Related

Getting error "SQL compilation error: Unsupported subquery type cannot be evaluated"

Am new to Snowflake programming though I had much experience in Oracle DB.
When am running the below query in Snowflake am getting the error as
SQL compilation error: Unsupported subquery type cannot be evaluated
SELECT organization_id,
inventory_item_id,
revision,
effectivity_date,
revision_label,
revision_id
FROM cg1_mtl_item_revisions_b mir
WHERE effectivity_date IN
(SELECT FIRST_VALUE (ir2.effectivity_date)
OVER (ORDER BY ir2.effectivity_date DESC)
effectivity_date
FROM cg1_mtl_item_revisions_b ir2
WHERE ir2.inventory_item_id = mir.inventory_item_id
AND ir2.organization_id = mir.organization_id
AND ir2.effectivity_date <= CURRENT_DATE
AND ir2.implementation_date IS NOT NULL)
AND mir.revision IN
(SELECT FIRST_VALUE (ir3.revision)
OVER (ORDER BY ir3.revision DESC)
revision
FROM cg1_mtl_item_revisions_b ir3
WHERE ir3.inventory_item_id = mir.inventory_item_id
AND ir3.organization_id = mir.organization_id
AND ir3.implementation_date IS NOT NULL
AND ir3.effectivity_date = mir.effectivity_date);
Am I missing something here??
Can someone plz help me here.
Thanks in Advance,
Sudarshan
The Snowflake database doesn't support correlated subqueries as extensively as Oracle does.
You have to find a way to rewrite, eg. using
WITH <common table expressions ...>
SELECT ...
JOIN ...
You seem to want the latest revision from the latest effective date. Window functions are probably a better approach in any database:
SELECT mir.* -- whatever columns you want
FROM (SELECT mir.*,
ROW_NUMBER() OVER (PARTITION BY mir.inventory_item_id, mir.organization_id
ORDER BY mir.effectivity_date DESC, mir.revision DESC) as seqnum
FROM cg1_mtl_item_revisions_b mir
) mir
WHERE seqnum = 1;
I have figured out that the third condition of the join in the last sub-query is throwing this error. If we comment out the following line then the code returns data:
{ AND ir3.effectivity_date = mir.effectivity_date }
So it seems three join conditions in a sub-query are not getting supported.
We need to work out on finding an alternate piece of code to satisfy the above condition so that we get the correct result set.
Another approach may be to use lateral joins - https://docs.snowflake.com/en/sql-reference/constructs/join-lateral.html
One important thing about lateral joins is it will only return matched records.

SQL Sever query execute

select top(1)
Tb_Customer.ID
from
Tb_Customer
inner join
Tb_Agency_Eshterak on Tb_Customer.ID = Tb_Agency_Eshterak.CustomerID
where
Tb_Agency_Eshterak.TypeE = 2
order by
((convert(decimal(10), Tb_Customer.Lat) - '36.828381258846065')
(convert(decimal(10), Tb_Customer.Lat) - '36.828381258846065')) +
((convert(decimal(10), Tb_Customer.Lng) - '54.454983147717144')
(convert(decimal(10), Tb_Customer.Lng) - '54.454983147717144')) ASC
Does this query have any problems?
I Cant Excute it In Sql Server
This Is Emergency Thanks
You are missing paranthesis). You are missing operators in your ORDER BY clause. I have put multiplication operator, to make the query compile. Also, it is not recommended to have this big expression in the ORDER BY clause.
I would suggest you to have a persisted computed column and use that column in your ORDER BY clause.
select top(1) Tb_Customer.ID
from Tb_Customer
inner join Tb_Agency_Eshterak
on Tb_Customer.ID=Tb_Agency_Eshterak.CustomerID
where Tb_Agency_Eshterak.TypeE=2
ORDER BY
((CONVERT(decimal(10),Tb_Customer.Lat)-'36.828381258846065') * (CONVERT(decimal(10),Tb_Customer.Lat)-'36.828381258846065')) + ((CONVERT(decimal(10),Tb_Customer.Lng) - '54.454983147717144') * (CONVERT(decimal(10),Tb_Customer.Lng) - '54.4549831477171'))

Migrate query with START WITH and CONNECT BY PRIOR from oracle to postgresql

I am migrating a process from oracle to postgresql, and I am in another problem with the conversion of them.
I have been researching how to migrate an oracle query, which has "START WITH" and "CONNECT BY PRIOR", I have documented with respect to this, and I think the easiest way to do it is with "WITH RECURSIVE"
Make the migration of the query, but I'm not sure about the results they throw since the bd oracle and postgres are different, and it is not possible to homologate the bd.
This is the query in Oracle
SELECT edef_codigo, etdf_transac, edef_detail--, LEVEL
FROM edeft
WHERE edef_distrib in('OM', 'N/A')
AND pers_codigo_socadm = 311745439
AND ctac_correlativo = 7513
START WITH etdf_transac = 'SDN'
CONNECT BY PRIOR edef_codigo = edef_padre;
And this is the query in postgresql
WITH RECURSIVE edf AS ( SELECT ed.edef_codigo, ed.etdf_transac,
ed.edef_detail
FROM edeft ed
WHERE ed.edef_distrib in('OM', 'N/A')
AND ed.pers_codigo_socadm = 311745439
AND ed.ctac_correlativo = 7513
AND ed.etdf_transac = 'SDN'
UNION ALL
SELECT ed.edef_codigo, ed.etdf_transac,
ed.edef_detail
FROM edeft ed
JOIN edf ON edf.edef_codigo = ed.edef_padre
WHERE ed.edef_distrib in('OM', 'N/A')
AND ed.pers_codigo_socadm = 311745439
AND ed.ctac_correlativo = 7513
)
SELECT * FROM edf;
I am still new to postgres and this consultation has made me especially complicated, since I have not found examples similar to what I have.
Yes, I have also used "Connect by prior" conversion in Postgresql using "With Recursive" queries And I find this is the right approach.
One simple example in reference to connect by prior:
Oracle:
Select name, age from user_test connect by prior user_id=parent_id start with user_id='a';
Postgres:
with recursive cte_name as
(select u1.name, u1.user_id, u1.age from user_test u1 where user_id='a'
UNION ALL select u2.name, u2.user_id, u2.age from user_test u2
join cte_name on cte_name.user_id=u2.parent_id) select name,age from cte_name;

ORA-00918: column ambigously defined, using DB Link

When I execute the query below I get the following error message :
ORA-00918: column ambigously defined
ORA-02063: preceding line from ABC
Query:
SELECT
dos.*,
cmd.*,
cmd_r.*,
adr_inc.*,
adr_veh.*,
loc.*,
fou_d.*,
fou_r.*, --Works if I comment this line
mot.*
FROM
DOSSIERS#ABC dos
LEFT JOIN CMDS#ABC cmd ON cmd.DOS_CODE_ID = dos.dos_code_id
LEFT JOIN CMDS_RECCSTR#ABC cmd_r ON cmd_r.DOS_CODE_ID = dos.DOS_CODE_ID AND cmd_r.CMD_CODE_ID = cmd.CMD_CODE_ID AND cmd_r.CMD_DT_CREAT = cmd.CMD_DT_CREAT
LEFT JOIN HISTO_ADR#ABC adr_inc ON adr_inc.DOS_CODE_ID = dos.DOS_CODE_ID
LEFT JOIN HISTO_ADR#ABC adr_veh ON adr_veh.DOS_CODE_ID = dos.DOS_CODE_ID
LEFT JOIN LOC#ABC loc ON dos.DOS_CODE_ID = loc.DOS_CODE_ID
LEFT JOIN FOURNISS#ABC fou_d ON fou_d.PAY_CODE_ID = loc.PAY_CODE_ID_D AND fou_d.FOU_CODE_ID = loc.FOU_CODE_ID_D
LEFT JOIN FOURNISS#ABC fou_r ON fou_r.PAY_CODE_ID = loc.PAY_CODE_ID_R AND fou_r.FOU_CODE_ID = loc.FOU_CODE_ID_R
LEFT JOIN REF_MOT#ABC mot ON mot.RMR_CODE_ID = cmd_r.RMR_CODE_ID
WHERE
dos.REF_EXT = 'XXXXXXX'
If I comment fou_r.* in SELECT it works.
The following queries don't work neither:
SELECT *
FROM ... ;
SELECT (SELECT count(xxx) FROM ...)
FROM ...;
I looked at similar issues on SO but they were all using complex queries or was using many SELECT inside WHERE. Mine is simple that is why I don't understand what could be wrong.
Current Database: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
Target Database (refers to db link ABC target): Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
Client: Toad for Oracle 9.7.2.5
You seem to be hitting bug 13589271. I can't share details from MOS, but there isn't much to share anyway. It's related to the remote table having a column with a 30-character name though, as you have in your remote FOURNIUSS table.
Unfortunately simply aliasing the column in your query, like this:
fou_d.COLUMN_WITH_30_CHARACTERS_NAME alias_a,
fou_r.COLUMN_WITH_30_CHARACTERS_NAME alias_b,
... doesn't help and still gets the same error, as the alias is applied by the local database and the problem seems to be during the remote access. What does seem to work is using an in-line view to apply a column alias before the join:
...
LEFT JOIN LOC#ABC loc ON dos.DOS_CODE_ID = loc.DOS_CODE_ID
LEFT JOIN (
SELECT PAY_CODE_ID, FOU_CODE_ID, COLUMN_WITH_30_CHARACTERS_NAME alias_a FROM FOURNISS#ABC
) fou_d ON fou_d.PAY_CODE_ID = loc.PAY_CODE_ID_D AND fou_d.FOU_CODE_ID = loc.FOU_CODE_ID_D
LEFT JOIN (
SELECT PAY_CODE_ID, FOU_CODE_ID, COLUMN_WITH_30_CHARACTERS_NAME alias_b FROM FOURNISS#ABC
) fou_r ON fou_r.PAY_CODE_ID = loc.PAY_CODE_ID_R AND fou_r.FOU_CODE_ID = loc.FOU_CODE_ID_R
LEFT JOIN REF_MOT#ABC mot ON mot.RMR_CODE_ID = cmd_r.RMR_CODE_ID
...
This even works if you give the column the same alias in both inline views. The downside is that you have to explicitly list all of the columns from the table (or at least those you're interested in) in order to be able to apply the alias to the problematic one, but having done so you can still use fou_d.* and fou_r.* in the outer select list.
I don't have an 11.2.0.2 database but I've run this successfully in an 11.2.0.3 database which still showed the ORA-00918 error from your original code. It's possible something else in 11.2.0.2 will stop this workaround being effective, of course. I don't see the original problem in 11.2.0.4 at all, so upgrading to that terminal patch release might be a better long-term solution.
Using * is generally considered a bad practice anyway though, not least because you're going to get a lot of duplicated columns from the joins (lots of dos_code_id in each row, for example); but you're also likely to be getting other data you don't really want, and anything that consumes this result set will have to assume the column order is always the same in those tables - any variation, or later addition or removal of a column, will cause problems.

join and group by query works in firebird 2.1 but not in 1.5

I have the following query that works fine in firebird 2.1, however I cannot get it to work on a db with the exact same structure in 1.5
select c.printchecknumber, v.voidamount
from checks c
join (select checknumber, sum(voidamount) as voidamount
from checkitem
where voidtype =1
group by checknumber) v on c.checknumber = v.checknumber
order by c.printchecknumber
Any ideas?
The error message is
Invalid token
Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, char 61
the error is at the start of the second select
I'm guessing that Firebird 1.5 doesn't support subqueries in the from clause. In any case, you can write this as a simpler query. The following should do what you want:
select c.printchecknumber, sum(voidamount) as voidamount
from checkitem ci join
checks c
on ci.checknumber = c.checknumber
where ci.voidtype =1
group by c.printchecknumber;
EDIT:
If you want to include checkid, then this might work:
select c.printchecknumber, c.checkid, sum(voidamount) as voidamount
from checkitem ci join
checks c
on ci.checknumber = c.checknumber
where ci.voidtype =1
group by c.printchecknumber, c.checkid;
I don't know why it does not work on an earlier system and I'd need to know the error message to help, but the following would work on any sql which supports the over() clause which I believe firebird does with 3.0
select c.printchecknumber,
sum(v.voidamount) over (partition by printchecknumber)
from checks c
join checkitem v on c.checknumber = v.checknumber and v.voidtype = 1