select PO_Order_Qty, Avg([PO_Order_Qty])as totalAverage
FROM [FirstStrike_Retail].[custom].[Whse_Pricing_QR]
where item_code ='111' and PO_order_qty <(Avg([PO_Order_Qty])*2)
group by PO_Order_Qty
我在MS Sql中使用上面的sql获取特定项目代码订单数量的平均值。我期待只有一个回报值。
如果任何数量超过平均值的两倍,我需要消除。
由于 Xplr
答案 0 :(得分:2)
在检查聚合函数(MAX,MIN,SUM,AVG等)时应该使用HAVING子句。我不想只给你答案,但尝试这样的事情。
select PO_Order_Qty, Avg([PO_Order_Qty])as totalAverage
FROM [FirstStrike_Retail].[custom].[Whse_Pricing_QR]
where item_code ='111'
group by PO_Order_Qty
HAVING PO_order_qty <(Avg([PO_Order_Qty])*2)
编辑:根据你的评论尝试这个...像这样
select *
FROM [FirstStrike_Retail].[custom].[Whse_Pricing_QR]
where item_code ='111'
and PO_Order_Qty <
(select Avg([PO_Order_Qty])as totalAverage
FROM [FirstStrike_Retail].[custom].[Whse_Pricing_QR]) * 2