-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2820-ElectionResults.sql
More file actions
129 lines (123 loc) · 4.48 KB
/
2820-ElectionResults.sql
File metadata and controls
129 lines (123 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
-- 2820. Election Results
-- Table: Votes
-- +-------------+---------+
-- | Column Name | Type |
-- +-------------+---------+
-- | voter | varchar |
-- | candidate | varchar |
-- +-------------+---------+
-- (voter, candidate) is the primary key (combination of unique values) for this table.
-- Each row of this table contains name of the voter and their candidate.
-- The election is conducted in a city where everyone can vote for one or more candidates or choose not to vote. Each person has 1 vote so if they vote for multiple candidates, their vote gets equally split across them. For example, if a person votes for 2 candidates, these candidates receive an equivalent of 0.5 votes each.
-- Write a solution to find candidate who got the most votes and won the election. Output the name of the candidate or If multiple candidates have an equal number of votes, display the names of all of them.
-- Return the result table ordered by candidate in ascending order.
-- The result format is in the following example.
-- Example 1:
-- Input:
-- Votes table:
-- +----------+-----------+
-- | voter | candidate |
-- +----------+-----------+
-- | Kathy | null |
-- | Charles | Ryan |
-- | Charles | Christine |
-- | Charles | Kathy |
-- | Benjamin | Christine |
-- | Anthony | Ryan |
-- | Edward | Ryan |
-- | Terry | null |
-- | Evelyn | Kathy |
-- | Arthur | Christine |
-- +----------+-----------+
-- Output:
-- +-----------+
-- | candidate |
-- +-----------+
-- | Christine |
-- | Ryan |
-- +-----------+
-- Explanation:
-- - Kathy and Terry opted not to participate in voting, resulting in their votes being recorded as 0. Charles distributed his vote among three candidates, equating to 0.33 for each candidate. On the other hand, Benjamin, Arthur, Anthony, Edward, and Evelyn each cast their votes for a single candidate.
-- - Collectively, Candidate Ryan and Christine amassed a total of 2.33 votes, while Kathy received a combined total of 1.33 votes.
-- Since Ryan and Christine received an equal number of votes, we will display their names in ascending order.
-- Create table if not exists Votes(voter varchar(30), candidate varchar(30))
-- Truncate table Votes
-- insert into Votes (voter, candidate) values ('Kathy', 'None')
-- insert into Votes (voter, candidate) values ('Charles', 'Ryan')
-- insert into Votes (voter, candidate) values ('Charles', 'Christine')
-- insert into Votes (voter, candidate) values ('Charles', 'Kathy')
-- insert into Votes (voter, candidate) values ('Benjamin', 'Christine')
-- insert into Votes (voter, candidate) values ('Anthony', 'Ryan')
-- insert into Votes (voter, candidate) values ('Edward', 'Ryan')
-- insert into Votes (voter, candidate) values ('Terry', 'None')
-- insert into Votes (voter, candidate) values ('Evelyn', 'Kathy')
-- insert into Votes (voter, candidate) values ('Arthur', 'Christine')
-- Write your MySQL query statement below
-- -- 计算每个投票者的投票值 投的人越多越小
-- SELECT
-- voter,
-- COUNT(*) AS cnt,
-- 1 / COUNT(*) AS vote_val
-- FROM
-- Votes
-- GROUP BY
-- voter
-- -- 统计评分并排名
-- SELECT
-- v.candidate,
-- SUM(t.vote_val) AS val,
-- RANK() OVER(ORDER BY SUM(t.vote_val) DESC) AS rk
-- FROM
-- Votes AS v
-- LEFT JOIN
-- (
-- -- 计算每个投票者的投票值 投的人越多越小
-- SELECT
-- voter,
-- COUNT(*) AS cnt,
-- 1 / COUNT(*) AS vote_val
-- FROM
-- Votes
-- GROUP BY
-- voter
-- ) AS t
-- ON
-- t.voter = v.voter
-- WHERE
-- v.candidate IS NOT NULL
-- GROUP BY
-- v.candidate
SELECT
candidate
FROM
(
-- 统计评分并排名
SELECT
v.candidate,
SUM(t.vote_val) AS val,
RANK() OVER(ORDER BY SUM(t.vote_val) DESC) AS rk
FROM
Votes AS v
LEFT JOIN
(
-- 计算每个投票者的投票值 投的人越多越小
SELECT
voter,
COUNT(*) AS cnt,
1 / COUNT(*) AS vote_val
FROM
Votes
GROUP BY
voter
) AS t
ON
t.voter = v.voter
WHERE
v.candidate IS NOT NULL
GROUP BY
v.candidate
) AS tt
WHERE
rk = 1
ORDER BY
candidate -- 返回按 candidate 升序排序 的结果表