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 ;
这是我的查询,即使我在哪里使用,也会一直收到此错误。
我不知道该怎么做。
第15行,第15行,第1行,第22行 关键字附近的语法错误'有'。
答案 0 :(得分:1)
你不能使用别名。请改用下面的方法,或者使用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
答案 1 :(得分:0)
您无法在having语句中引用select语句中的别名。尝试使用子查询重建查询以对其进行过滤。