2

我有一个数据库表campaign_data。我需要选择customer_id在竞选活动中有关税差异的地方。我如何使用 MySQL 查询来做到这一点。这是一些示例数据。

SQL Fiddle 架构

| CAMPAIGN_ID | CUSTOMER_ID |    CAMPAIGN_NAME | TARIFF |
---------------------------------------------------------
|           1 |           1 |         Richmond |    100 |
|           2 |           1 | Sutton Coldfield |     75 |
|           3 |           1 |           Putney |    100 |
|           4 |           1 |     Kentish Town |    100 |
|           5 |           1 |           Woking |    100 |
|           6 |           2 |         Chiswick |     90 |
|           7 |           2 |           Ealing |    100 |
|           8 |           2 |           Camden |    100 |
|           9 |           3 |          Croydon |     75 |
|          10 |           3 |         Croydon1 |    100 |
|          11 |           3 |          Archway |    100 |
|          12 |           4 |          Ealing0 |    100 |
|          13 |           4 |         Ealing01 |    100 |
|          14 |           4 |         Ealing02 |    100 |
|          15 |           4 |        Chingford |    100 |
|          16 |           4 |      chingford01 |    100 |

现在您可以看到客户 ID 1 和 3 具有不同的关税。我想选择它们并将客户 ID 保留为 4,因为它具有相同关税的广告系列。

期望的输出

| CUSTOMER_ID |
---------------
|           1 |
|           2 |
|           3 |

为了清楚起见,您可以看到客户 1 有 5 条记录。如果在他的 5 条记录中关税相同(100),我想避免但如果关税不是一些,因为 4 条记录有 100 条记录有 75 条,我想选择。

4

3 回答 3

5
SELECT customer_id, count(DISTINCT tariff) as tariffs 
FROM campaign_data 
GROUP BY customer_id 
HAVING tariffs > 1
于 2013-05-02T11:04:19.703 回答
1
select
  customer_id,
  tariff
from campaign_data
group by customer_id
having sum(tariff)/count(tariff) <> tariff;
于 2013-05-02T11:03:00.467 回答
1

你在找这个也许

SELECT customer_id
FROM campaign_data 
GROUP BY customer_id 
HAVING count(DISTINCT tariff) > 1

http://sqlfiddle.com/#!2/48b6e/31

于 2013-05-02T11:07:24.107 回答