合计投票结果

发布于 2024-11-18 15:34:51 字数 1150 浏览 3 评论 0原文

我有一张选票,每个选民获得 3 票,从 10 名不同的候选人中进行选择。投票 1 得 3 分,投票 2 得 2 分,投票 3 得 1 分。

我有以下 SQL 查询来计算每次投票获得的总分(因此投票 1、2 和 3 的结果是分开的)。

我需要做的是将所有这些结果放在一个表中,但我不太确定从哪里开始。

SELECT cn.cand_name, (count(vote_1) * 3) as vote_1 FROM candidate_votes cv Inner Join candidate_names cn ON cv.vote_1 = cn.cand_number GROUP BY cand_name;

SELECT cn.cand_name, (count(vote_2) * 2) as vote_2 FROM candidate_votes cv Inner Join candidate_names cn ON cv.vote_2 = cn.cand_number GROUP BY cand_name;

SELECT cn.cand_name, (count(vote_3) * 1) as vote_3 FROM candidate_votes cv Inner Join candidate_names cn ON cv.vote_3 = cn.cand_number GROUP BY cand_name;

我有以下结果表:

Voter_number    Vote_1     Vote2      Vote3
123             cand_1     cand_3     cand_2
456             cand_2     cand_1     cand_3
789             cand_2     cand_3     cand_1

以及以下候选者姓名表:

cand_number     cand_name
cand_1          Dave
cand_2          Sarah
cand_3          Nigel

所以我正在寻找的结果将类似于:

Candidate       Votes
Dave            6
Sarah           7
Nigel           5

I have a ballot where each voter gets 3 votes, choosing from 10 different candidates. Vote 1 is allocated 3 points, vote 2 gets 2 points and vote 3 gets 1 point.

I have the following SQL queries to total the number of points gained from each of the votes (so separate results for votes 1, 2 and 3).

What I need to do is put all these results together in a single table, but I'm not too sure where to start.

SELECT cn.cand_name, (count(vote_1) * 3) as vote_1 FROM candidate_votes cv Inner Join candidate_names cn ON cv.vote_1 = cn.cand_number GROUP BY cand_name;

SELECT cn.cand_name, (count(vote_2) * 2) as vote_2 FROM candidate_votes cv Inner Join candidate_names cn ON cv.vote_2 = cn.cand_number GROUP BY cand_name;

SELECT cn.cand_name, (count(vote_3) * 1) as vote_3 FROM candidate_votes cv Inner Join candidate_names cn ON cv.vote_3 = cn.cand_number GROUP BY cand_name;

I have the following results table:

Voter_number    Vote_1     Vote2      Vote3
123             cand_1     cand_3     cand_2
456             cand_2     cand_1     cand_3
789             cand_2     cand_3     cand_1

And the following candidate name table:

cand_number     cand_name
cand_1          Dave
cand_2          Sarah
cand_3          Nigel

So the results I'm looking for will look something like:

Candidate       Votes
Dave            6
Sarah           7
Nigel           5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萤火眠眠 2024-11-25 15:34:51
SELECT
    cn.cand_name, 
    count(cv1.vote_1) * 3 as vote_1, 
    count(cv2.vote_2) * 2 as vote_2, 
    count(cv3.vote_3) as vote_3
FROM
    candidate_names cn
    LEFT JOIN
    candidate_votes cv1 ON cv1.vote_1 = cn.cand_number
    LEFT JOIN
    candidate_votes cv2 ON cv2.vote_2 = cn.cand_number
    LEFT JOIN
    candidate_votes cv3 ON cv3.vote_3 = cn.cand_number
GROUP BY cn.cand_name;

这还允许您添加所有投票

(count(cv1.vote_1) * 3) +
    (count(cv2.vote_2) * 2) +
    count(cv3.vote_3) as totalvotes

编辑:行将乘以 JOIN,这就是 cand2 和 cand3 错误的原因

SELECT
    cn.cand_name, 
    SUM(CASE WHEN cv.vote_1 = cn.cand_number THEN 3 ELSE 0 END) as vote_1, 
    SUM(CASE WHEN cv.vote_2 = cn.cand_number THEN 2 ELSE 0 END) as vote_2, 
    SUM(CASE WHEN cv.vote_3 = cn.cand_number THEN 1 ELSE 0 END) as vote_3
FROM
    candidate_names cn
    JOIN
    candidate_votes cv ON cn.cand_number IN (cv.vote_1, cv.vote_2, cv.vote_3)
GROUP BY cn.cand_name;
SELECT
    cn.cand_name, 
    count(cv1.vote_1) * 3 as vote_1, 
    count(cv2.vote_2) * 2 as vote_2, 
    count(cv3.vote_3) as vote_3
