我有一个数据库表campaign_data
。我需要选择customer_id
广告系列中的关税存在差异。我怎么能用MySQL查询做到这一点。这是一些示例数据。
| 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个,我想选择。
答案 0 :(得分:5)
SELECT customer_id, count(DISTINCT tariff) as tariffs
FROM campaign_data
GROUP BY customer_id
HAVING tariffs > 1
答案 1 :(得分:1)
select
customer_id,
tariff
from campaign_data
group by customer_id
having sum(tariff)/count(tariff) <> tariff;
答案 2 :(得分:1)
你正在寻找这个
SELECT customer_id
FROM campaign_data
GROUP BY customer_id
HAVING count(DISTINCT tariff) > 1