我有一张这样的表:
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
并希望获得满足以下两个条件的user_id:
1 - their summed transactions cost > 500
2 - the transactions they made have at least two types
有一种很好的方法可以在一个清晰的选择中检查两个聚合条件,并获得一个不同的user_id列表吗? 谢谢!
答案 0 :(得分:3)
如果您只想要通过条件的用户ID:
select user_id
from t
group by user_id
having sum(transaction_cost) > 500 and
count(distinct transaction_type) >= 2;