Luc
Luc

Reputation: 747

Returning rows that have more than one unique value in a column

select customer_id, currency, price
from sales_table

customer_id   currency  price
1001            CHF       50
1001            EUR       35
1002            EUR       40
1003            AUD       25

I have hundreds of thousands of transactions in the table. How can I do a select clause that only returns the customers who have paid in two different currencies? In this case, the select statement should return customer_id 1001.

Upvotes: 0

Views: 37

Answers (2)

LoztInSpace
LoztInSpace

Reputation: 5697

select customer_id
from sales_table
group by customer_id
HAVING count(distinct currency) = 2 // or >1 or whatever

Upvotes: 2

TimLer
TimLer

Reputation: 1360

select customer_id
from
(
    select customer_id, currency
    from sales_table
    group by customer_id, currency
) t
group by customer_id
having count(customer_id) > 1;

You can test it here

Upvotes: 1

Related Questions