2017-08-10 73 views
0

我有這樣的一個表:計數有兩個總條件

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_ids,它滿足兩個條件:

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

有一個很好的方法來檢查兩個骨料在一個清晰的選擇條件,並獲得不同的user_ids列表作爲結果? 謝謝!

+0

顯示你想要得到的結果。 –

+0

我想通過我寫的兩個條件的user_ids – bry888

回答

3

如果你只是想通過該條件的用戶ID:

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

非常好!我不知道有那麼強大 - 謝謝! – bry888