I have 2 tables.
Contacts
+----+------+
| ID | Tier |
+----+------+
| 1 | Low |
| 2 | High |
| 3 | Max |
+----+------+
Events
+----+-----------+-----------+
| ID | EventType | GroupType |
+----+-----------+-----------+
| 1 | Open | A |
| 2 | Open | A |
| 3 | Open | A |
| 1 | Delete | B |
| 2 | Open | B |
| 3 | Open | B |
| 1 | Open | A |
| 3 | Open | C |
+----+-----------+-----------+
If Events contains 2 unique GroupTypes where EventType = 'Open' then the associated Contact record needs to be updated to a Tier of 'High', else if there are more than 2 I need to update to 'Max', else if there are fewer I need to update to 'Low'. (The above table shows correct tiers)
When attempting the below, I get "Error near Group". Can I group while updating? Is there a better way to get these results?
Update c
SET c.Tier = (CASE WHEN count(DISTINCT(e.GroupType)) > 2 THEN 'Max'
WHEN count(DISTINCT(e.GroupType)) = 2 THEN 'High'
ELSE 'Low'
END)
FROM Contacts c JOIN Events e on c.ID = e.ID
WHERE e.EventType = 'Open'
GROUP BY c.ID