How to count the number of occurrent of each user ID with conditions in SQL database