根据GROUP BY对查询进行排序

时间:2017-10-25 16:09:14

标签: mysql sql

我有以下查询

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

给了我以下结果:

----------------------------------------
   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    
---------------------------------------

我想要的是从最高百分比订购瘟疫到最低,根据上周的价值是201723,但是与201722周分组

所以我想要以下结果:

   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     
---------------------------------------

我已经研究过,但我无法将其分组并以此方式组织,我希望我可以提供帮助!

1 个答案:

答案 0 :(得分:1)

您需要CASTNUMERIC/DECIMAL才能获得数字排序而不是文字排序:

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 CAST(percentage AS NUMERIC(38,4)) DESC;

甚至更好,不要使用FORMAT

SELECT plague, fecha, CAST(((cuadrantes_infectados * 100) / total_cuadrantes) AS NUMERIC(38,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