I’d say have SQL do the work. It’s fairly trivial and won’t soak up CPU time, and SQL will have to load the pertinent info in memory anyway so it’s already there for processing. Doing it on the app side, to a certain extent it seems like you have to “re-stage” the data for analysis, and (imho) the java code seems more difficult to read through and understand.
Note that there’s a minor flaw in your SQL code, you can’t use WHEN NULL
that way in a case statement. You’d want something like
...case
when min(Id) is null then 2
when min(Id) = -1 then 1
else 3
end