bry888
bry888

Reputation: 297

Count with two aggregate conditions

I have a table like this:

User_id, transaction_id, transaction_cost,  transaction_type 
1000, 2000, 123, a
1000, 2001, 234, a
1000, 2002, 345, b
1001, 2003, 456, b
1001, 2004, 567, b

And want to get these user_ids, which fulfill two conditions:

1 - their summed transactions cost > 500
2 - the transactions they made have at least two types 

Is there a nice way to check two aggregate conditions in one legible select and get a list of distinct user_ids as a result? Thank you!

Upvotes: 0

Views: 43

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269443

If you just want user ids that pass the conditions:

select user_id
from t
group by user_id
having sum(transaction_cost) > 500 and
       count(distinct transaction_type) >= 2;

Upvotes: 3

Related Questions