2

3 つのテーブル間の結合に基づく合計に問題があります。以下の例では、Ref には 1 か月あたり 11 のエントリが含まれています。Errors は、error_count 以外のすべての列による集計です。アイテムは、item_count 以外のすべての列による集計です。Errors と items の間のキーは、id と month year です。Ref と Errors の間のキーは、error_code と month_year です。Ref と Items の間のキーは month_year です。

Ref の元の各エントリに基づいて作成された Result のエントリ、つまり、月ごと、年ごと、ID ごとに 11 エントリを表示したいと思います。以下を実行すると、Ref と Errors の一致が見つからない場合、エントリは作成されませんが、Items への結合に基づいて total_item_count に実際の合計を出力することもできるため、その場合は total_error_count をゼロに出力したいと考えています。

id = 1 で 10 行、id = 2 で 27 行が欠落しています。アドバイスをお願いします。

SQL フィドル

Oracle 11g R2 スキーマのセットアップ:

create table Ref(
  month_year varchar2(6),
  error_code number(2)
)
/

create table Errors(
  id number(18),
  month_year varchar2(6),
  error_code number(2),
  include_ind varchar2(1),
  exclude_ind varchar2(1),
  error_count number
  )
/

create table Items(
  id number(18),
  month_year varchar2(6),
  include_ind varchar2(1),
  exclude_ind varchar2(1),
  item_count number
  )
