假设 Sql Server 2005+,这应该工作
;With Cte As(
Select
h.cstponbr 'Customer PO No.',
h.sopnumbe 'Invoice No.',
d.itemnmbr 'Item No.',
d.itemdesc 'Item Description',
d.qtyfulfi 'Qty Fulfilled',
sum(d.qtyfulfi) Over(Partition By d.itemnmbr) 'Total Qty Fulufilled',
Rn =Row_Number() Over(Partition By d.itemnmbr Order By (Select 1))
from sop10100 h
inner join sop10200 d on (h.sopnumbe = d.sopnumbe)
where h.cstponbr = @CUSTPONUM )
Select *
From Cte
Where Rn = 1
更通用的应该是
select h.cstponbr 'Customer PO No.',
h.sopnumbe 'Invoice No.',
X.ItemNo,
X.ItemDescription,
X.QtyFulfilled,
X.TotalQtyFulufilled
from sop10100 h
inner join
(Select
X.ItemNo
,d.itemdesc 'ItemDescription'
,d.qtyfulfi 'QtyFulfilled'
,d.sopnumbe
,X.TotalQtyFulufilled
From sop10200 d
Inner Join
(Select d.itemnmbr 'ItemNo',sum(d.qtyfulfi)'TotalQtyFulufilled'
From sop10200 d
group by d.itemnmbr)X
On d.itemnmbr = X.ItemNo)X
on (h.sopnumbe = X.sopnumbe)
where h.cstponbr = @CUSTPONUM