我有以下查询:
select distinct
b.BinName, Si.Code, si.Name,
b.ConfirmedQtyInStock + b.UnconfirmedQtyInStock as StockQty,
AllocationPriority,
Case
When r1.BinName = b.BinName Then r2.BinName
Else r1.BinName
End as [Location1],
Case
When r1.BinName = b.BinName Then r2.Qty
Else r1.Qty
End as [Qty1],
Case
When r2.BinName In (b.BinName, r1.BinName) Then r3.BinName
Else r2.BinName
End as [Location2]
from
BinItem b
Inner Join
StockItem si On si.ItemID = b.ItemID
left outer join
(select
ROW_NUMBER() Over (Partition By si.Code Order By si.Code, ConfirmedQtyInStock + UnconfirmedQtyInStock asc) as ranking,
si.Code, bi.BinName,
bi.ConfirmedQtyInStock + bi.UnconfirmedQtyInStock as Qty
from
stockitem si
inner join
binitem bi on bi.ItemID = si.itemid
where
bi.ConfirmedQtyInStock + bi.UnconfirmedQtyInStock > 0) r1 On r1.ranking = 1 and r1.Code = si.Code
left outer join
(select
ROW_NUMBER() Over (Partition By si.Code Order By si.Code, ConfirmedQtyInStock+UnconfirmedQtyInStock asc) as ranking,
si.Code, bi.BinName,
bi.ConfirmedQtyInStock + bi.UnconfirmedQtyInStock as Qty
from
stockitem si
inner join
binitem bi on bi.ItemID = si.itemid
where
bi.ConfirmedQtyInStock + bi.UnconfirmedQtyInStock > 0) r2 On r2.ranking = 2 and r2.Code = si.Code
left outer join
(select
ROW_NUMBER() Over (Partition By si.Code Order By si.Code, ConfirmedQtyInStock+UnconfirmedQtyInStock asc) as ranking,
si.Code, bi.BinName,
bi.ConfirmedQtyInStock + bi.UnconfirmedQtyInStock as Qty
from
stockitem si
inner join
binitem bi on bi.ItemID = si.itemid
where
bi.ConfirmedQtyInStock + bi.UnconfirmedQtyInStock > 0) r3 On r3.ranking = 3 and r3.Code = si.Code
left outer join
(select
ROW_NUMBER() Over (Partition By si.Code Order By si.Code, ConfirmedQtyInStock + UnconfirmedQtyInStock asc) as ranking,
si.Code, bi.BinName,
bi.ConfirmedQtyInStock + bi.UnconfirmedQtyInStock as Qty
from
stockitem si
inner join
binitem bi on bi.ItemID = si.itemid
where
bi.ConfirmedQtyInStock + bi.UnconfirmedQtyInStock > 0) r4 On r4.ranking = 4 and r4.Code = si.Code
Where
si.StockItemStatusID = 0
and ConfirmedQtyInStock + UnconfirmedQtyInStock > 0
and si.code = '06403107C'
Order By
Code, b.BinName
我要做的是确保每个列中只有唯一的位置。例如:
C03B1 06403107C 1 1/4in COUNTER NUT CHROME PLATED BRASS 543.00000 1
C04A1 9.00000
C04A1 06403107C 1 1/4in COUNTER NUT CHROME PLATED BRASS 9.00000 4
C03B1 543.00000
但是我还没有完全确定案例逻辑来正确移动r1,r2,r3,r4子查询中的所有值。