Invalid syntax for SQL Server - sql

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.

Related

Using WITH in MonetDB

I'm trying to execute the next query in MonetDB using "WITH":
with a as (select data_string from colombia.dim_tempo)
select
t.ano_mes
,f.sg_estado
,f.cod_produto
, sum(f.qtd_vendidas) as qtd_vendidas
, count(*) as fact_count
from colombia.fact_retail_market f, colombia.dim_tempo t
where f.cod_anomes = t.data_string
and t.data_string in (a.data_string)
group by
t.ano_mes
,f.sg_estado
,f.cod_produto ;
But always get this message:
What's wrong with the sentence?
The WHERE clause needs to be:
WHERE f.cod_anomes = t.data_string AND
t.data_string IN (SELECT data_string FROM a)
That is, IN needs to be followed by a subquery against the CTE.

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.

How to get ROW_NUMBER() in SQL?

I tried to get the row number using ROW_NUMBER() but it shows the following error:
can't format message 13:896 -- message file C:\WINDOWS\firebird.msg
not found. Dynamic SQL Error. SQL error code = -104. Token unknown -
line 2, column 66.
Here is my code:
SELECT avg(CSIDTL.RATING) ,SVD.SVCADVISORNAME, ROW_NUMBER() OVER(ORDER BY avg(CSIDTL.RATING) )
FROM T_APPT_BOOKING_MSTR MSTR ,T_APPT_CSI_SURVEY CSI,T_APPT_CSI_SURVEY_DTL CSIDTL,
T_SVC_SVCADVISOR_MASTER SVD
WHERE MSTR.APPTBKID = CSI.APPTBKID
AND CSI.CSI_SURVERYID = CSIDTL.CSI_SURVERYID
AND SVD.SVCADVISORID = MSTR.SVCADVISORID
AND CSI.FEEDBACK_STATUS = 'Y'
AND CSIDTL.question ='Service Advisor'
GROUP BY SVD.SVCADVISORNAME
ORDER by avg(CSIDTL.RATING)
The ROW_NUMBER() function was introduced with Firebird 3.0, released just few days ago. See release notes, chapter Window (Analytical) Functions for exact syntax. The error you get suggests you're using an older version of Firebird which doesn't have this feature.
I use this in Firebird 2.5
Reference: http://www.firebirdfaq.org/faq343/
SELECT rdb$get_context('USER_TRANSACTION', 'row#') as row_number, DUMMY, A.*
FROM your_table A
CROSS JOIN
(SELECT rdb$set_context('USER_TRANSACTION', 'row#',
COALESCE(CAST(rdb$get_context('USER_TRANSACTION', 'row#') AS INTEGER), 0) + 1) AS dummy
FROM rdb$database) dummy
Example for Firebird 3.0+
SELECT row_number() over(), t.* FROM test t
Other alternative in Firebird 2.5 is use of generators
CREATE GENERATOR tmp$rn;
UPDATE my_table t SET t.id_field = (SELECT FIRST 1 NEXT VALUE FOR tmp$rn AS "row_number"
FROM my_table ORDER BY another_field1 DESC, another_field2 DESC);
DROP GENERATOR tmp$rn;
This may be helpful,
Reference
Firebird 3.0 Language Reference - 10.4. Ranking Functions
Available in :DSQL, PSQL
Result type :BIGINT
Syntax
ROW_NUMBER () OVER <window-specification>
Returns the sequential row number in the partition of the result set, where 1 is the first row in each of the partitions.

Query giving error ora-00923 from keyword not found where expected

I am using Oracle 8i. When I tried to run this below script I am getting the error ora-00923 from keyword not found where expected.
Please find the below query which I am using.
select i.siid,
sp.access_point_status,
csp.id_number,
act.entry_time,
act.addnl_info,
row_num() over (partition by i.siid order by act.entry_time desc) act_row
from table_Service_point sp,
table_case_to_service_point csp,
table_case cs,
table_act_entry act,
(select distinct siid,
iopt.installedopts2axspoint
from table_installed_options iopt,
tmp_efms_clarify inp
where iopt.siid = inp.service_instance
and iopt.siid = 'DSL580155-105-1') i
where sp.objid = csp.case2servicepoint
and csp.id_number = cs.id_number
and cs.objid = act.act_entry2case
and sp.objid = i.installedopts2axspoint
Try using row_number() instead of row_num().
Single quotes '' in oracle denote characters, not names.
Replace
iopt.siid='DSL580155-105-1'
with
iopt.siid="DSL580155-105-1"
I do think row_number() is the correct spelling, not row_num(). Google it for more on how it works. And also, just a reminder, is DSL580155-105-1 actually a column?

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