无法找出子查询中的错误

问题描述 投票:0回答:2

我在 sql server 2019 中收到此消息

消息 102,级别 15,状态 1,第 17 行“)”附近的语法不正确。

我认为我在最后一行遇到了错误。我犯了什么错误,请告诉 预先感谢

*这是我的查询*

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)
sql sql-server subquery derived-table
2个回答
2
投票

您必须提供子查询和聚合列的别名:

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

测试一下这里


0
投票

或者(按照 Slava 的说法),完全删除

select
子句中的聚合函数,因为您对它返回的内容并不真正感兴趣。如果 main select 也获取了该值,那么您
需要它,但是 - 它没有。

select count(*)
from (select t1.cust_id
      from transactions t1
      where t1.qty > 0
      group by t1.cust_id
      having count(t1.qty) > 10
     )
© www.soinside.com 2019 - 2024. All rights reserved.