我正在下面解决这个问题,但是我被困住了。
我如何找到哪个政党赢得了哪个地区(选区)?
您能帮我修改查询吗?
我正在添加创建表并插入下面的命令中以进行测试;
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,乙方赢得了选区2。
答案 0 :(得分:1)
带有dense_rank()
或row_number()
的选项1,这里是demo。
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 |
*-----------------------*
带有union all
的选项2,这里是demo。
(
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
)
带有group_concat()
的选项3。这是demo。
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
答案 1 :(得分:0)
第 1 步: 获取一个或多个查询中的所有数据字段,您需要提供所有报告或标准值(确定选区的每一方的选票数量以及赢得的选票数量)一个选区):
/* Need to know how many votes each party won in a constituency */
select c.party, d.constituency_id, sum(d.votes) as total_constituency_votes
from candidates c, results d
where c.id = d.candidate_id
group by c.party, d.constituency_id
/* Need to know how many votes won a constituency to identify which party won a constituency */
select y.constituency_id, max(y.total_constituency_votes) as winning_total_constituency_votes
from (select a.party, b.constituency_id, sum(b.votes) as total_constituency_votes
from candidates a, results b
where a.id = b.candidate_id
group by a.party, b.constituency_id) y
group by y.constituency_id
第 2 步:将其放在一个查询中,以便在报告中使用,使用子查询避免创建中间工作表。
/* Fields all found in one or more queries/subqueries that you need to provide all the reporting or criteria values: */
Select x.party, x.constituency_id
from
(select c.party, d.constituency_id, sum(d.votes) as total_constituency_votes
from candidates c, results d
where c.id = d.candidate_id
group by c.party, d.constituency_id) x,
(select y.constituency_id, max(y.total_constituency_votes) as winning_total_constituency_votes
from (select a.party, b.constituency_id, sum(b.votes) as total_constituency_votes
from candidates a, results b
where a.id = b.candidate_id
group by a.party, b.constituency_id) y
group by y.constituency_id) z
where x.total_constituency_votes = y.winning_total_constituency_votes
第 3 步: 最终报告。无需将先前的查询作为子查询,因为结果已经能够生成所请求的最终信息和格式。
Select x.party as Party, count(*) as Seats_won
from
(select c.party, d.constituency_id, sum(d.votes) as total_constituency_votes
from candidates c, results d
where c.id = d.candidate_id
group by c.party, d.constituency_id) x,
(select y.constituency_id, max(y.total_constituency_votes) as winning_total_constituency_votes
from (select a.party, b.constituency_id, sum(b.votes) as total_constituency_votes
from candidates a, results b
where a.id = b.candidate_id
group by a.party, b.constituency_id) y
group by y.constituency_id) z
where x.total_constituency_votes = y.winning_total_constituency_votes
group by x.party
order by 2 desc
答案 2 :(得分:0)
使用 dense_rank() 你可以试试这个以获得准确的解决方案
select
party,
COUNT(*)
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
group by
party
HAVING
COUNT(*) >= 1
ORDER BY COUNT(*) DESC
答案 3 :(得分:-1)
Select x.party as Party, count(*) as Seats_won
from
(select c.party, d.constituency_id, sum(d.votes) as total_constituency_votes
from candidates c, results d
where c.id = d.candidate_id
group by c.party, d.constituency_id) x,
(select y.constituency_id, max(y.total_constituency_votes) as winning_total_constituency_votes
from (select a.party, b.constituency_id, sum(b.votes) as total_constituency_votes
from candidates a, results b
where a.id = b.candidate_id
group by a.party, b.constituency_id) y
group by y.constituency_id) z
where x.total_constituency_votes = winning_total_constituency_votes
group by x.party
order by 2 desc