Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用自己的数组选择。PostgreSQL 9.5_Sql_Postgresql_Join_Intersect_Postgresql 9.5 - Fatal编程技术网

使用自己的数组选择。PostgreSQL 9.5

使用自己的数组选择。PostgreSQL 9.5,sql,postgresql,join,intersect,postgresql-9.5,Sql,Postgresql,Join,Intersect,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 t

我有以下模式:

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 TYPE并且失败“出现新的困难”和“参数不匹配”

正确的

1 -> 'ERROR2 TYPE'
1 -> 'Appeared new difficulty'
1 -> 'Parameters Mismatch'
不正确

2 -> 'Parameters Mismatch'
2 -> 'Appeared new difficulty'
因此,请求应查找请求失败且错误属于特定运行的所有运行:

1
运行不适合我,因为它没有“参数不匹配”和错误ERROR2类型。
2
运行适合我,因为它有所有错误(ERROR2类型)和所有失败(“出现新困难”和“参数不匹配”)。
3
运行适合我,因为它有所有错误(ERROR2类型)和所有失败(“出现新困难”和“参数不匹配”)。如果运行的失败次数(它有新的失败:“附件日志不匹配”)比我们请求的次数多,则可以,但它必须至少有请求的失败和错误

我怎么做

以下sql根本不起作用(它什么也找不到):

该代码是soens;t也不起作用,它只搜索1个匹配项,但不搜索整组('出现新困难','参数不匹配'):


看来我需要一个十字路口。

这是我的理解:

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"}
选择
reports.id作为报告,
scens.id作为scen,
scens.path\u id作为路径,
runs.id作为run,
数组_agg(distinct fails.type)作为失败,
数组_agg(distinct errors.type)作为错误
从…起
报告
内连接
报告上的scens.id=scens.report\u id
内连接
在runs.scen_id=scens.id上运行
左连接
失败时失败。运行\u id=runs.id
左连接
errors.run_id=runs.id上的错误
其中reports.id=2
按1,2,3,4分组
有

数组['Parameters Mismatch','Exeated new Demobility']::varchar[]我已经用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
主题可以关闭

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"}
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