Search code examples
sqlsql-serversubqueryderived-table

getting message Incorrect syntax near ')' in the last line


I am getting this message in SQL Server 2019:

Msg 102, Level 15, State 1, Line 17 Incorrect syntax near ')'.

I am getting the error in the last line.

What mistake am I making?

Select count(*) from (
   select t1.CUST_ID, count(t1.qty)
   from TRANSACTIONS t1
   where t1.qty>0
   group by t1.CUST_ID
   having count(t1.qty)>10)

Solution

  • You must provide the aliases for your subquery and aggregated column:

    Select count(*) from (
               select t1.CUST_ID, count(t1.qty) AS CNT
               from TRANSACTIONS t1
               where t1.qty>0
               group by t1.CUST_ID
               having count(t1.qty)>10
            ) AS T
    

    Test it here