2017-02-17 2 views
2

私は、次のスキーマがあります。独自の配列で選択してください。 PostgreSQLの9.5

create table reports (id bigserial primary key); 
create table scens (id bigserial primary key,report_id bigint references reports(id), path_id bigint); 
create table runs (id bigserial primary key,scen_id bigint references scens(id)); 
create table fails (
    id bigserial primary key, 
    run_id bigint references runs(id), 
    type varchar not null 
); 

create table errors (
    id bigserial primary key, 
    run_id bigint references runs(id), 
    type varchar not null 
); 
INSERT INTO reports(id) 
    VALUES (1); 
INSERT INTO reports(id) 
    VALUES (2); 

INSERT INTO scens(id, report_id) 
    VALUES (555, 1); 
INSERT INTO scens(id, report_id) 
    VALUES (666, 2); 

INSERT INTO runs(id, scen_id) 
    VALUES (1, 555); 
INSERT INTO runs(id, scen_id) 
    VALUES (2, 666); 
INSERT INTO runs(id, scen_id) 
    VALUES (3, 666); 
INSERT INTO runs(id, scen_id) 
    VALUES (4, 666); 

INSERT INTO errors(id, run_id, type) 
    VALUES (DEFAULT, 2, 'ERROR2 TYPE'); 
INSERT INTO errors(id, run_id, type) 
    VALUES (DEFAULT, 3, 'ERROR2 TYPE'); 

INSERT INTO fails(id, run_id, type) 
    VALUES (DEFAULT, 1, 'Attachment Journal Mismatch'); 
INSERT INTO fails(id, run_id, type) 
    VALUES (DEFAULT, 2, 'Appeared new difficulty'); 
INSERT INTO fails(id, run_id, type) 
    VALUES (DEFAULT, 2, 'Parameters Mismatch'); 
INSERT INTO fails(id, run_id, type) 
    VALUES (DEFAULT, 3, 'Attachment Journal Mismatch'); 
INSERT INTO fails(id, run_id, type) 
    VALUES (DEFAULT, 3, 'Appeared new difficulty'); 
INSERT INTO fails(id, run_id, type) 
    VALUES (DEFAULT, 3, 'Parameters Mismatch'); 

私はエラーERROR2タイプを持っていると「登場新難易度」と「パラメータの不一致」を果たせない、実行(実際にはより多くの実行)を見つける必要があります。

正しい

1 -> 'ERROR2 TYPE' 
1 -> 'Appeared new difficulty' 
1 -> 'Parameters Mismatch' 

誤った

2 -> 'Parameters Mismatch' 
2 -> 'Appeared new difficulty' 

ので、要求されたすべての実行を見つける必要があり、要求が失敗し、エラーが特定の実行に属し:

1実行が合いません私は 'パラメータの不一致'がなく、エラーERROR2 TYPEを持っていないので、私は私です。 2はすべてのエラー(ERROR2 TYPE)があり、すべてが失敗しました( '新しい難易度が出現しました'と 'パラメータが一致しません')ので、実行に適しています。 3はすべてエラー(ERROR2 TYPE)があり、すべてが失敗しました( '新しい難易度が出現しました'と 'パラメータが一致しません')ので、実行に適しています。実行に失敗した場合(新しい添付ファイル「添付ファイルのジャーナルの不一致」)、要求されたエラーとエラーが少なくとも発生している必要があります。

どうすればいいですか?

次のSQLが全く動作しませんが(それが何も見つからない):

select scens.id as scen_id, 
scens.path_id, fails.type, reports.id, runs.id 
from reports 
inner join scens on reports.id=scens.report_id 
inner join runs on runs.scen_id=scens.id 
inner join fails on fails.runs_id=runs.id 
where reports.id=2 and fails.type=''Parameters Mismatch' 
and fails.type='Appeared new difficulty' 

このコードsoens; tは、あまりにも動作し、それが唯一の1試合が、のではないセット全体を検索する(「新しい困難を登場'、' Parameters Mismatch '):

select scens.custom_id as scen_custom_id, scens.id as scen_id, 
    scens.path_id, scens.category, fails_map.type, f.error_type 
    from reports 
    inner join scens on reports.id=scens.report_id 
    inner join runs on runs.scen_id=scens.id 
    inner join fails on fails.runs_id=runs.id 
    INNER JOIN 
    unnest(array['Appeared new difficulty', 'Parameters Mismatch']) f (error_type) 
    on fails.type=f.error_type 
    where reports.id=2 

私は何らかの種類の交差点が必要です。

答えて

0

私はSQLでそれを解決した:

select runs.id 
    from reports 
    inner join scens s on reports.id=s.report_id 
    inner join runs on runs.scen_id=s.id 
    where exists(select * from fails where fails.path_id=s.path_id and fails.type='Rotation Mismatch') 
    and exists(select * from fails where fails.path_id=s.path_id and fails.type='Disappeared inspection') 
    and reports.id=2 

対象は閉じることができます。

0

これが私の理解です:

select 
    reports.id as report, 
    scens.id as scen, 
    scens.path_id as path, 
    runs.id as run, 
    array_agg(distinct fails.type) as fails, 
    array_agg(distinct errors.type) as errors 
from 
    reports 
    inner join 
    scens on reports.id = scens.report_id 
    inner join 
    runs on runs.scen_id = scens.id 
    left join 
    fails on fails.run_id = runs.id 
    left join 
    errors on errors.run_id = runs.id 
where reports.id = 2 
group by 1,2,3,4 
having 
    array['Parameters Mismatch','Appeared new difficulty']::varchar[] <@ array_agg(fails.type) and 
    array['ERROR2 TYPE']::varchar[] <@ array_agg(errors.type) 
; 
report | scen | path | run |          fails          |  errors  
--------+------+------+-----+---------------------------------------------------------------------------------+----------------- 
     2 | 666 |  | 2 | {"Appeared new difficulty","Parameters Mismatch"}        | {"ERROR2 TYPE"} 
     2 | 666 |  | 3 | {"Appeared new difficulty","Attachment Journal Mismatch","Parameters Mismatch"} | {"ERROR2 TYPE"} 
関連する問題