SELECT records WHERE rows have difference on a specific column

I have a database table campaign_data. I need to select the customer_id where in the campaign there is difference in tariff. How can i do that with MySQL query. Here is some sample data.

SQL Fiddle Schema

| 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 |

Now as you can see customer id 1 , and 3 has different tariffs. I want to select them and leave the customer id 4 because it has campaigns with same tariffs.

Desired Output

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

For clearification you can see customer 1 has 5 records. If in his 5 records the tariff is same (100) i want to avoid but if the tariff is not some as 4 records have 100 and one has 75, i want to select.

Answers


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

Need Your Help

Firefox ignores page-break-before when printing

html css firefox gecko

I have a @media print{} section to tweak a site for printing. I'm forcing a page break before main blocks. However, elements overlap in Firefox (other browsers render everything as expected). As the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.