从已在

时间:2015-08-10 15:30:04

标签: sql-server sql-server-2008

我正在编写一个查询,用于填充用于将库存存入仓库的报告。

该报告有3个参数,即源库存位置,源仓库编号和目标库存位置。

该股票目前将保存在源股票仓位和仓位编号中。

目标库存位置是需要将库存移动到的位置。

每个Variant在每个库存位置都有一个默认的箱号。 多个变体可以具有相同的默认bin编号。

仓库周围的箱号可能不是按字母顺序排列的,因此每个箱号都被分配了一条步行路线,作为仓库周围最有效的步行路线。

报告将查看与目标库存位置中的项目关联的默认箱号,如果为空,则将其作为收起建议提供。 如果默认的bin编号不为空,它将查找目标库存位置中的下一个可用空仓(步行路径高于默认仓位),然后将其作为收起建议提供。

查询工作正常,并且正是如此,但它报告的内容与之前的#Be; NextBinNo"相同。以上行中的建议。

如何让OUTER APPLY NextBinNo过滤掉更高行数据中之前建议的二进制文件?此外,如果两个项目具有相同的默认bin编号,则应使用NextBinNo作为第二行的默认bin编号。

我当前的查询:

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作为默认bin有库存。 Row3也建议使用AA08A2作为下一个bin。

第6行目前建议使用AA01A2,但已在第1行中提出建议。

enter image description here

1 个答案:

答案 0 :(得分:0)

回答这个问题,最终我无法直接在SQL中实现我想要的东西。

数据最终返回给可以运行Visual Basic代码的报表编写器。

我必须在VB中单独执行NextBinNo子查询。

在VB中我可以定义一个字符串,其中包含所有“已使用”的bin编号的列表,所以在查询的WHERE中引用每行,以检查它是否已连续使用上方。

然后我可以将此值作为在运行时动态插入数据集中的新列返回。