首页 > 解决方案 > 如何从 2 个不同的表中选择一个共同属性,其中一个共同属性的出现率最高?

问题描述

所以我有一个表“事故”,以及属性“pess_segura”,它是参与事故的受保人的数量。

我还有一个表“acid_pess”,其中有两个相关属性与此问题“事故”和“pessoa”,该表指的是卷入无法保证的事故的人(他们参与了事故但他们没有开车)。所以我想选择大多数人参与的事故。

这些是本练习的相关表格

create table person(
    n_pess bigint not null,
    nif integer not null,
    cart_cond varchar(15),
    pais_cart_cond varchar(30),
    doc_id varchar(15) not null,
    pais_doc_id varchar(30) not null,
    nome varchar(80) not null,
    primary key(n_pess),
    unique(nif),
    unique(cart_cond),
    unique(doc_id)
);

create table ensurance(
    apolice bigint not null,
    ensured bigint not null,
    veiculo bigint not null,
    datai date not null,
    dataf date not null,
    cobertura numeric(10,2) not null,
    primary key(apolice),
    unique(ensured, veiculo),
    foreign key (ensured) references person(n_pess),
    foreign key (veiculo) references veiculo(n_veic)
);  

create table accident(
    n_acid bigint not null,
    pess_segura bigint not null,
    veic_seguro bigint not null,
    data date not null,
    local varchar(255) not null,
    descr text not null,
    primary key(n_acid),
    unique(n_acid, veic_seguro),
    foreign key (pess_segura,veic_seguro) references ensurance(ensured, veiculo)
);

create table acid_pess(
    accident bigint not null,
    person bigint not null,
    veiculo bigint not null,
    descr text not null,
    primary key(accident, person),
    foreign key (accident) references accident(n_acid),
    foreign key (person) references person(n_pess),
    foreign key (accident, veiculo) references accident(n_acid, veic_seguro)
);

我想选择参与人数最多的 n_acid(事故数量)。

标签: sqlselect

解决方案


推荐阅读