1

私は次の表を持っています。読みやすくするために、すべてのケース番号ごとにマネージャーごとに分類しました。

最後の列は、最後から 2 番目の列の累積合計です。

月の値は、理想的には 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 <----

私が望むのは、最初に欠落している行を見つけて値を挿入することです。

不足しているものの場合、monthly_total = 0

会計合計 = 前の行の値。

たとえば、最初の行が欠落している場合は、次のようになります。

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

2 に答える 2

0

各ケースの各マネージャーに3つのエントリがあると仮定すると、次のように試すことができます。

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

CASE_COUNT列には、マネージャーが取り組んだ各ケースのテーブルにエントリがどのようにあるかが表示されます。その数が3未満の場合は、エントリが欠落していることがわかります(もちろん、これは3つのエントリがあることを前提としています)。それらのそれぞれについて)。

于 2012-11-07T15:47:57.733 に答える
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)
于 2012-11-07T03:30:51.883 に答える