使用其他表中的count(*)结果的SQL构建表

时间:2018-11-07 16:02:43

标签: postgresql count create-table

我正在构建一个Pokemon数据集,并希望对其进行一些查询。 这是数据库的设置:

create table pokedex(
    name varchar(20) not null,
    weigth int not null,
    height int not null,
    primary key(name)
);
create table trainer(
    name varchar(20) not null,
    location varchar(20) not null,
    gender varchar(10) not null,
    birth_year int not null,
    primary key(name)
);

create table trainer_pokemon(
    trainer_name varchar(20) not null,
    pokemon_name varchar(20) not null,
    level int not null,
    year_obtained int not null,
    primary key(trainer_name, pokemon_name, level, year_obtained),
    foreign key(trainer_name) references trainer(name),
    foreign key(pokemon_name) references pokedex(name)
);
create table type(
    name varchar(20) not null,
    primary key(name)
);
create table poke_type(
    pokemon_name varchar(20) not null,
    type_name varchar(20) not null,
    primary key(pokemon_name, type_name),
    foreign key(pokemon_name) references pokedex(name),
    foreign key(type_name) references type(name)
);

这个想法是数据集不应该有多余的数据,因此,如果我想获得一个表,其中包含每位教练员最常用的口袋妖怪类型,我需要获取每种类型的表,或者至少获取我认为是atm的表:

with psychics as (
    select trainer_name, count(type_name) psychic from trainer_pokemon as tp
    inner join poke_type as pt on pt.pokemon_name = tp.pokemon_name
    group by tp.trainer_name, pt.type_name
    having pt.type_name = 'Psychic'
),
waters as (
    select trainer_name, count(type_name) water from trainer_pokemon as tp
    inner join poke_type as pt on pt.pokemon_name = tp.pokemon_name
    group by tp.trainer_name, pt.type_name
    having pt.type_name = 'Water'
),
select tp.trainer_name, w.water, p.psychic from trainer_pokemon as tp
inner join waters as w on w.trainer_name = tp.trainer_name
inner join psychics as p on p.trainer_name = tp.trainer_name
group by tp.trainer_name, w.water, p.psychic

但是,这不会导致培训师没有特定类型的口袋妖怪(在此示例中为水/精神)。

有人能指出我正确的方向来建立训练师表的时候,其中包含特定类型出现在口袋妖怪收藏中的次数吗?

1 个答案:

答案 0 :(得分:0)

假设您使用的是Postgresql的相对最新版本(您已经标记了Mysql和Postgres,它们是完全不同的数据库)

SELECT tp.trainer_name,
  COUNT(*) FILTER (WHERE pt.type_name = 'Psychic') as psychic,
  COUNT(*) FILTER (WHERE pt.type_name = 'Water')   as water
FROM trainer_pokemon tp, poke_type pt
WHERE tp.pokemon_name = pt.pokemon_name
GROUP BY 1

在您的问题中发布的查询可能会作为对表的多次扫描而执行,但是如果您确实想执行此查询,则可以使用“ LEFT OUTER JOIN”(OUTER关键字是可选的)执行:

with psychics as (
    select trainer_name, count(type_name) AS psychic
    from trainer_pokemon as tp
      inner join poke_type as pt on pt.pokemon_name = tp.pokemon_name
    group by tp.trainer_name, pt.type_name
    having pt.type_name = 'Psychic'
), waters as (
    select trainer_name, count(type_name) AS water
    from trainer_pokemon as tp
      inner join poke_type as pt on pt.pokemon_name = tp.pokemon_name
    group by tp.trainer_name, pt.type_name
    having pt.type_name = 'Water'
)
select tp.trainer_name, w.water, p.psychic
from trainer_pokemon as tp
  left join waters as w on w.trainer_name = tp.trainer_name
  left join psychics as p on p.trainer_name = tp.trainer_name

或另外两种可能会更好地扩展的方法:

with trained as (
    select trainer_name, pt.type_name, count(*) as num
    from trainer_pokemon as tp
      inner join poke_type as pt on pt.pokemon_name = tp.pokemon_name
    group by tp.trainer_name, pt.type_name
)
select tp.trainer_name, w.num as water, p.num as psychic
from trainer_pokemon as tp
  left join trained as w on tp.trainer_name = w.trainer_name and w.type_name = 'Water'
  left join trained as p on tp.trainer_name = p.trainer_name and p.type_name = 'Psychic'