我正在执行查询并显示错误无法对包含聚合或子查询的表达式执行聚合函数。
查询
SELECT
tbl_Product.ID,
tbl_Product.ArticleID,
tbl_Product.Title,
tbl_Product.Description,
tbl_Product.Price,
tbl_ProductType.Name,
tbl_Status.StatusName,
tbl_VisibilityStatus.VisibilityStatus,
MAX(
CASE
WHEN tbl_RelatedProduct.TypeOfRelation = 1 THEN 'Bundle'
WHEN tbl_Product.ID IN
(
select tbl_RelatedProduct.Product2ID
from tbl_RelatedProduct
where tbl_RelatedProduct.Product1ID=9 and tbl_RelatedProduct.TypeOfRelation=1) THEN 'Bundle'
END
) 'Bundle',
MAX(
CASE
WHEN tbl_RelatedProduct.TypeOfRelation = 2 THEN 'Follower' END) 'Follower',
MAX(
CASE
WHEN tbl_RelatedProduct.TypeOfRelation = 3 THEN 'Related' END) 'Related'
FROM
tbl_Product inner JOIN
tbl_ProductType ON tbl_Product.ProductTypeId = tbl_ProductType.ID Inner JOIN
tbl_Status ON tbl_Product.StatusID = tbl_Status.ID Inner JOIN
tbl_VisibilityStatus ON tbl_Product.VisibilityID = tbl_VisibilityStatus.ID
left JOIN tbl_RelatedProduct ON tbl_Product.ID = tbl_RelatedProduct.Product1ID
group by
tbl_Product.ID,
tbl_Product.ArticleID,
tbl_Product.Title,
tbl_Product.Description,
tbl_Product.Price,
tbl_ProductType.Name,
tbl_Status.StatusName,
tbl_VisibilityStatus.VisibilityStatus
order by tbl_Product.Title
任何人都知道如何帮助... plsss
答案 0 :(得分:0)
以下行导致问题:
WHEN tbl_Product.ID IN
(
select tbl_RelatedProduct.Product2ID
from tbl_RelatedProduct
where tbl_RelatedProduct.Product1ID=9 and tbl_RelatedProduct.TypeOfRelation=1) THEN 'Bundle'
END
) 'Bundle',
您正在MAX
使用CASE
CASE
中的一个语句使用子查询,这会导致错误。您可能需要考虑使用连接而不是子查询来实现此目的。
答案 1 :(得分:0)
SELECT tbl_Product.ID,tbl_Product.ArticleID,tbl_Product.Title,tbl_Product.Description,tbl_Product.Price,tbl_ProductType.Name,tbl_Status.StatusName,tbl_VisibilityStatus.VisibilityStatus, 情况何时((从tbl_RelatedProduct中选择count(1),其中tbl_RelatedProduct.TypeOfRelation = 1,tbl_RelatedProduct.Product1ID = tbl_Product.Id)> 0)然后1 when((从tbl_RelatedProduct中选择count(1),其中tbl_RelatedProduct.TypeOfRelation = 1,tbl_RelatedProduct.Product2ID = tbl_Product.Id)> 0)然后1 其他0结束为Bundle, (从tbl_RelatedProduct中选择count(1),其中tbl_RelatedProduct.TypeOfRelation = 2,tbl_RelatedProduct.Product1ID = tbl_Product.Id)作为追随者, (从tbl_RelatedProduct中选择count(1),其中tbl_RelatedProduct.TypeOfRelation = 3,tbl_RelatedProduct.Product1ID = tbl_Product.Id)as related
FROM tbl_Product 内部联接 tbl_ProductType ON tbl_Product.ProductTypeId = tbl_ProductType.ID Inner JOIN tbl_Status ON tbl_Product.StatusID = tbl_Status.ID 内部联接 tbl_VisibilityStatus ON tbl_Product.VisibilityID = tbl_VisibilityStatus.ID
通过此查询解决:)
答案 2 :(得分:0)
我想我知道你要做什么,这有点奇怪。我会将此全部输出为单个列,并丢失MAX
聚合,如下所示:
SELECT
p.ID,
p.ArticleID,
p.Title,
p.Description,
p.Price,
pt.Name,
s.StatusName,
v.VisibilityStatus,
CASE
WHEN rp.TypeOfRelation = 1 OR (rp2.Product1ID = 9 AND rp2.TypeOfRelation = 1)
THEN 'Bundle'
WHEN rp.TypeOfRelation = 2
THEN 'Follower'
WHEN rp.TypeOfRelation = 3
THEN 'Related'
ELSE Null
END AS Relation
FROM
tbl_Product p
INNER JOIN tbl_ProductType pt
ON p.ProductTypeId = pt.ID
INNER JOIN tbl_Status s
ON p.StatusID = s.ID
INNER JOIN tbl_VisibilityStatus v
ON p.VisibilityID = v.ID
LEFT JOIN tbl_RelatedProduct rp
ON p.ID = rp.Product1ID
LEFT JOIN tbl_RelatedProduct rp2
ON p.ID = rp2.Product2ID
ORDER BY p.Title
如果您仍希望将它们放在不同的列中,请按以下方式分解CASE
语句:
SELECT
p.ID,
p.ArticleID,
p.Title,
p.Description,
p.Price,
pt.Name,
s.StatusName,
v.VisibilityStatus,
CASE
WHEN rp.TypeOfRelation = 1 OR (rp2.Product1ID = 9 AND rp2.TypeOfRelation = 1)
THEN 'Bundle'
ELSE Null
END AS Bundle,
CASE
WHEN rp.TypeOfRelation = 2
THEN 'Follower'
ELSE Null
END AS Follower,
CASE
WHEN rp.TypeOfRelation = 3
THEN 'Related'
ELSE Null
END AS Related
FROM
tbl_Product p
INNER JOIN tbl_ProductType pt
ON p.ProductTypeId = pt.ID
INNER JOIN tbl_Status s
ON p.StatusID = s.ID
INNER JOIN tbl_VisibilityStatus v
ON p.VisibilityID = v.ID
LEFT JOIN tbl_RelatedProduct rp
ON p.ID = rp.Product1ID
LEFT JOIN tbl_RelatedProduct rp2
ON p.ID = rp2.Product2ID
ORDER BY p.Title