I have a table in MySQL 8.0 with the following structure.
CREATE TABLE `rg` ( `name` text,
`zip` text,
`Year` bigint,
`r` double,
`geom` GEOMETRY
);
INSERT INTO `rg` (name, zip, Year, r, geom)
VALUES
('AB, NM', '87105', '2015', '10', 'POLYGON ((35.066062 -106.700005))'),
('AB, NM', '87105', '2016', '20', 'POLYGON ((35.067912 -106.700884))'),
('AB, NM', '87105', '2017', '30', 'POLYGON ((35.067912 -106.700884))')
How do I write a query that returns the percentage difference of r between the latest 2 available years? I group by name, zip and geom columns.
select name, zip, ST_AsText(geom) as geom, ....
from rg
GROUP BY name, zip, geom
Expected output:
AB, NM 87105 POLYGON ((35.067912 -106.700884)) 50
i.e difference between r values 20, 30 of 2016 and 2017 is 50.