sql server 2008中,如何得出:A列除以B列,然后算出他们的平均值,还有一个条件:先按照name分组。在线等
有一个Orders表,先按照ordername分组,然后计算每一组中的A列除以B列后所有行的平均值。我这么写的,但是错的:selectavg(rate)from(sele...
有一个Orders表,先按照ordername分组,然后计算每一组中的A列除以B列后所有行的平均值。
我这么写的,但是错的:select avg(rate) from (select round(finishedNumber/quatity,4) as rate from Orders Order by ordername)
我打错了我,上述分组是用group by,不是 order by排序! 展开
我这么写的,但是错的:select avg(rate) from (select round(finishedNumber/quatity,4) as rate from Orders Order by ordername)
我打错了我,上述分组是用group by,不是 order by排序! 展开
2个回答
展开全部
with cte_t(rate)
as (select round(sum(finishedNumber)/sum(quatity),4) from Orders group by ordername)
select rate from cte_t --如果是求全部平均,则改为:select avg(rate) from cte_t
as (select round(sum(finishedNumber)/sum(quatity),4) from Orders group by ordername)
select rate from cte_t --如果是求全部平均,则改为:select avg(rate) from cte_t
追问
显示得出每一行的A与B的商,然后才求平均值,平均值是按分组以后算的,就是先分组,在求平均值!
追答
我已将order by 改为group by了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询