有条件地相加/相减

时间:2018-09-12 17:48:18

标签: sql sql-server-2014

我有两个表(PS_BU_ITEMS_INV APS_TRANSACTION_INV B)用于库存项目管理。 PS_BU_ITEMS_INV A包含一个字段,其中包含物料A.QTY_ONHAND的当前现有数量。表PS_TRANSACTION_INV B是一个事务表,其中列出了所有A.QTY_ONHAND表中PS_BU_ITEMS_INV A值从称为{的字段中减少或增加的值(装运,消耗等)。 {1}}。

存储在B.QTY_BASE列中的值都是正数,因此我必须查看另一列称为B.QTY_BASE的列,以确定是否应将B.TRANSACTION_GROUP中的数量添加到当前值中或从当前值中减去B.QTY_BASE值。

下面是每个表的示例数据

PS_BU_ITEMS_INV:

A.QTY_ONHAND

结果:

  SELECT BUSINESS_UNIT, INV_ITEM_ID, QTY_ONHAND, DT_TIMESTAMP 
  FROM PS_BU_ITEMS_INV
  WHERE INV_ITEM_ID = '1'
  AND BUSINESS_UNIT = '11MMS'

PS_TRANSATION_INV:

 BUSINESS_UNIT   INV_ITEM_ID    QTY_ONHAND    DT_TIMESTAMP
 11MMS           1              16.0000       2018-09-11 08:12:46.827

结果:

enter image description here

假设我想确定9月4日的 SELECT BUSINESS_UNIT, INV_ITEM_ID, TRANSACTION_DATE, TRANSACTION_GROUP, QTY_BASE, DT_TIMESTAMP FROM PS_TRANSACTION_INV WHERE INV_ITEM_ID = '1' AND BUSINESS_UNIT = '11MMS' ORDER BY DT_TIMESTAMP DESC 值是多少;我将从QTY_ONHAND表中获取当前的QTY_ONHAND值(在本例中为16.0,然后从PS_BU_ITEMS_INV中加上(求和)与Transaction_Group相关的PS_TRANSACTION_INV值)QTY_BASE(这些是项目耗竭),并用'030', and '036'

减去数量

