SQL Поиск победителя на выборах - PullRequest
0 голосов
/ 30 апреля 2020

Я работаю над этой проблемой ниже, но я застрял.

Как узнать, какая партия выиграла в каком регионе (округ)?

Можете ли вы помочь мне изменить мой запрос?

enter image description here

Я добавляю таблицу создания и вставляю в команды для целей тестирования ниже;


CREATE TABLE qbiz_candidates
(id  int PRIMARY KEY,
 gender text,
 age    int,
 party   TEXT
 );


INSERT INTO qbiz_candidates (id, gender, age, party)
    VALUES
     (1, 'M', 50, 'A'),
     (2, 'M', 50, 'B'),
     (3, 'F', 50, 'A'),
     (4, 'M', 50, 'B'),
     (5, 'F', 50, 'A'),
     (6, 'M', 50, 'B');

CREATE TABLE qbiz_results
(constituency_id  int ,
 candidate_id int ,
 votes int,
 PRIMARY KEY (constituency_id, candidate_id)
 );

INSERT INTO qbiz_results (constituency_id, candidate_id, votes)
    VALUES
     (1, 1, 10),
     (1, 2, 5),
     (1, 3, 10),
     (1, 4, 5),
     (1, 5, 10),
     (1, 6, 5),
     (2, 1, 5),
     (2, 2, 10),
     (2, 3, 5),
     (2, 4, 10),
     (2, 5, 5),
     (2, 6, 10);

Мой запрос:

select c1.party, b.constituency_id, max(b.total_votes)
from qbiz_candidates as c1
join (select c.party,r.constituency_id, sum(r.votes) as total_votes
from qbiz_candidates as c
join qbiz_results as r
    on r.candidate_id = c.id
group by r.constituency_id,c.party) b 
on b. party = c1.party
group by c1.party, b.constituency_id

Ожидаемый результат:

A 1
B 2

Значение партии А выиграл избирательный округ 1 и партии B выиграл избирательный округ 2.

1 Ответ

1 голос
/ 30 апреля 2020

Вариант 1 с dense_rank() или row_number(), вот демоверсия .

select
    party,
    constituency_id
from
(
    select
      party,
      constituency_id,
      sum(votes) as total_votes,
      dense_rank() over (partition by party order by sum(votes) desc) as rnk
  from qbiz_candidates qc
  join qbiz_results qr
  on qc.id = qr.candidate_id
  group by
      party,
      constituency_id
) val
where rnk = 1

Выход:

*-----------------------*
|party | constituency_id|
*-----------------------*
| A    |  1             |
| B    |  2             |
*-----------------------*

Вариант 2 с union all, вот демоверсия .

 (
   select
      party,
      constituency_id
  from qbiz_candidates qc
  join qbiz_results qr
  on qc.id = qr.candidate_id
  where constituency_id = 1
  group by
      party,
      constituency_id
order by
    sum(votes) desc
limit 1
)
union all
(
 select
      party,
      constituency_id
  from qbiz_candidates qc
  join qbiz_results qr
  on qc.id = qr.candidate_id
  where constituency_id = 2
  group by
      party,
      constituency_id
order by
    sum(votes) desc
limit 1
)

Вариант 3 с group_concat(). вот демо 1019 *.

select
    party,
    SUBSTRING_INDEX(GROUP_CONCAT(constituency_id order by total_votes desc), ',', 1) as constituency_id
from
(
  select
      party,
      constituency_id,
      sum(votes) as total_votes
  from qbiz_candidates qc
  join qbiz_results qr
  on qc.id = qr.candidate_id
  group by
      party,
      constituency_id

) t
group by
    party
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...