数据丢失时插入表中

时间:2012-11-06 23:24:44

标签: sql oracle plsql oracle11g

我有下表,为了便于阅读,我为每个案例编号分解了每位经理。

最后一列是第二列的累计总和。

理想情况下,月份的价值应该从1-3开始(就像你看到芝加哥的两个案例一样)。

但从表中可以看出,在某些情况下,某些条目缺失(标记为< -----)。

CITY     CASE   CASE_NUMBER   MANAGER  MONTH  MONTHLY_TOTAL   FISCAL_TOTAL 
---------------------------------------------------------------------------
chicago  case_1   1             John       1         2              2     
chicago  case_1   1             John       2         3              5  
chicago  case_1   1             John       3         5              10

chicago  case_1   1             Jeff       1         4              4     
chicago  case_1   1             Jeff       2         2              6  
chicago  case_1   1             Jeff       3         3              9

chicago  case_2   2             John       1         3              3     
chicago  case_2   2             John       2         2              5  
chicago  case_2   2             John       3         4              9

chicago  case_2   2             Jeff       1         2              2     
chicago  case_2   2             Jeff       2         7              9 <----

newyork  case_1   1             Lee        1         3              3     
newyork  case_1   1             Lee        2         4              7 <----

newyork  case_1   1             Sue        1         2              2     
newyork  case_1   1             Sue        2         3              5     
newyork  case_1   1             Sue        3         2              7 

newyork  case_1   2             Lee        1         2              2     
newyork  case_1   2             Lee        2         4              6  
newyork  case_1   2             Lee        3         4              10  

newyork  case_1   2             Sue        1         3              3     
newyork  case_1   2             Sue        2         2              5 <----

我想要的是首先找出那些缺失的行并插入值。

对于那些缺少的人,month_total = 0

fiscal_total =上一行中的值。

E.g。对于第一个缺失的行应该是:

CITY     CASE   CASE_NUMBER   MANAGER  MONTH  MONTHLY_TOTAL   FISCAL_TOTAL 
---------------------------------------------------------------------------
chicago  case_2   2             Jeff       3         0              9

2 个答案:

答案 0 :(得分:0)

也许这可能会提供一些想法。我在Postgresql上运行它,但只需要对Oracle进行微小的更改。然后一些清理。这个想法是这样的:

  • 生成月份列表
  • 使用这组键交叉产品月份
  • 然后将结果返回到原始表中以获取值
  • 对运行总计使用分析函数

设定:

create table theTable(city varchar(64), theCase varchar(64), case_number int, manager varchar(64), month int, monthly_total int, fiscal_total int);


insert into theTable values('chicago', 'case_1', 1, 'John', 1, 2, 2);
insert into theTable values('chicago', 'case_1', 1, 'John', 2, 3, 5);
insert into theTable values('chicago', 'case_1', 1, 'John', 3, 5, 10);

insert into theTable values('chicago', 'case_1', 1, 'Jeff', 1, 4, 4);
insert into theTable values('chicago', 'case_1', 1, 'Jeff', 2, 2, 6);
insert into theTable values('chicago', 'case_1', 1, 'Jeff', 3, 3, 9);

insert into theTable values('chicago', 'case_2', 2, 'John', 1, 3, 3);
insert into theTable values('chicago', 'case_2', 2, 'John', 2, 2, 5);
insert into theTable values('chicago', 'case_2', 2, 'John', 3, 4, 9);

insert into theTable values('chicago', 'case_2', 2, 'Jeff', 1, 2, 2);
insert into theTable values('chicago', 'case_2', 2, 'Jeff', 2, 7, 9);

insert into theTable values('newyork', 'case_1', 1, 'Lee', 1, 3, 3);
insert into theTable values('newyork', 'case_1', 1, 'Lee', 2, 4, 7);

insert into theTable values('newyork', 'case_1', 1, 'Sue', 1, 2, 2);
insert into theTable values('newyork', 'case_1', 1, 'Sue', 2, 3, 5);
insert into theTable values('newyork', 'case_1', 1, 'Sue', 3, 2, 7);

