I am new to oracle sql, this code would have worked in tsql, but does not seem to work in oracle sql. I get a
ORA-00918: column ambiguously defined
. I cant spot where the issue is, can you help ?
the problem seem to be in the join, because when i replace the join with a simple where clause, the error goes away
WITH cte1 AS
(select t1.CONTAINER_NUMBER as t1_CONTAINER_NUMBER,
t1.LAST_UPDATE_TIME as t1_LAST_UPDATE_TIME,
t1.TRANSACTION_TIME as t1_TRANSACTION_TIME,
t1.EVENT,
t1.LINE_OPERATOR,
t1.BOOKING_NUMBER,
t1.ISO_SIZE_TYPE,
t1.GROSS_WEIGHT,
t1.CATEGORY,
t1.STATUS,
t1.POS_MODE,
t1.ARR_MODE,
t1.ARR_CARRIER,
t1.ARR_VOYAGE,
t1.DEP_CARRIER,
t1.DEP_MODE,
t1.DEP_CARRIER,
t1.LOAD_PORT,
t1.DESTINATION_PORT,
t1.TRUCKER_LICENSE,
t1.FROM_RAILCAR_NAME,
t1.TO_RAILCAR_NAME,
t1.YARDARRIVAL_TIME,
t1.YARDDEPARTURE_TIME,
t1.CUSTOMER_REFERENCE,
t1.CONTAINER_LENGTH,
t1.CONTAINER_WIDTH,
t1.CONTAINER_HEIGHT,
t1.TERMINAL_ARRIVAL_TIME,
t1.TERMINAL_DEPARTURE_TIME
from CONTAINER_HISTORY_MVW t1
where t1.CONTAINER_NUMBER = 'BEAU2105501'
and t1.EVENT is not null),
cte2 AS
(select t2.CONTAINER_NUMBER as t2_CONTAINER_NUMBER,
t2.LAST_UPDATE_TIME as t2_LAST_UPDATE_TIME,
t2.TRANSACTION_TIME as t2_TRANSACTION_TIME,
t2.CREATION_TIME,
t2.EQUIPMENT_MOVE_TYPE,
t2.FROM_EQUIPMENT_NUMBER
from CONTAINER_HISTORY_MVW t2
where t2.CONTAINER_NUMBER = 'BEAU2105501'
and t2.EVENT is null)
SELECT cte1.t1_TRANSACTION_TIME,
cte1.t1_CONTAINER_NUMBER,
cte1.t1_LAST_UPDATE_TIME,
cte1.EVENT,
cte1.BOOKING_NUMBER,
cte2.t2_TRANSACTION_TIME,
cte2.CREATION_TIME,
cte2.EQUIPMENT_MOVE_TYPE,
cte2.FROM_EQUIPMENT_NUMBER
from cte1
inner join cte2
on cte1.t1_CONTAINER_NUMBER = cte2.t2_CONTAINER_NUMBER
and cte1.t1_LAST_UPDATE_TIME = cte2.t2_LAST_UPDATE_TIME
order by cte1.t1_TRANSACTION_TIME, cte2.t2_TRANSACTION_TIME
Most likely a cut and paste error but only select a column once or you have to alias the second to distinguish it from the first.
t1.DEP_CARRIER,
t1.DEP_MODE,
t1.DEP_CARRIER, -- duplicate column name in cte1
I would say it's because your columns you have put "as .."
try to format the column outside the WITH query.
Related
I've the following subquery in an sql query:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, NOMBRE AS NOMBREUNIDAD FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY ID_PLAN, ID_CURSO, NEDICION) ASIS
Problem I have I believe lies in that both table ALUMNOS and UNIDADES have a column named 'NOMBRE' so if I attempt to execute the query I obtain:
00000 - "column ambiguously defined"
To avoid that I thought about changing NOMBRE AS NOMBREUNIDAD to:
UNIDADES.NOMBRE AS NOMBREUNIDAD
But if I do that I get a:
00000 - "not a GROUP BY expression"
So, I don't know what to do so that subquery executes properly.
What should I change to properly execute query without changing the column name?
Aliases are pretty useful, if you use them. The simplify queries and make them easier to read and maintain. I'd suggest you to do so, as it'll also help query to work because Oracle doesn't know which table you actually meant when you selected those 4 columns - which tables do they belong to?
This is just a guess as I don't know your tables so you'll have to fix it yourself. Also, I literally JOINed tables; try to avoid comma-separating them in FROM clause and doing join in WHERE clause as it is supposed to filter data.
GROUP BY, as already commented, is probably useless. If you wanted to fetch distinct set of values, then use appropriate keyword: distinct.
SELECT DISTINCT n.id_plan,
s.id_curso,
u.nedicion,
u.nombre
FROM asisten n
JOIN alumnos s ON n.cod = s.cod
JOIN unidades u
ON u.idestructura = s.idestructura
AND u.cdundorg = s.cdundorg
WHERE UPPER (TRANSLATE (u.nombre, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
I managed to solve my problem:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, UNIDADES.NOMBRE AS NOMBREUNIDAD
FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY UNIDADES.NOMBRE,ID_PLAN, ID_CURSO, NEDICION
)
I don't know what is wrong with this syntax.
SELECT Count (id_conv) AS NUM_CAMP
FROM csd_mx_mae_camp_dro
WHERE id_conv = (SELECT id_conv
FROM csd_mx_mae_conv_dro
WHERE num_cta = 60385300500)
AND id_cncpt = (SELECT A.id_cncpt
FROM csd_mx_mae_camp_dro A
INNER JOIN csd_mx_mae_cncpt_dro B
ON A.id_cncpt = B.id_cncpt
WHERE ( ( flg_tipo_camp = 'A'
AND txt_nombr_clase_logic IS NOT NULL )
OR ( flg_tipo_camp = 'C' ) )
AND txt_nom NOT IN ( 'Concepto' )
AND B.txt_cve = '84'
AND A.id_conv = (SELECT id_conv
FROM csd_mx_mae_conv_dro
WHERE num_cta = 60385300500)
AND rownum = 1
ORDER BY id_cmp)
AND flg_tipo_camp = 'A';
The expected result is 4, taking into account my records in the DB, however I have the error mentioned in the title (ORA-00907: missing the right parenthesis
00907. 00000 - "missing right parenthesis"
* Cause:
* Action:
Error in the line: 171, column: 90).
There are some subqueries where ORDER BY makes sense - and it is allowed by the syntax.
However, you use ORDER BY in a scalar subquery - one that is required to return a single value (one row / one column), and such subqueries do not allow ORDER BY.
You are using it incorrectly anyway (most likely) - you limit the number of rows to 1 by the condition ROWNUM = 1, which in conjunction with your ORDER BY probably means you wanted to order by ID_CMP and then take the first row from the result. That is not how it works; ORDER BY comes only after ROWNUM is assigned anyway. If that's what you were trying to do, remove ORDER BY as well as the condition on ROWNUM, and instead select MIN(ID_CMP) in the SELECT clause of the scalar subquery.
The specific error about the missing right parenthesis is caused by the ORDER BY clause: at that point, in a scalar subquery, the parser expects the closing parenthesis for the subquery, not any other token/clause/whatever.
I'm trying to execute the following query in SQL Server, but it's throwing an error. How can I fix it?
select
T.T_Email
from
Stu_Question S, Tutor_Answer T
where
S.S_Quest_Id = '4f7a1518-a765-40c0-ae53-3ee61eef6673'
and S.S_Quest_Id = T.S_Quest_Id
and (T_Email,T_Answer_Update_Status)
IN (T_Email, Select MAX(T_Answer_Update_Status)
from Tutor_Answer
where S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673'
group by T_Email)
and S.S_Quest_Update_Status = (Select MAX(S_Quest_Update_Status)
from Stu_Question
where S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673')
This is the offending part of your statement:
and (T_Email,T_Answer_Update_Status)
IN (T_Email, Select MAX(T_Answer_Update_Status)
from Tutor_Answer
where S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673'
group by T_Email)
What on earth are you trying to do here???
T-SQL's IN operator works on one column at a time - like this:
WHERE T_EMail IN (SELECT EMail FROM .....)
marc_s correctly pointed out the offending part of your query.
You'll have to try to convert that to a join instead. Here is how I would do it:
select T.T_Email
from Stu_Question S
join (select T_Email,
row_number() over (partition by S_Quest_Id order by S_Quest_Update_Status desc) as rn
from Tutor_Answer) T
on T.S_Quest_Id=S.S_Quest_Id
and T.rn = 1
where S.S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673'
AND S.S_Quest_Update_Status=(Select MAX(S_Quest_Update_Status)
from Stu_Question
where S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673')
Notice that you can definitely improve this further. But it should get you going.
I have the following sql syntax that I used in my database query (SQL Server)
SELECT Nieuwsbrief.ID
, Nieuwsbrief.Titel
, Nieuwsbrief.Brief
, Nieuwsbrief.NieuwsbriefTypeCode
, (SELECT COUNT(*) AS Expr1
FROM NieuwsbriefCommentaar
WHERE (Nieuwsbrief.ID = NieuwsbriefCommentaar.NieuwsbriefID
AND NieuwsbriefCommentaar.Goedgekeurd = 1)) AS AantalCommentaren
FROM Nieuwsbrief
I'm changing now to sql-server-ce (compact edition) which won't allow me to have subqueries like this. Proposed solution : inner join. But as I only need a count of the subtable 'NieuwsbriefCommentaar', I have to use a 'group by' clause on my base table attributes to avoid doubles in the result set.
However the 'Nieuwbrief.Brief' attribute is of datatype 'text'. Group by clauses are not allowed on 'text' datatype in sql-server-ce. 'Text' datatype is deprecated, but sql-server-ce doesn't support 'nvarchar(max)' yet...
Any idea how to solve this? Thx for your help.
I think that the solution could be easier. I don't know exactly how is your metadata but I think that this code could fit your requirements by simply using LEFT JOIN.
SELECT Nieuwsbrief.ID
, Nieuwsbrief.Titel
, Nieuwsbrief.Brief
, Nieuwsbrief.NieuwsbriefTypeCode
, COUNT(NieuwsbriefCommentaar.NieuwsbriefID) AS AantalCommentaren
FROM Nieuwsbrief
LEFT JOIN NieuwsbriefCommentaar ON (Nieuwsbrief.ID = NieuwsbriefCommentaar.NieuwsbriefID)
WHERE NieuwsbriefCommentaar.Goedgekeurd = 1
Edited: 2ndOption
SELECT N.ID, N.Titel, N.Brief, N.NieuwsbriefTypeCode, G.AantalCommentaren FROM Nieuwsbrief as N LEFT JOIN (SELECT NieuwsbriefID, COUNT(*) AS AantalCommentaren FROM NieuwsbriefCommentaar GROUP BY NieuwsbriefID) AS G ON (N.ID = G.NieuwsbriefID)
Please, let me know if this code works in order to find out another workaround..
regards,
I'm getting a syntax error on this query, but I can't figure it out.
Incorrect syntax near the keyword
'group'.
I believe its on the last group by, but I don't see whats wrong. Can anyone suggest how to correct this?
UPDATE [NCLGS].[dbo].[CP_CustomerShipTo]
SET TimesUsed = TimesUsed + B.NewCount
from [NCLGS].[dbo].[CP_CustomerShipTo] CST
INNER JOIN (
Select
PKH.CompanyCode,
PKH.CompanyName,
PKH.Addr1,
PKH.Addr2,
PKH.City,
PKH.State,
PKH.Zip,
Count(recid) As NewCount
from avanti_packingslipheader PKH
where pksdate > dbo.ufn_StartOfDay(DATEADD(d, -1, GETDATE() ) )
group by
PKH.CompanyCode,
PKH.CompanyName,
PKH.Addr1,
PKH.Addr2,
PKH.City,
PKH.State,
PKH.Zip
) B
ON CST.CustomerCode = B.CompanyCode
AND CST.ShipToName = B.CompanyName
AND CST.ShipToAddress1 = B.Addr1
AND CST.City = B.City
AND CST.PostalCode = B.Zip
group by
PKH.CompanyCode,
PKH.CompanyName,
PKH.Addr1,
PKH.Addr2,
PKH.City,
PKH.State,
PKH.Zip
BACKGROUND - I'm trying to do an update statement with a Count(), but of course you can't use agg. functions in an update set statement, so I'm trying to use a subquery.
You have already got GROUP BY inside the subselect, so what does the outer GROUP BY stand for?
You can't reference an alias in a subselect from an outer GROUP BY. But in any event you can't use GROUP BY with an UPDATE statement, and that's what the error message is about.
Try removing the last Group By. What exactly are you hoping this last group by will do?
Change the code to this:
update mytable set
mycolumn = mycolumn + (select x from ...);