简体   繁体   English

如何在查询计数结果中使DISTINCT等效

[英]How to make the equivalent of DISTINCT in query counting results

My table stores a set of survey results, which I am outputting to a results page using this query: 我的表存储了一组调查结果,我将使用以下查询将其输出到结果页面:

SELECT a.question_one AS answer, q1.ct AS q1_count, q2.ct AS q2_count, q3.ct AS q3_count 
FROM vote_entries 
AS a 
INNER JOIN (SELECT question_one, count(question_one) AS ct from vote_entries group by question_one) q1 on q1.question_one=a.question_one 
INNER JOIN (SELECT question_two, count(question_two) AS ct FROM vote_entries group by question_two) q2 on q2.question_two=a.question_one 
INNER JOIN (SELECT question_three, count(question_three) AS ct FROM vote_entries group by question_three) q3 on q3.question_three=a.question_one 
GROUP BY a.question_one

This works how I want it to, but I have just noticed that some people have submitted more than once from the same email address. 这可以达到我想要的效果,但是我刚刚注意到有些人从同一电子邮件地址提交了不止一次的邮件。 I want to filter these people out so that only one of their responses is included. 我想过滤掉这些人,以便只包括他们的回应之一。

Essentially I want to have something like WHERE DISTINCT email , but of course that doesn't exist. 本质上,我想拥有类似WHERE DISTINCT email ,但是当然不存在。 Does anyone know how I can achieve what I am trying to do, preferrably in this one query? 有谁知道我可以实现我想要做的事情,最好是在这个查询中?

This is my table structure: 这是我的表结构:

CREATE TABLE IF NOT EXISTS `vote_entries` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(128) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  `name` varchar(128) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  `question_one` varchar(32) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  `question_two` varchar(32) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  `question_three` varchar(32) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

An example of the data looks like the below. 数据示例如下所示。 The user can select one of three options in a radio button set for each of the questions. 用户可以为每个问题选择单选按钮中的三个选项之一。

+----+------------------+------------+--------------+--------------+----------------+
| id |      email       |    name    | question_one | question_two | question_three |
+----+------------------+------------+--------------+--------------+----------------+
|  1 | test@email.com   | John Doe   | RIC          | RIC          | RIC            |
|  2 | test2@email.com  | Jane Smith | BAR          | BAR          | BAR            |
|  3 | test2@email.com  | Jane Smith | BAR          | BAR          | BAR            |
|  4 | sample@email.com | Kelly Doe  | Existing     | Existing     | Existing       |
+----+------------------+------------+--------------+--------------+----------------+

The issue is, if Jane Smith submits her answer twice, it's going to skew the results unfairly. 问题是,如果简·史密斯(Jane Smith)提交她的答案两次,将会不公平地歪曲结果。 But I don't want to encourage people to make fake email addresses to submit more than once, either - so a frontend solution of some sort is not what I'm lookign for. 但是,我也不想鼓励人们制作虚假的电子邮件地址来提交多次,因此,我不希望使用某种前端解决方案。

I would recommend conditional aggregation. 我建议条件聚合。 This may be a simpler way to to do what you want: 这可能是一种执行所需操作的简单方法:

select a.answer,
       count(distinct case when a.answer = ve.question_one then ve.email) as q1_count,
       count(distinct case when a.answer = ve.question_two then ve.email) as q2_count,
       count(distinct case when a.answer = ve.question_three then ve.email) as q3_count
from (select distinct ve.question_one as answer
      from vote_entries ve
     ) a cross join
     vote_entries ve
group by a.answer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM