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

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.

Related

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

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.

Invalid syntax for SQL Server

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.

Error in select statement, with union all in a subquery

In Oracle 11g, I came across an error for a query and cannot figure why it is erroring on me. Here is the query:
select
main_data.issue_number,
main_data.transaction_number
from
(
select
p1.payment_date,
p1.media_number,
p1.payment_amount,
p1.issue_number,
p1.advice_na_number,
name.name_address_line_1,
name.name_address_line_2,
name.name_address_line_3,
name.name_address_line_4,
name.name_address_line_5,
name.name_address_line_6,
name.name_address_line_7,
name.name_address_city,
name.state_code,
name.address_country_code,
name.zip_code,
name.tax_id_number,
p1.output_tx_number_prin,
p1.output_tx_number_int,
'' as "transaction_number",
p1header.check_account_number
from
p1
left join name on p1.name_address_number = name.name_address_number
left join p1header on p1.issue_number = p1header.issue_number
UNION ALL
select
check.date_of_payment,
check.media_number,
check.payment_amount,
check.issue_number,
check.payee_na_number,
name.name_address_line_1,
name.name_address_line_2,
name.name_address_line_3,
name.name_address_line_4,
name.name_address_line_5,
name.name_address_line_6,
name.name_address_line_7,
name.name_address_city,
name.state_code,
name.address_country_code,
name.zip_code,
name.tax_id_number,
'' as "output_tx_number_prin",
'' as "output_tx_number_int",
check.transaction_number,
check.dda_number as "check_account_number"
from check
left join name on check.payee_na_number = name.name_address_number
) main_data
Selecting individual fields like above will give me an "invalid identifier error". If I do select * then it gives me back the data without any error. What am I doing wrong here? Thank you.
The old quoted identifier problem... see point 9 in the database object naming documentation, and note that Oracle does not recommend using quoted identifiers.
You've put your column alias as lower case inside double-quotes. That means that any references to it also have to be quoted and exactly match the case. So this would work:
select
main_data.issue_number,
main_data."transaction_number"
from
...
But unless you have a burning need to have that alias like that - and I doubt you do as all the identifier names from the actual table columns are not quoted - it would be simpler to remove the double quotes from the inner selects:
select
main_data.issue_number,
main_data.transaction_number
from
(
select
...
'' as transaction_number,
p1header.check_account_number
...
UNION ALL
select
...
'' as output_tx_number_prin,
'' as output_tx_number_int,
check.transaction_number,
check.dda_number as check_account_number
...
You don't actually need to alias the columns in the second branch of the union; the column identifiers will all be taken from the first branch.

Trying relational division on oracle (right parenthesis missing?)

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.

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