/
create table Result(
  id number(18),
  error_code number(2),
  partition varchar2(10),
  month_year varchar2(6),
  total_error_count number,
  total_item_count number,
  rate number,
  query_timestamp varchar2(19)
)
/
INSERT INTO ref(month_year,error_code) VALUES ('201212','11');
INSERT INTO ref(month_year,error_code) VALUES ('201212','12');
INSERT INTO ref(month_year,error_code) VALUES ('201212','13');
INSERT INTO ref(month_year,error_code) VALUES ('201212','14');
INSERT INTO ref(month_year,error_code) VALUES ('201212','16');
INSERT INTO ref(month_year,error_code) VALUES ('201212','17');
INSERT INTO ref(month_year,error_code) VALUES ('201212','3');
INSERT INTO ref(month_year,error_code) VALUES ('201212','4');
INSERT INTO ref(month_year,error_code) VALUES ('201212','5');
INSERT INTO ref(month_year,error_code) VALUES ('201212','6');
INSERT INTO ref(month_year,error_code) VALUES ('201212','8');
INSERT INTO ref(month_year,error_code) VALUES ('201301','11');
INSERT INTO ref(month_year,error_code) VALUES ('201301','12');
INSERT INTO ref(month_year,error_code) VALUES ('201301','13');
INSERT INTO ref(month_year,error_code) VALUES ('201301','14');
INSERT INTO ref(month_year,error_code) VALUES ('201301','16');
INSERT INTO ref(month_year,error_code) VALUES ('201301','17');
INSERT INTO ref(month_year,error_code) VALUES ('201301','3');
INSERT INTO ref(month_year,error_code) VALUES ('201301','4');
INSERT INTO ref(month_year,error_code) VALUES ('201301','5');
INSERT INTO ref(month_year,error_code) VALUES ('201301','6');
INSERT INTO ref(month_year,error_code) VALUES ('201301','8');
INSERT INTO ref(month_year,error_code) VALUES ('201302','11');
INSERT INTO ref(month_year,error_code) VALUES ('201302','12');
INSERT INTO ref(month_year,error_code) VALUES ('201302','13');
INSERT INTO ref(month_year,error_code) VALUES ('201302','14');
INSERT INTO ref(month_year,error_code) VALUES ('201302','16');
INSERT INTO ref(month_year,error_code) VALUES ('201302','17');
INSERT INTO ref(month_year,error_code) VALUES ('201302','3');
INSERT INTO ref(month_year,error_code) VALUES ('201302','4');
INSERT INTO ref(month_year,error_code) VALUES ('201302','5');
INSERT INTO ref(month_year,error_code) VALUES ('201302','6');
INSERT INTO ref(month_year,error_code) VALUES ('201302','8');
INSERT INTO items(id,month_year,include_ind,exclude_ind,item_count) VALUES ('1','201212','Y','N','30');
INSERT INTO items(id,month_year,include_ind,exclude_ind,item_count) VALUES ('1','201301','Y','N','30');
INSERT INTO items(id,month_year,include_ind,exclude_ind,item_count) VALUES ('1','201302','Y','N','30');
INSERT INTO items(id,month_year,include_ind,exclude_ind,item_count) VALUES ('2','201212','Y','N','30');
INSERT INTO items(id,month_year,include_ind,exclude_ind,item_count) VALUES ('2','201301','Y','N','30');
INSERT INTO items(id,month_year,include_ind,exclude_ind,item_count) VALUES ('2','201302','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201212','3','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201212','4','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201212','5','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201212','6','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201212','11','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201212','12','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201212','13','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201212','14','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201301','3','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201301','5','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201301','6','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201301','11','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201301','12','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201301','13','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201301','14','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201302','3','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201302','4','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201302','5','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201302','6','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201302','11','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201302','12','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201302','13','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('1','201302','14','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('2','201212','3','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('2','201212','6','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('2','201301','3','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('2','201301','6','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('2','201302','3','Y','N','30');
INSERT INTO errors(id,month_year,error_code,include_ind,exclude_ind,error_count) VALUES ('2','201302','6','Y','N','30');

クエリ 1 :

BEGIN

INSERT INTO result

  SELECT 
        errors.id,
        ref.error_code,
        to_char(sysdate,'YYYY-MM-DD'),
        ref.month_year,
        SUM(errors.error_count),
        SUM(items.item_count),
        SUM(errors.error_count) / SUM(items.item_count),
        to_char(sysdate,'YYYY-MM-DD HH24:MI:SS')
FROM    ref
        RIGHT OUTER JOIN errors
        ON REF.error_code = errors.error_code
        AND REF.month_year = errors.month_year
           RIGHT OUTER JOIN items
           ON items.id = errors.id
           AND items.month_year = errors.month_year
WHERE   errors.include_ind = 'Y'
AND     errors.exclude_ind = 'N'
GROUP   BY
        errors.id,
        ref.error_code,
        ref.month_year,
        sysdate;
END;

[結果][2] :

クエリ 2 :

select  *
from    result
order   by id, error_code, month_year

[結果][3] :

| ID | ERROR_CODE |  PARTITION | MONTH_YEAR | TOTAL_ERROR_COUNT | TOTAL_ITEM_COUNT | RATE |     QUERY_TIMESTAMP |
-----------------------------------------------------------------------------------------------------------------
|  1 |          3 | 2013-03-13 |     201212 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |          3 | 2013-03-13 |     201301 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |          3 | 2013-03-13 |     201302 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |          4 | 2013-03-13 |     201212 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |          4 | 2013-03-13 |     201302 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |          5 | 2013-03-13 |     201212 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |          5 | 2013-03-13 |     201301 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |          5 | 2013-03-13 |     201302 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |          6 | 2013-03-13 |     201212 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |          6 | 2013-03-13 |     201301 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |          6 | 2013-03-13 |     201302 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |         11 | 2013-03-13 |     201212 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |         11 | 2013-03-13 |     201301 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |         11 | 2013-03-13 |     201302 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |         12 | 2013-03-13 |     201212 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |         12 | 2013-03-13 |     201301 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |         12 | 2013-03-13 |     201302 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |         13 | 2013-03-13 |     201212 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |         13 | 2013-03-13 |     201301 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |         13 | 2013-03-13 |     201302 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |         14 | 2013-03-13 |     201212 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |         14 | 2013-03-13 |     201301 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  1 |         14 | 2013-03-13 |     201302 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  2 |          3 | 2013-03-13 |     201212 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  2 |          3 | 2013-03-13 |     201301 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  2 |          3 | 2013-03-13 |     201302 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  2 |          6 | 2013-03-13 |     201212 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  2 |          6 | 2013-03-13 |     201301 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
|  2 |          6 | 2013-03-13 |     201302 |                30 |               30 |    1 | 2013-03-13 06:43:16 |
4

1 に答える 1

0

COALESCE(a,b) を利用して、a の null 値を b に置き換えることができます。次に、外部結合を使用すると、十分な行が得られます

SELECT it.ID,
    r.MONTH_YEAR,
    r.ERROR_CODE,
    COALESCE(SUM(ERROR_COUNT),0) "TOTAL ERRORS",
    SUM(ITEM_COUNT) "TOTAL ITEMS",
    COALESCE(SUM(ERROR_COUNT),0)/SUM(ITEM_COUNT) "RATE"
FROM ITEMS it
    JOIN REF r
        ON (it.MONTH_YEAR = r.MONTH_YEAR)
    LEFT OUTER JOIN ERRORS er
        ON (r.MONTH_YEAR = er.MONTH_YEAR
            AND er.ERROR_CODE = r.ERROR_CODE
            AND er.ID = it.ID)
GROUP BY it.ID, 
    r.MONTH_YEAR, 
    r.ERROR_CODE
ORDER BY it.ID,
    r.MONTH_YEAR,
    r.ERROR_CODE
于 2013-03-13T07:40:48.090 に答える