SQL运行减法和偏差

时间:2011-05-30 21:24:47

标签: tsql sql-server-2000

--    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,'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. :) 
-- Conditions : any of row, we cant have ReceivedQty, DamageQty and ShortQty 
-- values more than ExpectedQty value. Item03 has this scenario
-- Query should run in SQL 2000 DB

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','20','10' ,'10'  UNION ALL 
SELECT 'ITEM03','PO-03','20','0','0','20' UNION ALL 
SELECT 'ITEM04','PO-01','30','25','0' ,'5'  

1 个答案:

答案 0 :(得分:0)

this solution为出发点,我最终得到了这个:

SELECT
  Item,
  PO,
  ExpectedQty,
  ReceivedQty = CASE
    WHEN RemainderQty >= 0 THEN ExpectedQty
    WHEN RemainderQty < -ExpectedQty THEN 0
    ELSE RemainderQty + ExpectedQty
  END,
  DamageQty = CASE
    WHEN RemainderQty >=0 OR ExpectedQty < -TotalRemainderQty THEN 0
    WHEN RemainderQty < -ExpectedQty AND TotalRemainderQty > 0 THEN ExpectedQty
    WHEN RemainderQty < -ExpectedQty AND TotalRemainderQty < -DamagedQty THEN ExpectedQty + TotalRemainderQty
    WHEN RemainderQty > -DamagedQty THEN -RemainderQty
    ELSE DamagedQty
  END,
  ShortQty = CASE
    WHEN TotalRemainderQty >= 0 THEN 0
    WHEN TotalRemainderQty < -ExpectedQty THEN ExpectedQty
    ELSE -TotalRemainderQty
  END
FROM (
  SELECT
    a.Item,
    a.PO,
    a.ExpectedQty,
    b.DamagedQty,
    RemainderQty = b.ReceivedQty - a.RunningTotalQty,
    TotalRemainderQty = b.ReceivedQty + b.DamagedQty - a.RunningTotalQty
  FROM (
    SELECT
      a.Item,
      a.PO,
      a.ExpectedQty,
      RunningTotalQty = SUM(a2.ExpectedQty)
    FROM (SELECT Item, PO, ExpectedQty FROM Temp WHERE STATUS IS NULL) AS a
      INNER JOIN (SELECT Item, PO, ExpectedQty FROM Temp WHERE STATUS IS NULL) AS a2
        ON a.Item = a2.Item AND a.PO >= a2.PO
    GROUP BY
      a.Item,
      a.PO,
      a.ExpectedQty
  ) a
    LEFT JOIN (
      SELECT
        Item,
        ReceivedQty = SUM(CASE STATUS WHEN 'OK' THEN ReceivedQty ELSE 0 END),
        DamagedQty = SUM(CASE STATUS WHEN 'DAMAGE' THEN ReceivedQty ELSE 0 END)
      FROM Temp
      GROUP BY Item
    ) b ON a.Item = b.Item
) s;