user3051630
user3051630

Reputation: 65

Incorrect syntax near the keyword 'SELECT'. Incorrect syntax near ')'

I used the sql code in vb.net

SELECT [Table1 Query].[amel_code], [Table1 Query].[kala_code], Sum([Table1 Query].
[SumOfqty]) AS SumOfSumOfqty FROM(
SELECT Table1.amel_code,
       Table1.amani_code,
       Table1.kala_code,
       Sum(Table1.qty) AS SumOfqty
FROM Table1
GROUP BY Table1.amel_code,
         Table1.amani_code,
         Table1.kala_code HAVING (((Table1.amel_code)=[?]) AND ((Table1.amani_code)<[?]));
)
GROUP BY [Table1 Query].[amel_code], [Table1 Query].[kala_code];

This code is working properly but the sql web. Sheet gives the following error:

Incorrect syntax near the keyword 'SELECT'. Incorrect syntax near ')'.

please help me.

Upvotes: 3

Views: 43396

Answers (3)

Amir Keshavarz
Amir Keshavarz

Reputation: 3108

Every subquery or nested query should have alias. example:

SELECT *
FROM (SELECT * FROM T1 WHERE ID>50) D

This was my answer on your other question.

Upvotes: 0

Vishal Suthar
Vishal Suthar

Reputation: 17183

This is what you are missing:

1) Give an alias Table1 Query to a nested query. The error says: It is not able to identify what [Table1 Query] is for. so you have to give that alias to a sub query.

SELECT [Table1 Query].[amel_code], [Table1 Query].[kala_code], Sum([Table1 Query].[SumOfqty]) AS SumOfSumOfqty 
FROM(
SELECT Table1.amel_code,
       Table1.amani_code,
       Table1.kala_code,
       Sum(Table1.qty) AS SumOfqty
FROM Table1
GROUP BY Table1.amel_code,
         Table1.amani_code,
         Table1.kala_code HAVING (((Table1.amel_code)=[?]) AND ((Table1.amani_code)<[?]))
) [Table1 Query]
GROUP BY [Table1 Query].[amel_code], [Table1 Query].[kala_code];

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

You need to remove semicolon at the end of the nested query, and add an alias to it:

SELECT [Table1 Query].[amel_code], [Table1 Query].[kala_code], Sum([Table1 Query].[SumOfqty]) AS SumOfSumOfqty
FROM (
SELECT Table1.amel_code,
       Table1.amani_code,
       Table1.kala_code,
       Sum(Table1.qty) AS SumOfqty
FROM Table1
GROUP BY Table1.amel_code,
         Table1.amani_code,
         Table1.kala_code
HAVING (((Table1.amel_code)=[?])
         AND ((Table1.amani_code)<[?])) -- ; <<== Remove this semicolon
) [Table1 Query] -- <<== Add this alias
GROUP BY [Table1 Query].[amel_code], [Table1 Query].[kala_code];

Demo on SQLFiddle.

Upvotes: 3

Related Questions