The SELECT statement below returns the correct data.
SELECT stu.sc, stu.sn, COUNT(*) AS Total,
CASE
WHEN COUNT(*) = 3 Then 'Letter 1'
WHEN COUNT(*) = 4 Then 'Letter 2'
WHEN COUNT(*) = 5 Then 'Letter 3'
ELSE 'SARB'
END AS Letter
FROM STU join att ON (stu.SC = att.SC and stu.SN = att.SN)
WHERE att.al in ('c','t','u')
GROUP by stu.sc, stu.sn
HAVING COUNT(*) >= 3
I need to perform an UPDATE based on that query, and I can’t seem to figure it out. I’ve looked at multiple examples online that are similar to what I need, but I can’t get this to work. I need to be able to do something like this…
UPDATE stu
SET stu.tru =
CASE
When COUNT(*) = 3 Then 'Letter 1'
When COUNT(*) = 4 Then 'Letter 2'
When COUNT(*) = 5 Then 'Letter 3'
ELSE 'SARB'
END
FROM stu JOIN att
on (stu.sc = att.sc and stu.sn = att.sn)
WHERE ATT.AL in ('c','t','u')
GROUP BY stu.sc, stu.sn
HAVING COUNT(*) >= 3
I know I can’t use Group By and Having in an Update statement directly, but I’ve seen examples where they use a Select with Group By before the Set and Join. I just can’t make it work.
Thanks for any help.
1 Answer