
select x.CategoryName '类别名称',y.ProductName '产品名称',sum(z.UnitPrice*Quantity) '产品销售额',
case when month(a.ShippedDate)<=3 and month(a.ShippedDate)>=1 then '1季度'
when month(a.ShippedDate)<=6 and month(a.ShippedDate)>=4 then '2季度'when month(a.ShippedDate)<=9 and month(a.ShippedDate)>=7 then '3季度'when month(a.ShippedDate)<=12 and month(a.ShippedDate)>=10 then '4季度'end as '发货季度'from Categories x,Products y,[Order Details] z,Orders a
where x.CategoryID=y.CategoryID and y.ProductID=z.ProductID and z.OrderID=a.OrderID and YEAR(a.OrderDate)=1997
group by y.ProductName,'发货季度'
结果报错:每个 GROUP BY 表达式必须至少包含一个不是外部引用的列。
求问怎么解决呀?
你这应该是严格模式
记住一个原则
在这种分组的需求中,出现在select中的字段,要和group by中的相同,select中多出来的字段要包含在聚合函数中,例如
select a,b,sum(c)
from tb
group by a,b
这是可以的
select a,b,c
from tb
group by a,b
就报错了。