简体   繁体   English

消息156,级别15,状态1,第22行在关键字“具有”附近的语法错误

[英]Msg 156, Level 15, State 1, Line 22 Incorrect syntax near the keyword 'having'

SELECT 
    CCDMS_Company.CompanyID, CCDMS_Company.CompanyName,
    CCDMS_Container.ContainerID, CCDMS_Container.Price, 
    SUM(CASE WHEN CCDMS_Container.ContainerSize = 1
             THEN 1 ELSE 0 END) AS size1,
    SUM(CASE WHEN CCDMS_Container.ContainerSize = 2
             THEN 1 ELSE 0 END) AS size2,
    SUM(CASE WHEN CCDMS_Container.ContainerSize = 3
             THEN 1 ELSE 0 END) AS size3,
    SUM(CASE WHEN CCDMS_Container.ContainerSize = 4
             THEN 1 ELSE 0 END) AS size4,
    SUM(CASE WHEN CCDMS_Container.ContainerSize = 5
             THEN 1 ELSE 0 END) AS size5
FROM 
    CCDMS_Company
INNER JOIN 
    CCDMS_Container ON CCDMS_Container.Co_ID = CCDMS_Company.CompanyID
                    AND CCDMS_Company.CityID = 1
GROUP BY 
    CCDMS_Company.CompanyID, CCDMS_Company.CompanyName,
    CCDMS_Container.ContainerID, CCDMS_Container.Price
HAVING
    size1 >=2
    AND size2 >= 1
    AND size3 >= 1
    AND size4 >= 0
    AND size5 >= 1 ;

This is my query and I keep getting this error even when I use where . 这是我的查询,即使使用where,我也一直收到此错误。

I don't know what to do . 我不知道该怎么办 。

Msg 156, Level 15, State 1, Line 22 消息156,第15层,州1,第22行
Incorrect syntax near the keyword 'having'. 关键字“具有”附近的语法不正确。

You can not use alias in having. 您不能在拥有别名时使用别名。 Use below instead, or use CTE for simplicity 请改为使用以下内容,或为了简单起见使用CTE

select CCDMS_Company.CompanyID , CCDMS_Company.CompanyName ,
CCDMS_Container.ContainerID ,CCDMS_Container.Price , 
sum(case when CCDMS_Container.ContainerSize=1
 then 1 else 0 end)as size1,
sum(case when CCDMS_Container.ContainerSize=2
 then 1 else 0 end)as size2,
sum(case when CCDMS_Container.ContainerSize=3
 then 1 else 0 end)as size3,
sum(case when CCDMS_Container.ContainerSize=4
 then 1 else 0 end)as size4,
sum(case when CCDMS_Container.ContainerSize=5
 then 1 else 0 end)as size5
   from 
CCDMS_Company
 inner join
CCDMS_Container
on CCDMS_Container.Co_ID = CCDMS_Company.CompanyID
and 
CCDMS_Company.CityID = 1
group by 
CCDMS_Company.CompanyID,CCDMS_Company.CompanyName,CCDMS_Container.ContainerID,CCDMS_Container.Price

having
    sum(case when CCDMS_Container.ContainerSize=1 then 1 else 0 end) >=2
and sum(case when CCDMS_Container.ContainerSize=2 then 1 else 0 end) >= 1
and sum(case when CCDMS_Container.ContainerSize=3 then 1 else 0 end) >= 1
and sum(case when CCDMS_Container.ContainerSize=4 then 1 else 0 end) >= 0
and sum(case when CCDMS_Container.ContainerSize=5 then 1 else 0 end) >= 1

You can't reference the aliases from your select statement in the having statement. 您不能在have语句中引用select语句中的别名。 Try rebuilding your query using a subquery to filter it. 尝试使用子查询来重建查询以对其进行过滤。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 消息 156,级别 15,State 1,第 5 行关键字“LEFT”附近的语法不正确。 消息 156,级别 15,State 1,第 6 行 'b' 附近的语法不正确 - Msg 156, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'LEFT'. Msg 156, Level 15, State 1, Line 6 Incorrect syntax near 'b' Msg 156,Level 15,State 1,Procedure inventory,Line 6 [Batch Start Line 2]关键字'Where'附近的语法不正确 - Msg 156, Level 15, State 1, Procedure inventory, Line 6 [Batch Start Line 2] Incorrect syntax near the keyword 'Where' 消息156,级别15,状态1,第50行关键字“ GROUP”附近的语法错误 - Msg 156, Level 15, State 1, Line 50 Incorrect syntax near the keyword 'GROUP' 消息156,级别15,状态1,过程ShowDirectorateList,第739行关键字'END'附近的语法不正确 - Msg 156, Level 15, State 1, Procedure ShowDirectorateList, Line 739 Incorrect syntax near the keyword 'END' 消息156,级别15,状态1,行2493关键字“ ON”附近的语法错误 - Msg 156, Level 15, State 1, Line 2493 Incorrect syntax near the keyword 'ON' 消息156,级别15,状态1,第16行关键字“ IF”附近的语法错误 - Msg 156, Level 15, State 1, Line 16 Incorrect syntax near the keyword 'IF' 消息156,级别15,状态1,过程CLR_TRIGGER_NAUDOJASI,第1行关键字“插入”附近的语法错误 - Msg 156, Level 15, State 1, Procedure CLR_TRIGGER_NAUDOJASI, Line 1 Incorrect syntax near the keyword 'insert' 消息 156,级别 15,状态 1,第 7 行关键字“BETWEEN”附近的语法不正确 - Msg 156, Level 15, State 1, Line 7 Incorrect syntax near the keyword 'BETWEEN' 消息 156,级别 15,状态 1,第 2 行 关键字“case”附近的语法不正确。 我的 SQL 有什么问题 - Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'case'. Whats wrong in my SQL SQL Server:错误消息156,级别15,状态1,第12行关键字“ AS”附近的语法不正确 - SQL Server: error Msg 156, Level 15, State 1, Line 12 Incorrect syntax near the keyword 'AS'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM