SQL选择剩余的库存行

SQL选择剩余的库存行,sql,sql-server,Sql,Sql Server,我需要有关库存表的帮助,此查询应选择库存表中的其余行: timestamp Prod_id Loc_ID Price Buy Sell 2017/2/21 12:24:00 50 A 10 1 0 2017/2/22 13:15:00 50 A 10 2 0 2017/2/23 14:00:00 50 A 12 0 2 2017/2/24 12:

我需要有关库存表的帮助,此查询应选择库存表中的其余行:

timestamp          Prod_id  Loc_ID  Price   Buy Sell
2017/2/21 12:24:00  50          A   10      1     0
2017/2/22 13:15:00  50          A   10      2     0
2017/2/23 14:00:00  50          A   12      0     2
2017/2/24 12:20:00  55          B   2       1     0
2017/2/25 10:04:00  55          B   2       1     0   
2017/2/26 11:44:00  55          B   5       0     3
2017/2/27 15:22:00  60          C   3       5     0
2017/2/28 16:24:00  60          C   4       0     5
timestamp           ord_label   prod_id Loc_ID  price   buy sell
2017/2/22 12:24:00  30411           54  72      52      2   0
2017/2/23 12:24:00  30412           54  72      53      2   0
2017/2/24 12:24:00  30413           54  72      55      0   3
2017/2/25 12:25:00  30414           56  70      42      2   0
2017/2/25 12:34:00  30415           56  70      32      2   0
2017/2/26 12:24:00  30416           56  70      62      0   4
2017/2/27 13:34:00  30417           56  77      2       2   0
2017/2/27 14:24:00  30418           56  77      4       0   1
2017/2/27 14:25:00  30419           60  80      4       0   1
选择结果应如下所示:

timestamp          Prod_id  Loc_ID  Price   Buy Sell
2017/2/22 13:15:00  50          A   10      1     0
2017/2/26 11:44:00  55          B   5       0     1
以下是此表的代码:

CREATE TABLE dbo.inventory
    (
      timestamp  DATETIME2 DEFAULT (getdate()) NOT NULL
    , Prod_id    INT NOT NULL
    , Loc_ID     nvarchar(3)
    , Price      INT NOT NULL
    , Buy      INT NOT NULL
    , Sell      INT NOT NULL
    )

INSERT INTO dbo.inventory
VALUES  ('2017-02-21 12:24:00',50,'A',10,1,0)

INSERT INTO dbo.inventory
VALUES  ('2017-02-22 13:15:00',50,'A',10,2,0)

INSERT INTO dbo.inventory
VALUES  ('2017-02-23 14:00:00',50,'A',12,0,2)

INSERT INTO dbo.inventory
VALUES  ('2017-02-24 12:20:00',55,'B',2,1,0)

INSERT INTO dbo.inventory
VALUES  ('2017-02-25 10:04:00',55,'B',2,1,0)

INSERT INTO dbo.inventory
VALUES  ('2017-02-26 11:44:00',55,'B',5,0,3)

INSERT INTO dbo.inventory
VALUES  ('2017-02-27 15:22:00',60,'C',3,5,0)

INSERT INTO dbo.inventory
VALUES  ('2017-02-28 16:24:00',60,'C',4,0,5)

目录2 此查询应选择库存表中的剩余行:

timestamp          Prod_id  Loc_ID  Price   Buy Sell
2017/2/21 12:24:00  50          A   10      1     0
2017/2/22 13:15:00  50          A   10      2     0
2017/2/23 14:00:00  50          A   12      0     2
2017/2/24 12:20:00  55          B   2       1     0
2017/2/25 10:04:00  55          B   2       1     0   
2017/2/26 11:44:00  55          B   5       0     3
2017/2/27 15:22:00  60          C   3       5     0
2017/2/28 16:24:00  60          C   4       0     5
timestamp           ord_label   prod_id Loc_ID  price   buy sell
2017/2/22 12:24:00  30411           54  72      52      2   0
2017/2/23 12:24:00  30412           54  72      53      2   0
2017/2/24 12:24:00  30413           54  72      55      0   3
2017/2/25 12:25:00  30414           56  70      42      2   0
2017/2/25 12:34:00  30415           56  70      32      2   0
2017/2/26 12:24:00  30416           56  70      62      0   4
2017/2/27 13:34:00  30417           56  77      2       2   0
2017/2/27 14:24:00  30418           56  77      4       0   1
2017/2/27 14:25:00  30419           60  80      4       0   1
结果应该是:

