SQL运行减法

时间:2011-05-03 08:23:18

标签: tsql sql-server-2000

只是简要介绍一下业务情景,就是为好收据创建了表格。所以在这里,我们在前几行中与PurchaseOrder(PO)有良好的预期线。然后我们在物理上接收每条预期的线路,并且那个时间这些数量可能会有所不同,因为像数量这样的商业案例可能会造成损坏和数量不足。所以我们保持这样的状态,例如:OK,损坏,我们还必须根据每个项目的预期数量和接收线路总数计算短期数量。

    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
    )

请查看前几行与PO数据将是预期的行, 然后休息行将收到行

    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' 

下表是我基于以上数据的预期结果。 我需要按照以下方式显示这些数据。 如果你能给我一个合适的查询,我很感激。 注意:第一行是空白的,它实际上是我的表头。 :)

    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'  

注意:我们收到的不是预期的。        解决方案应该基于SQL 2000

1 个答案:

答案 0 :(得分:1)

您应该重新考虑如何存储这些数据。在不同的表中分隔ExpectedReceived+Damaged(您有许多未使用的(null)单元格)。这样任何查询都应该更具可读性。

我认为使用存储过程可以更轻松地实现您尝试做的事情。

无论如何,请尝试此查询:

SELECT Item, PO, ExpectedQty, 
       CASE WHEN [rec-consumed] > 0 THEN ExpectedQty 
            ELSE CASE WHEN [rec-consumed] + ExpectedQty > 0
                      THEN [rec-consumed] + ExpectedQty
                      ELSE 0
                 END
       END ReceivedQty,
       CASE WHEN [rec-consumed] < 0
                 THEN CASE WHEN DamageQty >= -1*[rec-consumed]
                           THEN -1*[rec-consumed]
                           ELSE DamageQty
                      END
            ELSE 0
       END DamageQty,
       CASE WHEN [rec_damage-consumed] < 0
            THEN DamageQty - [rec-consumed]
            ELSE 0
       END ShortQty
FROM (
    select t1.Item,
           t1.PO,
           t1.ExpectedQty,
           st.sum_ReceivedQty_OK 
                  - (sum(COALESCE(t2.ExpectedQty,0)) 
                     +t1.ExpectedQty)
               [rec-consumed],
           st.sum_ReceivedQty_OK + st.sum_ReceivedQty_DAMAGE
                  - (sum(COALESCE(t2.ExpectedQty,0))
                     +t1.ExpectedQty)
               [rec_damage-consumed],
           st.sum_ReceivedQty_DAMAGE DamageQty
    from #tt t1
    left join #tt t2 on t1.Item = t2.Item
                    and t1.PO > t2.PO
                    and t2.PO is not null
    join (select Item
           , sum(CASE WHEN status = 'OK' THEN ReceivedQty ELSE 0 END)
                 sum_ReceivedQty_OK
           , sum(CASE WHEN status != 'OK' THEN ReceivedQty ELSE 0 END)
                 sum_ReceivedQty_DAMAGE
        from #tt where PO is null
        group by Item) st on t1.Item = st.Item
    where t1.PO is not null
    group by t1.Item, t1.PO, t1.ExpectedQty,
             st.sum_ReceivedQty_OK,
             st.sum_ReceivedQty_DAMAGE
) a
order by Item, PO