小编典典

SQL运行减法

sql

我有一个结果集如下:

Item    ExpectedQty  ReceivedQty  Short
Item01  30           45           5
Item01  20           45           5

Item02  40           38           2

item03  50           90           10
item03  30           90           10
item03  20           90           10

查询是:

select a.Item, a.ExpectedQty,b.ReceivedQty, b.Short
from a join b on a.Item = b.Item

我需要得到第二张图表中的结果。基本上,每行中我总有收到的数量,如果有短缺,我需要在最后一行中显示“预期数量”。

预期的:

Item    ExpectedQty  ReceivedQty  Short
item01  30           30           0
item01  20           15           5

item02  40           38           2

item03  50           50           0
item03  30           30           0
item03  20           10           10

提前致谢。

编辑,版本02;

--    Just a brief of business scenario is table has been created for a good receipt. 
--    So here we have good expected line with PurchaseOrder(PO) in first few line. 
--    And then we receive each expected line physically and that time these 
--    quantity may be different 
--    due to business case like quantity may damage and short quantity like that. 
--    So we maintain a status for that eg: OK, Damage, also we have to calculate
--    short quantity 
--    based on total of expected quantity of each item and total of received line.


if object_id('DEV..Temp','U') is not null
drop table Temp

CREATE TABLE Temp 
(        
ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,        
Item VARCHAR(32),
PO VARCHAR(32) NULL,        
ExpectedQty INT NULL,
ReceivedQty INT NULL,
[STATUS] VARCHAR(32) NULL,
BoxName VARCHAR(32) NULL
)


--  Please see first few line with PO data will be the expected lines, 
--  and then rest line will be received line

INSERT INTO TEMP (Item,PO,ExpectedQty,ReceivedQty,[STATUS],BoxName)
SELECT 'ITEM01','PO-01','30',NULL,NULL,NULL UNION ALL 
SELECT 'ITEM01','PO-02','20',NULL,NULL,NULL UNION ALL 
SELECT 'ITEM02','PO-01','40',NULL,NULL,NULL UNION ALL 
SELECT 'ITEM03','PO-01','50',NULL,NULL,NULL UNION ALL 
SELECT 'ITEM03','PO-02','30',NULL,NULL,NULL UNION ALL 
SELECT 'ITEM03','PO-03','20',NULL,NULL,NULL UNION ALL 
SELECT 'ITEM04','PO-01','30',NULL,NULL,NULL UNION ALL 
SELECT 'ITEM01',NULL,NULL,'20','OK','box01' UNION ALL 
SELECT 'ITEM01',NULL,NULL,'25','OK','box02' UNION ALL 
SELECT 'ITEM01',NULL,NULL,'5','DAMAGE','box03' UNION ALL 
SELECT 'ITEM02',NULL,NULL,'38','OK','box04' UNION ALL 
SELECT 'ITEM02',NULL,NULL,'2','DAMAGE','box05' UNION ALL 
SELECT 'ITEM03',NULL,NULL,'30','OK','box06' UNION ALL 
SELECT 'ITEM03',NULL,NULL,'30','OK','box07' UNION ALL 
SELECT 'ITEM03',NULL,NULL,'30','OK','box08' UNION ALL 
SELECT 'ITEM03',NULL,NULL,'10','DAMAGE','box09' UNION ALL
SELECT 'ITEM04',NULL,NULL,'25','OK','box10'



--  Below Table is my expected result based on above data. 
--  I need to show those data following way. 
--  So I appreciate if you can give me an appropriate query for it. 
--  Note: first row is blank and it is actually my table header. :)

SELECT  ''as'ITEM', ''as'PO#', ''as'ExpectedQty',''as'ReceivedQty',
''as'DamageQty' ,''as'ShortQty' UNION ALL

SELECT 'ITEM01','PO-01','30','30','0' ,'0'  UNION ALL 
SELECT 'ITEM01','PO-02','20','15','5' ,'0'  UNION ALL 
SELECT 'ITEM02','PO-01','40','38','2' ,'0'  UNION ALL 
SELECT 'ITEM03','PO-01','50','50','0' ,'0'  UNION ALL 
SELECT 'ITEM03','PO-02','30','30','0' ,'0'  UNION ALL 
SELECT 'ITEM03','PO-03','20','10','10','0' UNION ALL 
SELECT 'ITEM04','PO-01','30','25','0' ,'5'

阅读 264

收藏
2021-03-23

共1个答案

小编典典

问题的一部分是获取预期项目数量的总计。为此,您需要一种将具有相同物料的行彼此区分开的方法,以及用于排放相同物料数量的顺序的规则。

为了尝试解决您的问题,我将假设有一个时间戳列,其值提供放电顺序,并且在同一物料组中是唯一的。

这是我在以下解决方案上测试我的示例数据定义:

CREATE TABLE TableA (Item varchar(50), ExpectedQty int, Timestamp int);
INSERT INTO TableA
SELECT 'Item01', 30, 1 UNION ALL
SELECT 'Item01', 20, 2 UNION ALL
SELECT 'Item02', 40, 1 UNION ALL
SELECT 'item03', 50, 1 UNION ALL
SELECT 'item03', 30, 2 UNION ALL
SELECT 'item03', 20, 3;

CREATE TABLE TableB (Item varchar(50), ReceivedQty int);
INSERT INTO TableB
SELECT 'Item01', 45 UNION ALL
SELECT 'Item02', 38 UNION ALL
SELECT 'item03', 90;

这是我的解决方案:

SELECT
  Item,
  ExpectedQty,
  ReceivedQty = CASE
    WHEN RemainderQty >= 0 THEN ExpectedQty
    WHEN RemainderQty < -ExpectedQty THEN 0
    ELSE RemainderQty + ExpectedQty
  END,
  Short = CASE
    WHEN RemainderQty >= 0 THEN 0
    WHEN RemainderQty < -ExpectedQty THEN ExpectedQty
    ELSE ABS(RemainderQty)
  END
FROM (
  SELECT
    a.Item,
    a.ExpectedQty,
    RemainderQty = b.ReceivedQty - a.RunningTotalQty
  FROM (
    SELECT
      a.Item,
      a.Timestamp,
      a.ExpectedQty,
      RunningTotalQty = SUM(a2.ExpectedQty)
    FROM TableA a
      INNER JOIN TableA a AS a2 ON a.Item = a2.Item AND a.Timestamp >= a2.Timestamp
    GROUP BY
      a.Item,
      a.Timestamp,
      a.ExpectedQty
  ) a
    INNER JOIN TableB b ON a.Item = b.Item
) s
2021-03-23