假设一个小镇正在举行选举,每个公民都可以根据自己的意愿多次投票,但他们投票的次数越多,他们投票的次数越多,他们就不能进行重复投票。
Voter Vote
---------------
Jane Jane
Howard Jane
John Howard
John Howard
Jane John
Sara Howard
您如何在SQL查询中确定获胜者是谁?
我是基本用户,所以我得到的只是
SELECT
vote, COUNT( vote)
FROM
table1
GROUP BY
vote
这并不考虑重复或分割投票。
帮助?
答案 0 :(得分:1)
您可以使用动态创建的表
select vote, count(*)
from
(select distinct voter, vote
from table1) as t
group by vote
答案 1 :(得分:0)
SELECT V.vote, SUM(W.weight) AS votes
FROM
(
SELECT voter, CAST(1 AS FLOAT) / COUNT(vote) AS weight
FROM table1
group by voter
) W
JOIN
(
SELECT DISTINCT vote, voter
FROM table1
) V ON W.voter = V.voter
GROUP BY V.vote
答案 2 :(得分:0)
试试这个:
select vote, sum(1 / votes) as result from
(SELECT voter, count(*) as votes from Table1 group by voter) votecount
INNER JOIN Table1 on votecount.voter = Table1.voter
group by vote
order by result desc
这会以一个选民可以给出2/3和1/3票数的方式处理重复数据
修改强> 通过
添加订单答案 3 :(得分:0)
您可以使用不同的选民和投票组合来删除重复投票
SELECT vote,COUNT(distinct voter) FROM table1 group by vote
答案 4 :(得分:0)