aggregate-functions

MySQL how to get avg of values into specific range

北慕城南 提交于 2020-12-09 06:43:49
问题 I have the following table data: value 1 5 10.5 12 36 I want to map these values into: range avg 0-21 (1 + 5 + 10.5 + 12) / 4 21.001-34 0 34.001-64 36 64 + 0 Basically map each value to ranges and calculate the avg of all values in each range. I tried to do: select case when avggeneral between 0 and 21 then ' 0-21' when avggeneral between 21.00001 and 34 then ' 21-34' when avggeneral between 34.00001 and 64 then ' 34-64' else '64+' end as 'range', AVG(*) as 'avg' from table but this doesn't

MySQL aggregate functions with LEFT JOIN

泄露秘密 提交于 2020-12-08 07:24:17
问题 It's Friday afternoon * and my brain has stopped working. Normally it is me who's answering dumb SQL questions like this, sorry! I am trying to get one table, along with the highest value of a column of another table by LEFT JOINing the latter to the former. SELECT jobs.*, MAX(notes.`timestamp`) AS complete_date FROM jobs LEFT JOIN notes ON (jobs.id=notes.job_id) WHERE (jobs.status="complete" OR jobs.status="closed") AND (notes.type="complete" OR notes.type IS NULL) GROUP BY jobs.id ORDER BY