SQL选择剩余的库存行

时间:2017-03-19 03:10:01

标签: 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: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          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)

Inventory2 此查询应从库存表中选择剩余的行:

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代码)

CREATE TABLE dbo.inventory2
    (
      timestamp DATETIME2 DEFAULT (getdate()) NOT NULL
    , Ord_label INT 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.inventory2
VALUES  ('2017-02-22 12:24:00',30411,54,'72',52,2,0)

INSERT INTO dbo.inventory2
VALUES  ('2017-02-23 12:24:00',30412,54,'72',53,2,0)

INSERT INTO dbo.inventory2
VALUES  ('2017-02-24 12:24:00',30413,54,'72',55,0,3)

INSERT INTO dbo.inventory2
VALUES  ('2017-02-25 12:25:00',30414,56,'70',42,2,0)

INSERT INTO dbo.inventory2
VALUES  ('2017-02-25 12:34:00',30415,56,'70',32,2,0)

INSERT INTO dbo.inventory2
VALUES  ('2017-02-26 12:24:00',30416,56,'70',62,0,4)

INSERT INTO dbo.inventory2
VALUES  ('2017-02-27 13:34:00',30417,56,'77',2,2,0)

INSERT INTO dbo.inventory2
VALUES  ('2017-02-27 14:24:00',30418,56,'77',4,0,1)

INSERT INTO dbo.inventory2
VALUES  ('2017-02-27 14:25:00',30419,60,'80',4,0,1)

1 个答案:

答案 0 :(得分: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,
    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;

产地:

enter image description here

Demo

编辑:

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

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;

产地:

enter image description here

Demo

工作原理:

当出售或购买的金额更大时,它取得那些行的价值。如果买入总额超过卖出总额,那么,例如,我们将从非零买入的行中获取最大的ord_label。