insert into theTable values('newyork', 'case_1', 2, 'Lee', 1, 2, 2);
insert into theTable values('newyork', 'case_1', 2, 'Lee', 2, 4, 6);
insert into theTable values('newyork', 'case_1', 2, 'Lee', 3, 4, 10);

insert into theTable values('newyork', 'case_1', 2, 'Sue', 1, 3, 3);
insert into theTable values('newyork', 'case_1', 2, 'Sue', 2, 2, 5);

查询:

     -- the 3 months
with three as (select 1 AS month  union select 2 union select 3)
            -- select rownum from all_tables where rownum < 4  -- Oracle world

    -- the key
    ,city as (select distinct city, theCase, case_number, manager from theTable)

select x.city, x.theCase, x.case_number, x.manager, x.month, COALESCE(y.monthly_total,0)
      ,SUM(COALESCE(y.monthly_total,0)) OVER (PARTITION BY x.city, x.theCase, x.case_number, x.manager
                                              ORDER BY x.month) AS fiscal_total2
      ,y.fiscal_total

         -- cross product the key to form record for each month
  from ( select c.city, c.theCase, c.case_number, c.manager, t.month
           from city c
               ,three t
       ) x

  -- join "desired" set back to "have" set

  left outer join theTable y ON (    x.city = y.city
                                 and x.theCase = y.theCase
                                 and x.case_number = y.case_number
                                 and x.manager = y.manager
                                 and x.month = COALESCE(y.month, x.month)
                                )

  order by x.city, x.theCase, x.case_number, x.manager, x.month;

结果:

  city   | thecase | case_number | manager | month | coalesce | fiscal_total2 |fiscal_total
---------+---------+-------------+---------+-------+----------+---------------+--------------
 chicago | case_1  |           1 | Jeff    |     1 |        4 |             4 |           4
 chicago | case_1  |           1 | Jeff    |     2 |        2 |             6 |           6
 chicago | case_1  |           1 | Jeff    |     3 |        3 |             9 |           9
 chicago | case_1  |           1 | John    |     1 |        2 |             2 |           2
 chicago | case_1  |           1 | John    |     2 |        3 |             5 |           5
 chicago | case_1  |           1 | John    |     3 |        5 |            10 |          10
 chicago | case_2  |           2 | Jeff    |     1 |        2 |             2 |           2
 chicago | case_2  |           2 | Jeff    |     2 |        7 |             9 |           9
 chicago | case_2  |           2 | Jeff    |     3 |        0 |             9 |
 chicago | case_2  |           2 | John    |     1 |        3 |             3 |           3
 chicago | case_2  |           2 | John    |     2 |        2 |             5 |           5
 chicago | case_2  |           2 | John    |     3 |        4 |             9 |           9
 newyork | case_1  |           1 | Lee     |     1 |        3 |             3 |           3
 newyork | case_1  |           1 | Lee     |     2 |        4 |             7 |           7
 newyork | case_1  |           1 | Lee     |     3 |        0 |             7 |
 newyork | case_1  |           1 | Sue     |     1 |        2 |             2 |           2
 newyork | case_1  |           1 | Sue     |     2 |        3 |             5 |           5
 newyork | case_1  |           1 | Sue     |     3 |        2 |             7 |           7
 newyork | case_1  |           2 | Lee     |     1 |        2 |             2 |           2
 newyork | case_1  |           2 | Lee     |     2 |        4 |             6 |           6
 newyork | case_1  |           2 | Lee     |     3 |        4 |            10 |          10
 newyork | case_1  |           2 | Sue     |     1 |        3 |             3 |           3
 newyork | case_1  |           2 | Sue     |     2 |        2 |             5 |           5
 newyork | case_1  |           2 | Sue     |     3 |        0 |             5 |
(24 rows)

答案 1 :(得分:0)

假设每个案例的每个经理都应该有三个条目,你可以尝试类似:

SELECT City, Case, Manager, COUNT(Manager) AS CASE_COUNT
FROM table(put actual table name)
GROUP BY City, Case, Manager

CASE_COUNT列将显示经理所处理的每个案例表格中的条目,如果该数字小于3,您知道缺少一个条目(当然这假设应该有每个人都有3个条目。)

相关问题