FROM
    candidate_names cn
    LEFT JOIN
    candidate_votes cv1 ON cv1.vote_1 = cn.cand_number
    LEFT JOIN
    candidate_votes cv2 ON cv2.vote_2 = cn.cand_number
    LEFT JOIN
    candidate_votes cv3 ON cv3.vote_3 = cn.cand_number
GROUP BY cn.cand_name;

This also allows you to add all votes

(count(cv1.vote_1) * 3) +
    (count(cv2.vote_2) * 2) +
    count(cv3.vote_3) as totalvotes

Edit: rows are being multiplied by the JOIN which is why it's wrong for cand2 and cand3

SELECT
    cn.cand_name, 
    SUM(CASE WHEN cv.vote_1 = cn.cand_number THEN 3 ELSE 0 END) as vote_1, 
    SUM(CASE WHEN cv.vote_2 = cn.cand_number THEN 2 ELSE 0 END) as vote_2, 
    SUM(CASE WHEN cv.vote_3 = cn.cand_number THEN 1 ELSE 0 END) as vote_3
FROM
    candidate_names cn
    JOIN
    candidate_votes cv ON cn.cand_number IN (cv.vote_1, cv.vote_2, cv.vote_3)
GROUP BY cn.cand_name;
谎言 2024-11-25 15:34:51
SELECT cn.cand_name
     , COALESCE(cv1.cnt_1,0)
     , COALESCE(cv2.cnt_2,0)
     , COALESCE(cv3.cnt_3,0)
     , 3*COALESCE(cv1.cnt_1,0) + 2*COALESCE(cv2.cnt_2,0)
       + 1*COALESCE(cv3.cnt_3,0) AS total
FROM candidate_names AS cn
  LEFT JOIN 
    ( SELECT vote_1 AS vote
           , COUNT(*) AS cnt_1
      FROM candidate_votes cv
      GROUP BY vote_1
    ) AS cv1
    ON cv1.vote = cn.cand_number
  LEFT JOIN 
    ( SELECT vote_2 AS vote
           , COUNT(*) AS cnt_2
      FROM candidate_votes cv
      GROUP BY vote_2
    ) AS cv2
    ON cv2.vote = cn.cand_number
  LEFT JOIN 
    ( SELECT vote_3 AS vote
           , COUNT(*) AS cnt_2
      FROM candidate_votes cv
      GROUP BY vote_3
    ) AS cv3
    ON cv3.vote = cn.cand_number
SELECT cn.cand_name
     , COALESCE(cv1.cnt_1,0)
     , COALESCE(cv2.cnt_2,0)
     , COALESCE(cv3.cnt_3,0)
     , 3*COALESCE(cv1.cnt_1,0) + 2*COALESCE(cv2.cnt_2,0)
       + 1*COALESCE(cv3.cnt_3,0) AS total
FROM candidate_names AS cn
  LEFT JOIN 
    ( SELECT vote_1 AS vote
           , COUNT(*) AS cnt_1
      FROM candidate_votes cv
      GROUP BY vote_1
    ) AS cv1
    ON cv1.vote = cn.cand_number
  LEFT JOIN 
    ( SELECT vote_2 AS vote
           , COUNT(*) AS cnt_2
      FROM candidate_votes cv
      GROUP BY vote_2
    ) AS cv2
    ON cv2.vote = cn.cand_number
  LEFT JOIN 
    ( SELECT vote_3 AS vote
           , COUNT(*) AS cnt_2
      FROM candidate_votes cv
      GROUP BY vote_3
    ) AS cv3
    ON cv3.vote = cn.cand_number
烦人精 2024-11-25 15:34:51
SELECT
  Candidate = n.cand_name,
  Votes = SUM(s.vote_weight)
FROM (
  SELECT
    cand_number = CASE x.weight
      WHEN 1 THEN Vote3
      WHEN 2 THEN Vote2
      WHEN 3 THEN Vote1
    END,
    vote_weight = x.weight
  FROM candidate_votes v
    CROSS JOIN (SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3) x (weight)
) s
  INNER JOIN candidate_names n ON s.cand_number = n.cand_number
GROUP BY n.cand_name
SELECT
  Candidate = n.cand_name,
  Votes = SUM(s.vote_weight)
FROM (
  SELECT
    cand_number = CASE x.weight
      WHEN 1 THEN Vote3
      WHEN 2 THEN Vote2
      WHEN 3 THEN Vote1
    END,
    vote_weight = x.weight
  FROM candidate_votes v
    CROSS JOIN (SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3) x (weight)
) s
  INNER JOIN candidate_names n ON s.cand_number = n.cand_number
GROUP BY n.cand_name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文