sql - 库存库存先进先出

标签 sql sql-server sql-server-2008

我有一个返回数据库中所有可用股票的查询。我 需要做一个存储过程来获取用户输入的具体商品的itemCode、batchNo、数量、价格。

----------------------------------------------
| id | itemCode | batchNo | availQty | price |
----------------------------------------------
| 1  | item_1   | 07292016|   5      |  5.50 |
| 2  | item_1   | 07312016|   10     |  5.50 |
| 3  | item_1   | 08012016|   2      |  6.00 |

我的问题是,如果用户输入了 6 个要购买的数量,我怎样才能得到结果的前 2 行以获得 6 个的总数量?

结果必须是:

07292016  --- 5
07312016  --- 1

最佳答案

我怀疑这是否有效并且查询很无聊,但以下内容将为您提供所需的内容:

-- Create Test Data
create table #Items
(
    id int not null primary key,
    itemCode varchar(30) not null,
    batchNumber varchar(30) not null,
    availQty int not null,
    price smallmoney not null
);

insert into #Items
values
    (1, 'item_1', '07292016', 5, 5.50),
    (2, 'item_1', '07312016', 10, 5.50),
    (3, 'item_1', '08012016', 2, 6.00)

select 
    *
from
    #Items
;

-- Set up required parameters
declare 
    @requiredItemCode varchar(30) = 'item_1',
    @requiredQty int = 6

-- The query to get the required result
select
    i.*,
    case
        when
            @requiredQty - 
            isnull(
                (
                    select
                        sum(availQty)
                    from
                        #Items i2
                    where
                        i2.itemCode = i.itemCode
                        and i2.id < i.Id
                ),
                0) < i.availQty
        then
            @requiredQty - 
            isnull(
                (
                    select
                        sum(availQty)
                    from
                        #Items i2
                    where
                        i2.itemCode = i.itemCode
                        and i2.id < i.Id
                ),
                0)
        else
            i.availQty
    end as qtyToTake
from 
    #Items i
where   
    i.ItemCode = @requiredItemCode
    and 
    case
        when
            @requiredQty - 
            isnull(
                (
                    select
                        sum(availQty)
                    from
                        #Items i2
                    where
                        i2.itemCode = i.itemCode
                        and i2.id < i.Id
                ),
                0) < i.availQty
        then
            @requiredQty - 
            isnull(
                (
                    select
                        sum(availQty)
                    from
                        #Items i2
                    where
                        i2.itemCode = i.itemCode
                        and i2.id < i.Id
                ),
                0)
        else
            i.availQty
    end > 0

-- Clean up test data
drop table #Items

输出:

id          itemCode batchNumber availQty    price
----------- -------- ----------- ----------- ---------------------
1           item_1   07292016    5           5.50
2           item_1   07312016    10          5.50
3           item_1   08012016    2           6.00

(3 row(s) affected)

id          itemCode batchNumber availQty    price                 qtyToTake
----------- -------- ----------- ----------- --------------------- -----------
1           item_1   07292016    5           5.50                  5
2           item_1   07312016    10          5.50                  1

(2 row(s) affected)

关于sql - 库存库存先进先出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38716665/

相关文章:

sql - Polybase EXTERNAL TABLE 访问失败 - 权限被拒绝

sql-server - 如何将我的 Microsoft SQL Database_Diagram 存储在 SSDT 项目中?

sql - 使用go in sql执行动态查询

sql - 查询的意义是什么?

mysql - 使用 SQL 计算平面表中事件之间的平均时间

mysql - Mysql中联合三张表

c# - Entity Framework GroupBy到Sql生成

python - 在 JetBrains PyCharm 中禁用 SQL 检测

tsql - 将表变量传递到动态SQL 2008中

sql - 删除列不会减少数据库大小