Мне нужно найти второй наименее представленный регион во всех командах.
У меня есть эти отношения между таблицами, но я не нашел решения для меня, как найти наименьшее число. Я до сих пор пробовал row_number и dens_number, но я не могу понять, как связать это между собой, потому что в каждой стране есть свой регион.
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 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 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)
Я понимаю, что мне нужно подключить идентификаторы между таблицами, но я не понимаю, как найти второй наименее представленный регион между всеми командами.