需要准确的输出

时间:2018-05-25 04:17:39

标签: mysql sql

我是SQL新手。我需要知道SQL查询以获得以下场景的输出。

我有一个表Campaign,其中包含

Customer ID, Campaign Name, Campaign ID, Channel

Channel可以包含以下值之一:

Direct Email, Email, Both

如何评估广告系列与客户的联系渠道?例如,通过两个渠道联系了一些客户。

如何获得通过这两个渠道联系的客户数量?请帮我查询。

1 个答案:

答案 0 :(得分:0)

简单,我给出了一个例子。你可以手动检查。

select count(*) from (select count(*) as b from Campaign group by customer_id) as a where a.b=2;

在这里,首先我们根据customer_id对它们进行分组,我认为这与customer_id截然不同。然后计算那些已经使用了两个频道的id。

这是虚拟案例:

+------+------+
| id   | ch   |
+------+------+
|    1 | d    |
|    2 | d    |
|    2 | e    |
|    3 | d    |
|    4 | d    |
|    4 | e    |
|    5 | d    |
|    6 | e    |
+------+------+

inner query: (select count(*) from q group by id);
output: 

       +----------+
        | count(*) |
        +---------+
        |        1 |
        |        2 |
        |        1 |
        |        2 |
        |        1 |
        |        1 |
        +----------+
whole query: mysql> select count(*) 
from (select count(*) as b from q group by id) as a where a.b=2;
        +----------+
        | count(*) |
        +----------+
        |        2 |
        +----------+

1行(0.00秒)