Can't find ORA-00907: missing the right parenthesis - sql

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.

Related

Why do I have an 'invalid column name' when I try to sum two columns with alias that contains a space? [duplicate]

I've create 3 computed columns as alias and then used the aliased columns to calculate the total cost. This is the query:
SELECT TOP 1000 [Id]
,[QuantityOfProduct]
,[Redundant_ProductName]
,[Order_Id]
,(CASE
WHEN [PriceForUnitOverride] is NULL
THEN [Redundant_PriceForUnit]
ELSE
[PriceForUnitOverride]
END
) AS [FinalPriceForUnit]
,(CASE
WHEN [QuantityUnit_Override] is NULL
THEN [Redundant_QuantityUnit]
ELSE
[QuantityUnit_Override]
END
) AS [FinalQuantityUnit]
,(CASE
WHEN [QuantityAtomic_Override] is NULL
THEN [Redundant_QuantityAtomic]
ELSE
[QuantityAtomic_Override]
END
) AS [Final_QuantityAtomic]
--***THIS IS WHERE THE QUERY CREATES AN ERROR***--
,([QuantityOfProduct]*[FinalPriceForUnit]*
([Final_QuantityAtomic]/[FinalQuantityUnit])) AS [Final_TotalPrice]
FROM [dbo].[ItemInOrder]
WHERE [IsSoftDeleted] = 0
ORDER BY [Order_Id]
The console returns this ERROR message:
Msg 207, Level 16, State 1, Line 55
Invalid column name 'FinalPriceForUnit'.
Msg 207, Level 16, State 1, Line 55
Invalid column name 'Final_QuantityAtomic'.
Msg 207, Level 16, State 1, Line 55
Invalid column name 'FinalQuantityUnit'.
If I remove the "AS [Final_TotalPrice]" alias computed column, no error occurs, but I need the total price. How can I solve this issue? It seems as the other aliases have not been created when the Final_TotalPrice is reached.
You can't use table aliases in the same select. The normal solution is CTEs or subqueries. But, SQL Server also offers APPLY. (Oracle also supports APPLY and other databases such as Postgres support lateral joins using the LATERAL keyword.)
I like this solution, because you can create arbitrarily nested expressions and don't have to worry about indenting:
SELECT TOP 1000 io.Id, io.QuantityOfProduct, io.Redundant_ProductName,
io.Order_Id,
x.FinalPriceForUnit, x.FinalQuantityUnit, x.Final_QuantityAtomic,
(x.QuantityOfProduct * x.FinalPriceForUnit * x.Final_QuantityAtomic / x.FinalQuantityUnit
) as Final_TotalPrice
FROM dbo.ItemInOrder io OUTER APPLY
(SELECT COALESCE(PriceForUnitOverride, Redundant_PriceForUnit) as FinalPriceForUnit,
COALESCE(QuantityUnit_Override, Redundant_QuantityUnit) as FinalQuantityUnit
COALESCE(QuantityAtomic_Override, Redundant_QuantityAtomic) as Final_QuantityAtomic
) x
WHERE io.IsSoftDeleted = 0
ORDER BY io.Order_Id ;
Notes:
I don't find that [ and ] help me read or write queries at all.
COALESCE() is much simpler than your CASE statements.
With COALESCE() you might consider just putting the COALESCE() expression in the final calculation.
You can't use an alias in the same select. What you can do is find the value in subquery and then use it outside in the expression (or may be repeated the whole case statement in your expression). Also, use COALESCE to instead of CASE.
select t.*,
([QuantityOfProduct] * [FinalPriceForUnit] * ([Final_QuantityAtomic] / [FinalQuantityUnit])) as [Final_TotalPrice]
from (
select top 1000 [Id],
[QuantityOfProduct],
[Redundant_ProductName],
[Order_Id],
coalesce([PriceForUnitOverride], [Redundant_PriceForUnit]) as [FinalPriceForUnit],
coalesce([QuantityUnit_Override], [Redundant_QuantityUnit]) as [FinalQuantityUnit],
coalesce([QuantityAtomic_Override], [Redundant_QuantityAtomic]) as [Final_QuantityAtomic]
from [dbo].[ItemInOrder]
where [IsSoftDeleted] = 0
order by [Order_Id]
) t;

What is the syntax problem here using this subquery inside where clause

