代码在更新当前月份记录时删除上个月的记录

时间:2013-05-16 20:32:20

标签: sql oracle oracle11g

使用11gR2,Windows 7客户端机器。我需要更新表missing_volume(下面),我需要计算estimated_missing列。当前月份的estimate_missing列的计算需要上个月的数字(在下面的代码中注释)。我希望输出像第一个表。请注意记录从1月开始,因此无法计算1月份的estimate_missing,但是对于剩余的月份,可以通过简单地更改“yr”和“mnth”(在代码内部向末尾注释)来完成。 / p>

yr      mnth        location    volume      actual_missing    expected_missing  estimated_missing

2013    January     loc1        48037       24                57                    
2013    February    loc1        47960       3660              53                24
2013    March       loc1        55007       78                57                28
2013    April       loc1        54345       72                58                77

代码:

UPDATE missing_volume g
    SET
        g.estimated_missing = 
        (
            SELECT

                CASE
                    -- If the previous month actual_missing number is less than 2 times than that of the previous month expected_missing number,
                    -- then for current month: 
                    -- estimated_missing = previous_month(actual_missing/volume)*current_month(volume)
                    -- 
                    -- Else, for current month:
                    -- estimated_missing = previous_month(estimated_missing/volume)*current_month(volume)

                    WHEN (NVL(p.actual_missing, 0)/NVL(p.expected_missing, 1)) <= 2.0
                        THEN ROUND(NVL(p.actual_missing, 0)*( NVL(g.volume, 0)/NVL(p.volume, 1) ), 0)
                    ELSE
                        ROUND(NVL(p.estimated_missing, 0)*( NVL(g.volume, 0)/NVL(p.volume, 1) ), 0)
                END                     

            FROM 
                (
                    SELECT
                        yr,
                        mnth,
                        loc_cd,
                        volume,
                        actual_missing,
                        expected_missing,   
                        estimated_missing
                    FROM missing_volume
                    WHERE yr = TRIM(TO_CHAR(ADD_MONTHS(SYSDATE, -2), 'YYYY'))   -- For February it is -4, for March it is -3, for April it is -2
                    AND UPPER(mnth) = UPPER(TRIM(TO_CHAR(ADD_MONTHS(SYSDATE, -2), 'Month')))  -- For February it is -4, for March it is -3, for April it is -2
                )p
            WHERE g.yr = TRIM(TO_CHAR(ADD_MONTHS(SYSDATE, -1), 'YYYY'))   -- For February it is -3, for March it is -2, for April it is -1
            AND UPPER(g.mnth) = UPPER(TRIM(TO_CHAR(ADD_MONTHS(SYSDATE, -1), 'Month')))  -- For February it is -3, for March it is -2, for April it is -1
            AND g.volume IS NOT NULL
            AND TRIM(p.location) = TRIM(g.location)                     
        );

当我运行每个月的代码时,代码确实计算了'estimated_missing'的正确数字,但问题是在更新当前月份时它也会删除上个月的记录。例如。从下面可以看出,在我更新4月之后,该列只有4月的记录,上个月的记录消失了,同样更新3月删除了2月等等。我无法理解为什么会发生这种情况!这是我得到的输出:

yr      mnth        location    volume      actual_missing    expected_missing  estimated_missing

2013    January     loc1        48037       24                57                    
2013    February    loc1        47960       3660              53                
2013    March       loc1        55007       78                57                
2013    April       loc1        54345       72                58                77

如果有任何机构可以解释它为什么会发生(我的意思是代码中的缺陷在哪里)以及如何获得所需的输出(第一个表格),我们将不胜感激。

由于

1 个答案:

答案 0 :(得分:0)

您需要为语句指定where子句。 在WHERE子句里面大括号的那一刻。你的陈述基本上是:

UPDATE missing_volume g
SET
    g.estimated_missing = 
    ( [...]
    ); -- no where clause here

需要的是:

UPDATE missing_volume g
SET
    g.estimated_missing = 
    ( [...]
    )
WHERE g.yr = TRIM(TO_CHAR(ADD_MONTHS(SYSDATE, -1), 'YYYY'));

Haven自己测试过,但这只应该更新当月的行。