对于每种产品,我需要知道在一段时间内哪个客户的库存量最大。我只希望每种产品有 1 个客户返回我的结果。我知道我应该在 QtyShip 上使用 MAX,但我不知道在哪里。在子查询中?
我从下面的代码开始。有人可以告诉我我在哪里投入最大值吗?
Select o.cono ,
o.ProdId ,
o.CustId ,
c.[name] as 'CustomerName' ,
s.shipto ,
s.[name] as 'ShiptoName' ,
s.user15 as 'divno' ,
o.WhseId ,
SUM(NetAmt) as 'totalNet' ,
SUM(cost) as 'totalCost' ,
SUM(QtyShip) as 'totalQtyShip'
FROM Order_Line_Invoice o
LEFT JOIN ARSC c
on o.cono = c.cono
and o.CustId = c.custno
and c.insx = 1
LEFT JOIN ARSS s
on o.cono = s.cono
and o.CustId = s.custno
and o.ShipToId = s.shipto
and s.insx = 1
GROUP BY o.cono ,
o.ProdId ,
o.CustId ,
c.[name] ,
s.shipto ,
s.[name] ,
s.user15 ,
o.WhseId