因此,对于TRANSATION_GROUP = '020',从9/4/18开始计算:16 +(总和->(14 + 2 + 2 + 5 + 2 + 1 + 2 + 10-24 + 10 + 10-24)= 26

到目前为止,我已经将以下查询写入ON_QTY的数量(对于今天(当前日期)与硬编码日期(9/4/18)之间发生的交易的030和036(正)数量此示例),但我在如何添加某种类型的SUM或条件性CASE WHEN语句方面颇为挣扎,其中如果IF是030或036,则将这些值加在一起,如果TRANSACTION_GROUP是020,然后减去这些值。由于缺少条件减法,该查询当前无法从9/4/18获得正确的ON_HAND_QTY。

TRANSACTION_GROUP

我想对此有所帮助:

  SELECT A.INV_ITEM_ID, A.BUSINESS_UNIT, A.QTY_ONHAND, A.DT_TIMESTAMP, 
  DATEADD(MINUTE, DATEDIFF(MINUTE, 0, B.DT_TIMESTAMP), 0) AS TRANSACTION_DT,
  (A.QTY_ONHAND + (SELECT SUM(BB.QTY_BASE) 
                  FROM PS_TRANSACTION_INV BB WHERE BB.TRANSACTION_DATE 
                  BETWEEN '2018-09-04' AND GETDATE() AND BB.INV_ITEM_ID  
                  = '1' 
                  AND BB.BUSINESS_UNIT = '11MMS'  
                  AND BB.BUSINESS_UNIT = B.BUSINESS_UNIT AND BB.INV_ITEM_ID 
                  = B.INV_ITEM_ID)) AS QTY_ONHAND_AS_OF_DT

 FROM PS_BU_ITEMS_INV A
  LEFT OUTER JOIN PS_TRANSACTION_INV B ON B.BUSINESS_UNIT
  = A.BUSINESS_UNIT 
    AND B.INV_ITEM_ID = A.INV_ITEM_ID 
    AND DATEADD(MINUTE, DATEDIFF(MINUTE, 0, A.DT_TIMESTAMP), 0) = 
     DATEADD(MINUTE, DATEDIFF(MINUTE, 0, B.DT_TIMESTAMP), 0)
  WHERE B.INV_ITEM_ID = '1'
   AND B.BUSINESS_UNIT = '11MMS'

此查询虽然无效。感谢您提供任何反馈意见。

编辑:

这是我编辑的SQL,但是它返回了错误的有条件计算的现有值。我什至在日期范围的SELECT A.INV_ITEM_ID, A.BUSINESS_UNIT, A.QTY_ONHAND, A.DT_TIMESTAMP, DATEADD(MINUTE, DATEDIFF(MINUTE, 0, B.DT_TIMESTAMP), 0) AS TRANSACTION_DT, (A.QTY_ONHAND + (CASE WHEN B.TRANSACTION_GROUP IN ('030', '036') THEN SUM(B.QTY_BASE) WHEN B.TRANSACTION_GROUP = '020' THEN SUM(-B.QTY_BASE) END AS ON_HANDS_AS_OF_DATE ) ) FROM PS_BU_ITEMS_INV A LEFT OUTER JOIN PS_TRANSACTION_INV B ON B.BUSINESS_UNIT = A.BUSINESS_UNIT AND B.INV_ITEM_ID = A.INV_ITEM_ID AND DATEADD(MINUTE, DATEDIFF(MINUTE, 0, A.DT_TIMESTAMP), 0) = DATEADD(MINUTE, DATEDIFF(MINUTE, 0, B.DT_TIMESTAMP), 0) WHERE B.INV_ITEM_ID = '1' AND B.BUSINESS_UNIT = '11MMS' AND B.TRANSACTION_DATE BETWEEN '2018-09-04' AND GETDATE() 中添加了其他条件,但它仍未返回正确的值(26)。

CASE WHEN

编辑2:

我很好奇为什么此查询可以获取正确的数量计数(26)(为简化起见,我从A.QTY_ONHAND硬编码了该值)

SELECT 
A.INV_ITEM_ID, A.BUSINESS_UNIT, A.QTY_ONHAND, A.DT_TIMESTAMP, 
  DATEADD(MINUTE, DATEDIFF(MINUTE, 0, B.DT_TIMESTAMP), 0) AS TRANSACTION_DT,
  (A.QTY_ONHAND + SUM(CASE WHEN B.TRANSACTION_GROUP IN ('030', '036') --AND B.TRANSACTION_DATE BETWEEN '2018-09-04' AND GETDATE()  
                      THEN (B.QTY_BASE)
                        WHEN B.TRANSACTION_GROUP = '020' --AND B.TRANSACTION_DATE BETWEEN '2018-09-04' AND GETDATE()  
                        THEN (-B.QTY_BASE) END   ) )
FROM PS_BU_ITEMS_INV A
  LEFT OUTER JOIN PS_TRANSACTION_INV B ON B.BUSINESS_UNIT
  = A.BUSINESS_UNIT 
    AND B.INV_ITEM_ID = A.INV_ITEM_ID 
    AND DATEADD(MINUTE, DATEDIFF(MINUTE, 0, A.DT_TIMESTAMP), 0) = 
     DATEADD(MINUTE, DATEDIFF(MINUTE, 0, B.DT_TIMESTAMP), 0)
  WHERE B.INV_ITEM_ID = '1'
   AND B.BUSINESS_UNIT = '11MMS'
   AND B.TRANSACTION_DATE BETWEEN '2018-09-04' AND GETDATE()
GROUP BY A.INV_ITEM_ID, A.BUSINESS_UNIT, A.QTY_ONHAND, A.DT_TIMESTAMP, DATEADD(MINUTE, DATEDIFF(MINUTE, 0, B.DT_TIMESTAMP), 0) 

但是下面的查询(使用相同的条件逻辑)为我提供了不同的(不正确的)数量吗?

SELECT  (32 + SUM(CASE WHEN B.TRANSACTION_GROUP IN ('030', '036') --AND B.TRANSACTION_DATE BETWEEN '2018-09-04' AND GETDATE()  THEN 
                        THEN (B.QTY_BASE)
                        WHEN B.TRANSACTION_GROUP = '020' --AND B.TRANSACTION_DATE BETWEEN '2018-09-04' AND GETDATE()  
                        THEN (-B.QTY_BASE) END   ) )
        FROM PS_TRANSACTION_INV B
        WHERE B.INV_ITEM_ID = '1'
   AND B.BUSINESS_UNIT = '11MMS'
   AND B.TRANSACTION_DATE BETWEEN '2018-09-04' AND GETDATE()

1 个答案:

答案 0 :(得分:1)

我不会调试您的确切查询。不过,下面您将看到如何有条件地对值求和并将其减去当前值,以便可以及时返回并找到原始数量:

假设您的数据是:

create table ps_bu_items_inv (
  business_unit varchar(20),
  inv_item_id int,
  qty_onhand decimal(12, 8),
  dt_timestamp datetime
);

insert into ps_bu_items_inv (business_unit, inv_item_id, qty_onhand, dt_timestamp)
  values ('11MMS', 1, 16.0, '2018-09-11 08:12:46.827');

create table ps_transaction_inv (
  business_unit varchar(20),
  inv_item_id int,
  transaction_date date,
  transaction_group varchar(6),
  qty_base decimal(12, 8),
  dt_timestamp datetime
);

insert into ps_transaction_inv (business_unit, inv_item_id, transaction_date, transaction_group, qty_base, dt_timestamp)
  values ('11MMS', 1, '2018-09-11', '036', 14, '2018-09-11 12:34:56');
insert into ps_transaction_inv (business_unit, inv_item_id, transaction_date, transaction_group, qty_base, dt_timestamp)
  values ('11MMS', 1, '2018-09-11', '020', 24, '2018-09-11 12:34:56');
insert into ps_transaction_inv (business_unit, inv_item_id, transaction_date, transaction_group, qty_base, dt_timestamp)
  values ('11MMS', 1, '2018-09-11', '030', 6, '2018-09-11 12:34:56');

查询应类似于:

select a.*, b.*, (a.qty_onhand - b.change) as original_qty
  from ps_bu_items_inv a
  join (
    select
        inv_item_id,
        sum( -- here's the conditional sum
           case when transaction_group <> '020' then -qty_base else qty_base end
           ) as change
      from ps_transaction_inv
      where transaction_date between '2018-09-01' and '2018-09-11'
      group by inv_item_id
  ) b on a.inv_item_id = b.inv_item_id

结果:

business_unit  inv_item_id  qty_onhand  dt_timestamp  inv_item_id  change  original_qty
-------------  -----------  ----------  ------------  -----------  ------  ------------
11MMS          1            16          2018-09-11    1            4       12

如您所见,原始数量显示为12。那是当前的16减去更改4。在这种情况下,“更改”的计算公式为:

-14 +24 -6 = +4