MySQL计数唯一的一列,其他列在Group By中具有单个值

时间:2015-02-28 13:54:18

标签: mysql sql select group-by

请参阅下表。我想对不同的访问者(visitor_id)进行计数,按组进行分组(group_id),但只计算那些访问者的结果始终为'declined'的访问者。

这样的东西
SELECT group_id, COUNT(DISTINCT visitor_id) AS always_declines
FROM customer_actions 
WHERE outcome='declined' [[AND HAS NEVER BEEN IN ('purchased')]]
GROUP BY group_id;

这是我的表格的简化版本:

SELECT * FROM customer_actions;
+----+------------+-----------+----------+
| id | visitor_id | outcome   | group_id |
+----+------------+-----------+----------+
|  1 |          5 | purchased |        1 |
|  2 |          5 | purchased |        1 |
|  3 |          6 | purchased |        1 |
|  4 |          7 | declined  |        1 |
|  5 |          6 | declined  |        1 |
|  6 |          7 | purchased |        1 |
|  7 |          8 | declined  |        1 |
|  8 |          8 | declined  |        1 |
+----+------------+-----------+----------+
8 rows in set (0.00 sec)

所以基本上如果它工作的结果我正在寻找第一个也是唯一一行(在这种情况下)返回:

group_id = 1

always_declines = 1(对应于曾经拒绝过的访客8)

2 个答案:

答案 0 :(得分:2)

not exists运算符应该可以解决问题:

SELECT   group_id, COUNT(DISTINCT visitor_id) AS always_declines
FROM     customer_actions  ca1
WHERE    NOT EXISTS (SELECT *
                     FROM   customer_actions ca2
                     WHERE  ca1.group_id = ca2.group_id AND
                            ca1.visitor_id = ca2.visitor_id AND
                            ca2.outcome != 'declined')
GROUP BY group_id;

答案 1 :(得分:1)

解决此问题的一种方法是作为两个聚合。首先,按群组和访客聚合,以获得合适的访问者。然后计算剩余的行:

SELECT group_id, count(*) AS always_declines
FROM (SELECT group_id, visitor_id
      FROM customer_actions 
      GROUP BY group_id, visitor_id
      HAVING SUM(outcome <> 'declined') = 0
     ) gv
GROUP BY group_id;