sql - 选择自己的阵列。 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 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 和 FAILS 'Appeared new difficulty' 和 'Parameters Mismatch' 的运行(实际上是更多运行)。

正确

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

不正确

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

因此,请求应该找到请求失败且错误属于特定运行的所有运行:

1 运行不适合我,因为它没有“参数不匹配”并且没有错误 ERROR2 TYPE。 2 运行适合我,因为它有所有错误(错误 2 类型)并且全部失败(“出现新的困难”和“参数不匹配”)。 3 运行适合我,因为它有所有错误(错误 2 类型)并且全部失败(“出现新的困难”和“参数不匹配”)。如果运行失败次数多于我们要求的次数(它有新的失败次数:'Attachment Journal Mismatch')也没关系,但它必须至少有请求的失败和错误。

我该怎么做?

以下 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'

此代码也无法正常工作,它仅搜索 1 个匹配项而不是整组(“出现新难度”、“参数不匹配”):

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

看来我需要某种交叉点。

最佳答案

这是我的理解:

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

关于sql - 选择自己的阵列。 PostgreSQL 9.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42294568/

相关文章:

SQL根据条件跳过最大行

python - 在 python-django 中移动数据库

postgresql - SET LOCAL 为先前存储的值

MySQL JOIN 返回意外值

sql - 如何编写查询以根据表 A 的一列值将表 A 连接到表 B 或 C?

mysql - 高级选择

SQL Server 2012链接服务器应用程序意图

MySQL 连接同时依赖连接表

php - 从ID指定的合并表中获取特定行数据

PostgreSQL 行插入带有额外返回字段的 CTE