Мне нужно найти второй наименее представленный регион во всех командах. Регион может дублироваться из страны в страну.
Таким образом, цель состоит в том, чтобы подсчитать инженерные регионы в командах и получить вторую наименьшую величину.
Я до сих пор пытался сделать что-то вроде this
select
countries.region,
teams.name,
count(contries.region)
from teams
join engineers on teams.id = engineers.team_id
join countries on countries.id = engineers.country_id
where countries.region in (
select
countries.region
from (select
countries.region,
rownumber() over () as rownum
from countries
) as x
where
x.rownum = 2
);
Ссылки между таблицами не очень хорошие, поэтому я понятия не имею, как я могу это сделать
sqlhunt_development=# \d engineers
Table "public.engineers"
Column | Type | Modifiers
------------+-----------------------------+--------------------------------------------------------
id | bigint | not null default nextval('engineers_id_seq'::regclass)
first_name | character varying |
last_name | character varying |
age | integer |
email | character varying |
country_id | bigint |
team_id | bigint |
created_at | timestamp without time zone | not null
updated_at | timestamp without time zone | not null
Indexes:
"engineers_pkey" PRIMARY KEY, btree (id)
"index_engineers_on_country_id" btree (country_id)
"index_engineers_on_team_id" btree (team_id)
Foreign-key constraints:
"fk_rails_48c685314b" FOREIGN KEY (country_id) REFERENCES countries(id)
"fk_rails_5c44cd68ac" FOREIGN KEY (team_id) REFERENCES teams(id)
Referenced by:
TABLE "bookshelves" CONSTRAINT "fk_rails_13dec3ee94" FOREIGN KEY (engineer_id) REFERENCES engineers(id)
TABLE "engineer_programming_languages" CONSTRAINT "fk_rails_3a4377ed71" FOREIGN KEY (engineer_id) REFERENCES engineers(id)
sqlhunt_development=# \d teams
Table "public.teams"
Column | Type | Modifiers
------------------+-----------------------------+----------------------------------------------------
id | bigint | not null default nextval('teams_id_seq'::regclass)
name | character varying |
floor | integer |
features_shipped | integer |
current_bugs | integer |
created_at | timestamp without time zone | not null
updated_at | timestamp without time zone | not null
Indexes:
"teams_pkey" PRIMARY KEY, btree (id)
Referenced by:
TABLE "engineers" CONSTRAINT "fk_rails_5c44cd68ac" FOREIGN KEY (team_id) REFERENCES teams(id)
sqlhunt_development=# \d countries
Table "public.countries"
Column | Type | Modifiers
------------+-----------------------------+--------------------------------------------------------
id | bigint | not null default nextval('countries_id_seq'::regclass)
name | character varying |
capital | character varying |
region | character varying |
population | integer |
area | integer |
created_at | timestamp without time zone | not null
updated_at | timestamp without time zone | not null
Indexes:
"countries_pkey" PRIMARY KEY, btree (id)
Referenced by:
TABLE "engineers" CONSTRAINT "fk_rails_48c685314b" FOREIGN KEY (country_id) REFERENCES countries(id)
Вывод вообще должен быть:
count | region
-------+------------+
146 | Africa
159 | Oceania
159 | Europe
160 | NA
170 | SA
171 | SEA
(8 rows)
Цель состоит в том, чтобы получить это:
count | region
-------+------------+
159 | Oceania
(8 rows)