简体   繁体   English

根据GROUP BY对查询排序

[英]Sort query according to GROUP BY

I have the following query 我有以下查询

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

which gives me the following result: 这给了我以下结果:

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

What I want is to order plague from the highest percentage to the lowest, according to the value of the last week is 201723, but is grouped with the week 201722 我想要的是按照上周的值是201723,按从最高百分比到最低的顺序订购鼠疫 ,但将其与201722一周分组

So I want the following result: 所以我想要以下结果:

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

I have researched but I have not been able to group it and organize it that way, I hope I can help! 我已经研究过,但无法将其分组和组织,我希望能有所帮助!

You need to CAST to NUMERIC/DECIMAL to get number sort instead of text sort: 您需要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;

Or even better, do not use FORMAT at all: 甚至更好的是,根本不使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM