Tengo la siguiente consulta:
SELECT plague, fecha, FORMAT(((cuadrantes_infectados * 100) / total_cuadrantes),3) AS percentage
FROM (
SELECT pr_plagues.plague, YEARWEEK(ph_planthealth.date) AS fecha,
(SELECT COUNT(pr_production_units_details.id)
FROM pr_production_units_details
INNER JOIN pr_grouper_details ON pr_grouper_details.id = pr_production_units_details.id_grouper_detail
WHERE pr_production_units_details.status = 100
AND pr_grouper_details.id_land = 1
AND pr_grouper_details.status = 100
AND pr_production_units_details.id_tenant = 1) * (SELECT value FROM cf_config WHERE parameter = 'PLANTHEALTH_QUADRANTS' AND id_tenant = 1) AS total_cuadrantes,
COUNT(ph_planthealth_detail.quadrant) AS cuadrantes_infectados
FROM ph_planthealth
INNER JOIN ph_planthealth_detail ON ph_planthealth_detail.id_planthealth = ph_planthealth.id
INNER JOIN pr_plagues ON pr_plagues.id = ph_planthealth_detail.id_plague
WHERE YEARWEEK(ph_planthealth.date) BETWEEN YEARWEEK('2017-06-01') AND YEARWEEK('2017-06-10')
AND ph_planthealth.status = 200
AND ph_planthealth.id_tenant = 1
AND ph_planthealth.id_land = 1
GROUP BY ph_planthealth_detail.id_plague, YEARWEEK(ph_planthealth.date)
) AS s
ORDER BY percentage DESC
que me arroja el siguiente resultado:
----------------------------------------
plague | fecha | percentage
----------------------------------------
PLAGA1 | 201723 | 9.911
---------------------------------------
PLAGA1 | 201722 | 6.728
---------------------------------------
PLAGA2 | 201722 | 4.727
---------------------------------------
PLAGA3 | 201723 | 4.358
---------------------------------------
PLAGA4 | 201723 | 4.023
---------------------------------------
PLAGA4 | 201722 | 2.903
---------------------------------------
PLAGA3 | 201722 | 2.760
---------------------------------------
PLAGA2 | 201723 | 10.266
---------------------------------------
Lo que quiero es ordenar plague desde el mayor percentage hasta el más menor, según la última semana es decir 201723, pero me quede agrupada con la semana anterior 201722, es decir,
plague | fecha | percentage
----------------------------------------
PLAGA2 | 201723 | 10.266
---------------------------------------
PLAGA2 | 201722 | 4.727
---------------------------------------
PLAGA1 | 201723 | 9.911
---------------------------------------
PLAGA1 | 201722 | 6.728
---------------------------------------
PLAGA3 | 201723 | 4.358
---------------------------------------
PLAGA3 | 201722 | 2.760
---------------------------------------
PLAGA4 | 201723 | 4.023
---------------------------------------
PLAGA4 | 201722 | 2.903
---------------------------------------
He investigado pero no he podido agruparlo y organizarlos de esa manera, espero me puedan ayudar!
ORDER BY fecha, percentage DESC
?