路明非的小跟班 2022-04-30 18:51 采纳率: 71.4%
浏览 78
已结题

sql查询语句中group by遇到一些问题

img


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 表达式必须至少包含一个不是外部引用的列。
求问怎么解决呀?

  • 写回答

1条回答 默认 最新

  • 夜猫nightcat 2022-04-30 19:18
    关注

    你这应该是严格模式
    记住一个原则
    在这种分组的需求中,出现在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
    就报错了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月9日
  • 已采纳回答 5月1日
  • 创建了问题 4月30日

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