SELECT p.pnum, p.pname
FROM professor p, class c
WHERE p.pnum = c.pnum AND c.cnum = CS245 AND (SELECT COUNT(*) FROM (SELECT MAX(m.grade), MAX(m.grade) - MIN(m.grade) AS diff
FROM mark m WHERE m.cnum = c.cnum AND m.term = c.term AND m.section = c.section AND diff <= 20)) = 3
Incorrect syntax near ')'. Expecting AS, FOR_PATH, ID, or QUOTED_ID.
Consider the following example:
SELECT COUNT(1)
FROM SYSCAT.TABLES T
WHERE
(
-- SELECT COUNT(1)
-- FROM
-- (
SELECT COUNT(1)
FROM SYSCAT.COLUMNS C
WHERE C.TABSCHEMA=T.TABSCHEMA AND C.TABNAME=T.TABNAME
-- )
) > 50;
The query above works as is. But the problem is, that if you uncomment the commented out lines, you get the following error message: "T.TABNAME" is an undefined name. and least in Db2 for Linux, Unix and Windows.
You can't push external to the sub-select column references too deeply.
So, your query is incorrect.
It's hard to correct it, until you provide the task description with data sample and the result expected.
I can see a potential syntax errors: It seems that CS245 refers to a value c.cnum may take and not a column name. If that is the case, it should be enclosed in single quotes.

ORA-00936: missing expression Java SQL Exception

I´ve been trying to find the error in this statement for a few hours and can´t seem to find it.
It must have something to do with the AND connecting the two WHERE´s since when deleting the first WHERE it works:
SELECT E_AUFMASS_KOMMENTARE.FIBU_FIRMA,
E_AUFMASS_KOMMENTARE.AUFTR_NR,
E_AUFMASS_KOMMENTARE.KOMMENTAR,
AUFTR_EXT.ART_GRUPPE
FROM HHNG_AU.E_AUFMASS_KOMMENTARE
INNER JOIN HHNG_AU.AUFTR_EXT ON E_AUFMASS_KOMMENTARE.AUFTR_NR = AUFTR_EXT.AUFTR_NR
WHERE (E_AUFMASS_KOMMENTARE.AUFTR_NR = '1248823' )
AND WHERE NOT EXISTS( SELECT * FROM HHNG_AU.EX_KOMMENTARE WHERE EX_KOMMENTARE.AUFTR_NR = '1248823' )
Too many WHERE. You only need where once, then use ANDs and ORs to combine conditions:
SELECT E_AUFMASS_KOMMENTARE.FIBU_FIRMA, E_AUFMASS_KOMMENTARE.AUFTR_NR, E_AUFMASS_KOMMENTARE.KOMMENTAR, AUFTR_EXT.ART_GRUPPE FROM HHNG_AU.E_AUFMASS_KOMMENTARE INNER JOIN HHNG_AU.AUFTR_EXT ON E_AUFMASS_KOMMENTARE.AUFTR_NR = AUFTR_EXT.AUFTR_NR WHERE (E_AUFMASS_KOMMENTARE.AUFTR_NR = '1248823' ) AND NOT EXISTS( SELECT * FROM HHNG_AU.EX_KOMMENTARE WHERE EX_KOMMENTARE.AUFTR_NR = '1248823' )

SQl - An expression of non-boolean type specified in a context where a condition is expected, near ')'

Getting this error with the following query in SQL Server 2014.
SELECT
[Case]
,[Course]
,[Device]
,[IntegerValue]
,[Question]
,IFL.[QuestionSimplified]
,[Revision]
,[Script]
,[TextValue]
,[Timestamp]
,[Type]
,[Variable]
,[Wave]
FROM [dbo].[CosmosData] CD
Left Outer join [dbo].[ImportedFacilityList] IFL on CD.[Variable] = IFL.[Variablename]
where
CD.[Script] = 'CARD-F' and
(select * from [dbo].[CosmosData] where Variable = 'SURVEY_TYPE' and IntegerValue = '2')
When I run the above query I am getting the beloiw error,
Msg 4145, Level 15, State 1, Line 20
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
Any help please?
You have this in the where clause:
and (select * from [dbo].[CosmosData] where Variable = 'SURVEY_TYPE' and IntegerValue = '2')
SQL needs a boolean expression. This is usually formed by using = or a similar comparison operator. In your case, I think you just exant exists:
exists (select * from [dbo].[CosmosData] where Variable = 'SURVEY_TYPE' and IntegerValue = 2)
That said, you might want a correlation clause as well.
Note: I removed the single quotes from the integer value. Only use single quotes for string and date constants.
based on the assumption that you are using case as a key in this table, you can use the follwing to return all rows from cosmos data where your conditions are applied and the select in your where clause has a match using the criteria within it.
SELECT
[Case]
,[Course]
,[Device]
,[IntegerValue]
,[Question]
,IFL.[QuestionSimplified]
,[Revision]
,[Script]
,[TextValue]
,[Timestamp]
,[Type]
,[Variable]
,[Wave]
FROM [dbo].[CosmosData] CD
Left Outer join [dbo].[ImportedFacilityList] IFL on CD.[Variable] = IFL. [Variablename]
where CD.[Script] = 'CARD-F'
and Case
IN
(select Case from [dbo].[CosmosData] where Variable = 'SURVEY_TYPE' and IntegerValue = '2')
Hope that helps any

SQL server syntax error in update statement, but I can't see it

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 ...);