timestamp               ord_label   prod_id Loc_ID  price   buy sell
2017/2/23 12:24:00      30412       54      72      53      1   0
2017/2/27 13:34:00      30417       56      77      2       1   0
2017/2/27 14:25:00      30419       60      80      4       0   1
这些是最近的行

(SQL代码)


使用条件聚合尝试此操作:

select case when sum(buy) > sum(sell) then max(case when buy > 0 then timestamp end) else max(case when sell > 0 then timestamp end) end as timestamp,
    prod_id,
    Loc_ID,
    case when sum(buy) > sum(sell) then max(case when buy > 0 then price end) else max(case when sell > 0 then price end) end as price,
    case when sum(buy) > sum(sell) then sum(buy) - sum(sell) else 0 end as buy,
    case when sum(sell) > sum(buy) then sum(sell) - sum(buy) else 0 end as sell
from dbo.inventory
group by prod_id,
    Loc_ID
having sum(buy) - sum(sell) <> 0;
选择sum(buy)时的case>sum(sell)然后max(buy>0时的case然后timestamp end)否则max(sell>0时的case然后timestamp end)作为时间戳结束,
产品id,
地址:,
当总和(买入)>总和(卖出)然后最大值时的情况(买入>0然后价格结束时的情况)否则最大值(卖出>0然后价格结束时的情况)作为价格结束,
当sum(buy)>sum(sell)然后sum(buy)–sum(sell)否则0结束为buy时,
当sum(sell)>sum(buy)然后sum(sell)-sum(buy)else 0结束为sell时的情况
来自dbo.inventory
按产品id分组,
地址
金额(买入)-金额(卖出)为0;
产生:

编辑: 对于问题中的最新编辑,您可以在选择中添加另一个案例陈述:

select case when sum(buy) > sum(sell) then max(case when buy > 0 then timestamp end) else max(case when sell > 0 then timestamp end) end as timestamp,
    case when sum(buy) > sum(sell) then max(case when buy > 0 then ord_label end) else max(case when sell > 0 then ord_label end) end as ord_label,
    prod_id,
    Loc_ID,
    case when sum(buy) > sum(sell) then max(case when buy > 0 then price end) else max(case when sell > 0 then price end) end as price,
    case when sum(buy) > sum(sell) then sum(buy) - sum(sell) else 0 end as buy,
    case when sum(sell) > sum(buy) then sum(sell) - sum(buy) else 0 end as sell
from dbo.inventory2
group by prod_id,
    Loc_ID
having sum(buy) - sum(sell) <> 0;
选择sum(buy)时的case>sum(sell)然后max(buy>0时的case然后timestamp end)否则max(sell>0时的case然后timestamp end)作为时间戳结束,
当sum(buy)>sum(sell)然后max时的情况(当buy>0时的情况,然后ord_标签结束)否则max(当sell>0时的情况,然后ord_标签结束)作为ord_标签结束,
产品id,
地址:,
当总和(买入)>总和(卖出)然后最大值时的情况(买入>0然后价格结束时的情况)否则最大值(卖出>0然后价格结束时的情况)作为价格结束,
当sum(buy)>sum(sell)然后sum(buy)–sum(sell)否则0结束为buy时,
当sum(sell)>sum(buy)然后sum(sell)-sum(buy)else 0结束为sell时的情况
来自dbo.inventory2
按产品id分组,
地址
金额(买入)-金额(卖出)为0;
产生:

工作原理:
当卖出或买入的金额较大时,它从这些行中获取价值。例如,如果buy total大于sell total,那么我们将从非零buy的行中获取ord_标签的最大值。

因此您希望该表中每个产品的最新行,但在buy和sell列中,它将显示所需的库存水平(购买或出售所需的数量)?mssql server 2012 Hi请查看Inventory 2。GurV解决了第一个问题。嗨,GurV,谢谢你对这个表的查询。我有另一个表Inventory2,它还有一列“ord_标签”,那么这个查询就不起作用了。你能给我看看新的查询吗?如果有时间,请解释查询正在做什么。。。想要了解它;)@弗兰基-更新了你最新的编辑的答案,并添加了一些解释。@弗兰基-请考虑标记答案,如果它是有用的。但是没有义务这样做