我正在编写一个查询,该查询将用于填充用于存放仓库中的库存的报告。
该报表有3个参数,即来源库存位置、来源仓位编号和目标库存位置。
当前库存将保存在源库存位置和仓位编号中。
目标库存位置是库存需要移动到的位置。
每个变量在每个库存位置都有一个默认的仓位编号。多个变体可以具有相同的默认仓位编号。
仓库周围的仓位编号可能不是按字母顺序排列的,因此每个仓位编号都被分配了一条步行路线,作为仓库周围最有效的步行路线。
报表将查看与目标库存位置中的物料关联的默认仓位编号,如果为空,则将其作为入库建议提供。如果默认仓位编号不为空,它将在目标库存位置(其中步行路线高于默认仓位)中查找下一个可用的空仓位,然后将其作为入库建议提供。
查询运行得很好,而且确实如此,但是它报告的bin与上面几行中的"NextBinNo“建议相同。
我如何让外部的APPLY NextBinNo在更高的数据行中过滤掉之前建议的任何bin?此外,如果两个项目具有相同的默认仓位编号,则应使用具有此默认仓位编号的第二行的NextBinNo。
我现在的问题是:
Select
row_number() Over(Order by DestSL.sl_id) as RowNo,
Stock_location.sl_name,
bin_number.bn_bin_number,
variant_detail.vad_variant_code,
variant_detail.vad_description,
variant_transaction_header.vth_current_quantity,
variant_transaction_header.vth_batch_number,
purchase_order_header.poh_order_number,
supplier_detail.sd_ow_account,
DestSL.sl_id as 'DestinationSLID',
DestSL.sl_name as 'DestinationStockLocation',
DestDefaultBin.bn_bin_number as 'DestinationDefaultBin',
DestDefaultBin.bn_walk_route as 'DestinationDefaultWalkRoute',
isnull(DestDefaultBinQty.BinQty,0) as QtyInDefaultBin,
NextBinNo.NextBinNo as 'NextBinNo',
NextBinNo.NextWalkRoute as 'NextBinWalkRoute',
isnull(NextBinNo.BinQty,0) as 'NextBinQty',
case when DestDefaultBin.bn_bin_number is null
then 'Not Stocked in This Location'
Else
case when isnull(DestDefaultBinQty.BinQty,0) > 0
then
case when NextBinNo.NextBinNo is NULL
then 'No Free Bin'
Else NextBinNo.NextBinNo
End
Else DestDefaultBin.bn_bin_number
End
End as 'Put Away Destination'
From variant_transaction_header
join bin_number on bin_number.bn_id = variant_transaction_header.vth_bn_id
join stock_location on stock_location.sl_id = variant_transaction_header.vth_sl_id
join variant_detail on variant_detail.vad_id = variant_transaction_header.vth_vad_id
join transaction_type on transaction_Type.tt_id = variant_transaction_header.vth_tt_id
left join purchase_order_line on purchase_order_line.pol_id = variant_transaction_header.vth_pol_id
left join purchase_order_header on purchase_order_header.poh_id = purchase_order_line.pol_poh_id
left join supplier_detail on supplier_detail.sd_id = purchase_order_header.poh_sd_id
join stock_location DestSL on DestSL.sl_id = @DestinationStockLoc
left join variant_stock_location DestVSL on DestVSL.vsl_vad_id = variant_detail.vad_id and DestVSL.vsl_sl_id = DestSL.sl_id
left join bin_number DestDefaultBin on DestDefaultBin.bn_id = DestVSL.vsl_bn_id
left join
(select sum(variant_transaction_header.vth_current_quantity) as BinQty,
variant_transaction_header.vth_bn_id
from variant_transaction_header
join transaction_type on transaction_Type.tt_id = variant_transaction_header.vth_tt_id
Where variant_transaction_header.vth_current_quantity > 0
and transaction_type.tt_transaction_type = 'IN' and transaction_Type.tt_update_current_qty = 1
Group by variant_transaction_header.vth_bn_id) as DestDefaultBinQty on DestDefaultBinQty.vth_bn_id = DestDefaultBin.bn_id
Outer Apply
(select top 1
row_number() Over(Order by NextBin.bn_bin_number) as RowNo,
NextBin.bn_bin_number as NextBinNo,
NextBin.bn_walk_route as NextWalkRoute,
BinQty.BinQty
from
Stock_location DestSL
Join bin_number NextBin on NextBin.bn_sl_id = DestSL.sl_id
left join
(select sum(variant_transaction_header.vth_current_quantity) as BinQty,
variant_transaction_header.vth_bn_id
from variant_transaction_header
join transaction_type on transaction_Type.tt_id = variant_transaction_header.vth_tt_id
Where variant_transaction_header.vth_current_quantity > 0
and transaction_type.tt_transaction_type = 'IN' and transaction_Type.tt_update_current_qty = 1
Group by variant_transaction_header.vth_bn_id) as BinQty on BinQty.vth_bn_id = NextBin.bn_id
Where NextBin.bn_sl_id = @DestinationStockLoc
and NextBin.bn_walk_route > DestDefaultBin.bn_walk_route
And isnull(BinQty.BinQty,0) = 0
order by NextBin.bn_walk_route, nextbin.bn_bin_number) as NextBinNo
where variant_transaction_header.vth_current_quantity > 0
and transaction_type.tt_transaction_type = 'IN' and transaction_Type.tt_update_current_qty = 1
and stock_location.sl_id = @SourceStockLoc and bin_number.bn_id = @SourceBinNo你可以在下面看到我当前的结果:
Row2正在使用NextBinNo,因为默认的仓位有库存。Row3还建议使用AA08A2作为下一个bin。
第6行目前建议使用AA01A2,但在Row1中已经建议这样做。

发布于 2015-08-19 19:17:30
为了回答这个问题,我最终不能直接用SQL实现我想要的结果。
数据最终将返回给可以运行Visual Basic代码的报表编写器。
我必须在VB中单独执行NextBinNo子查询。
在VB中,我可以定义一个字符串,它包含所有“使用的”仓位编号的列表,因此在查询的WHERE中引用的每一行上都可以检查它是否在上面的一行中使用过。
然后,我可以将该值作为在运行时动态插入到dataset中的新列返回。
https://stackoverflow.com/questions/31923548
复制相似问题