I am writing a query in Oracle(11g):
select DBTM,AVNR from E_MW_01Min_MIT m
cross apply(
select Avnr,XDatum1 from E_MW_01DAY_MEX d
where d.AVnr = m.avnr
and d.XDatum1 = m.DBTM
)
but it gives me the error
ORA-00905: missing keyword
where is the problem?
Thank you
This keywords (CROSS APPLY or OUTER APPLY) is introduced in Oracle 12c version.
You can see this link :
cross apply giving missing keyword error
cross apply is not available in that version of Oracle. Just use join instead:
select m.DBTM, d.AVNR
from E_MW_01Min_MIT m JOIN
E_MW_01DAY_MEX d
ON d.AVnr = m.avnr AND d.XDatum1 = m.DBTM ;
This is actually more easily expressed using JOIN, so I see no advantage to attempting APPLY even if the database does support it.
Related
I have the following query:
SELECT STDEV(Value) as Value, TimeOfTest as Date
FROM myTable
unpivot
(
value
for col in (WS1, WS2, WS3, WS4, WS5, WS6, WS7, WS8, WS9, WS10, WS11,
WS12, WS13, WS14, WS15, WS16, WS17, WS18, WS19, WS20)
) un
GROUP BY TimeOfTest
ORDER BY TimeOfTest DESC
but I get an SQL server 2008 error: Incorrect syntax near the keyword 'FOR'
Would anyone know the reason why? The syntax looks correct.
Just use APPLY.
SELECT t.TimeOfTest, STDEV(ws) as Value
FROM myTable t CROSS APPLY(
(VALUES (WS1), (WS2), . . . (WS20)) v(ws)
GROUP BY t.TimeOfTest
ORDER BY t.TimeOfTest DESC;
APPLY implements something called a "lateral join". This is a very powerful (and ANSI-standard) construct, that can be used for many things beyond unpivoting. On the other hand, unpivot is very specific syntax that is used for only one purpose . . . and as you have found, may not work well other constructs such as GROUP BY (I think you could resolve your issue by using a subquery).
please tell me what is the syntax problem in this query
SELECT sde
FROM TABLE_EW sde , CASE_W spr, DOCUMENT swp
JOIN swp.id, swp.YEAR ON (swp.id = sde.ID_DOCUMENT)
JOIN spr.ID, spr.STATE, spr.NUMBER ON (spr.ID_DOCUMENT = swp.ID)
WHERE sde.IDENT_TABLEEW LIKE '122337456464'
AND swp.YEAR LIKE 2015;
SQLDeleoper point problem to From Line
I think this is the query you meant to write:
SELECT sde.*,swp.id, swp.YEAR,spr.ID, spr.STATE
FROM TABLE_EW sde
JOIN DOCUMENT swp ON (swp.id = sde.ID_DOCUMENT)
JOIN CASE_W spr ON (spr.ID_DOCUMENT = swp.ID)
WHERE sde.IDENT_TABLEEW = '122337456464'
AND swp.YEAR = 2015;
As mentioned in the comments, you have A LOT of errors in your SQL code.
You use implicit and explicit joins together, AVOID the use of implicit joins syntax and use only the proper syntax like my example.
Also, only in the select you can specify the columns you want, I'm guessing what you've been trying to do is
JOIN spr.ID, spr.STATE -> wanted this columns.
You should write them in the select part.
Another problem is the join condition, you either use implicit joins, (from table,table2,table3..) and then the join condition is in the where clause or you use explicit joins and then the condition is in the ON clause. You can't use both!
Another problem is the unnecessary use of LIKE . When comparing to an exact match, use EQUALS sign.
I'm trying to execute the following query in Oracle:
SELECT c.id_cliente, c.nombre_cliente, c.apellidos_cliente
FROM cliente c
WHERE not exists (SELECT f.id_finca
FROM finca f
WHERE f.habitaciones = 3
EXCEPT
SELECT v.id_fincas
FROM visitas v
WHERE v.id_cliente = c.id_cliente)
But I am getting the error "missing right parenthesis".
The parenthesis are well-balanced, how can I solve this error?
Use MINUS instead of EXCEPT.
SELECT c.id_cliente,
c.nombre_cliente,
c.apellidos_cliente
FROM cliente c
WHERE NOT EXISTS (SELECT f.id_finca
FROM finca f
WHERE f.habitaciones = 3
MINUS
SELECT v.id_fincas
FROM visitas v
WHERE v.id_cliente = c.id_cliente)
there is no operator EXCEPT in Oracle. Use MINUS instead of it.
Additionally, your query seems weird: better try to make it with two not exists, I think it can give some performance.
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,
Today while inside a client's production system, I found a SQL Server query that contained an unfamiliar syntax. In the below example, what does the *= operator do? I could not find any mention of it on MSDN. The query does execute and return data. As far as anyone knows, this has been in the system since they were using SQL Server 2000, but they are now running 2005.
declare #nProduct int
declare #iPricingType int
declare #nMCC int
set #nProduct = 4
set #iPricingType = 2
set #nMCC = 230
--Build SQL for factor matrix
Select distinct
base.uiBase_Price_ID,
base.nNoteRate,
base.sDeliveryOpt,
IsNull(base.nPrice,0) as nPrice,
IsNull(base.nPrice,0) + Isnull(fact.nFactor,0) as nAdjPrice,
base.iProduct_ID,
fact.iPosition as fiPosition,
base.iPosition,
CONVERT(varchar(20), base.dtDate_Updated, 101) + ' ' + CONVERT(varchar(20), base.dtDate_Updated, 108) as 'dtDate_Updated',
fact.nFactor,
fact.nTreasFactor,
product.sProduct_txt ,
pfi.sPFI_Name,
mccprod.nServicing_Fee,
fact.nNoteRate as fNoteRate,
mcc.nLRA_Charge as nLRA
From
tbl_Base_Prices base, tbl_Factors fact, tbl_Product product, tbl_PFI pfi, tbl_MCC mcc, tbl_MCC_Product mccprod
Where
base.iProduct_ID = #nProduct
And base.iProduct_ID *= fact.iProduct_ID
And base.iPosition *= fact.iPosition
And base.nNoteRate *= fact.nNoteRate
And base.iPricing_Type = #iPricingType
And fact.iMCC_ID = #nMCC
And fact.iProduct_ID = #nProduct
And mcc.iMCC_ID = #nMCC
And mcc.iPFI_ID = pfi.iPFI_ID
And mccprod.iMCC_ID = #nMCC
And mccprod.iProduct_ID = #nProduct
And base.iProduct_ID = product.iProduct_ID
and fact.iPricing_Type= #iPricingType
Order By
base.nNoteRate, base.iPosition
Remove this code immediately and replace with a left join. This code does not always interpret correctly (Sometimes SQL Server decides it is a cross join) even in SQL Server 2000 and thus can give incorrect results! Also it is deprecated for the future (Using Outer Joins, SQL Server 2000 documentation archived from the original).
I'm going to add that in adjusting to left joins you should remove all of those other implicit joins as well. The implicit join syntax has been obsolete since 1992, there is no excuse for it still being in production code. And mixing implicit and explicit joins can give unexpected results.
It is a left outer join, =* is a right outer join.
E.g. the following are equal;
SELECT * FROM Table1 LEFT OUTER JOIN Table2 ON Table1.ID = Table2.FK_ID
SELECT * FROM Table1, Table2 WHERE Table1.ID *= Table2.FK_ID
The non-ANSI syntax for outer joins (*= and =*) is on the official list of deprecated features that will be removed in the next version of SQL.
The following SQL Server Database
Engine features will not be supported
in the next version of SQL Server. Do
not use these features in new
development work, and modify
applications that currently use these
features as soon as possible.
The replacement feature is the ANSI compliant syntax of JOIN.
It's a shorthand join syntax. Take a look at this thread which covers this topic.
Transact-SQL shorthand join syntax?
I believe those are "non-ANSI outer join operators". Your database compatibility level must be 80 or lower.
That's the older ANSI (ANSI-89) syntax left outer join operator. I'd recommend not using it - the ANSI syntax is more verbose and is much more readable.