Now, I would like to find the longest or heaviest catch (a fish) from the above. I have either length or weight or both specified for each catch. If a catch is more than one then I can use length (as longest) but not weight. Once found I would like to have the key for the catch - and the weight for the longest catch. Oh, and I want find these grouped by species
I have this query right now that will give me the longest (or heaviest - if no catches of this species had a length):
So the “catch” is that if any of the catches has a length then we want to return the longest of those (with the corresponding weight - although other catches without length could be heavier).
SELECT specieskey,IFMISSINGORNULL(max(length),max(case when count = 1 then weight else null end)) as maxlength
FROM data
WHERE type='Catch' and userkey='2124DEFEC111BA8FC1257ED20034B387'
group by specieskey
SELECT specieskey, CASE WHEN m[0] != 0 THEN m[0] ELSE m[1] END AS maxlength
FROM data
WHERE type='Catch' and userkey='2124DEFEC111BA8FC1257ED20034B387'
GROUP BY specieskey
LETTING m = MAX([IFMISSINGORNULL(length,0), CASE WHEN `count` = 1 THEN weight ELSE NULL END]);
then I would like to have returned the one with length 55 and its weight 2.55 (and key - for allowing the app to link to it) - although there is another one that is heavier - but it is another fish, and we prioritize length over weight
SELECT specieskey, CASE WHEN m[0] != 0 THEN m[0] ELSE m[1] END AS maxlength, m[1] AS weight, m[2] AS key1
FROM data
WHERE type='Catch' and userkey='2124DEFEC111BA8FC1257ED20034B387'
GROUP BY specieskey
LETTING m = MAX([IFMISSINGORNULL(length,0), CASE WHEN `count` = 1 THEN weight ELSE NULL END, `key`]);
I really just need to return null instead of m[1] to not return weight in the “maxlength”. I had tried to do something similar but didn’t manage to get the LETTING... statement quite right.