mysql - 根据 GROUP BY 对查询进行排序

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

我已经研究过,但我无法以这种方式对其进行分组和组织,我希望我能提供帮助!

最佳答案

您需要将 CAST 转换为 NUMERIC/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

关于mysql - 根据 GROUP BY 对查询进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46937244/

相关文章:

sql - 参数异常 : Keyword not supported: 'server'

mysql - 在 INSERT 数据 A INTO B 之前比较表 A 和表 B

php - Mysqlnd 无法连接到 MySQL 5.5 服务器

php - 在 PHP 中使用 while 循环从函数获取数组结果

php - UTF-8 字符未作为 UTF-8 从 PHP 插入到 MySQL 表中

java - jComboBox 的登录窗口不起作用

PHP( Grocery 增删改查)| Mysql在两个数字之间生成一个唯一的随机数

mysql - 使用sql结果指定要连接的表

sql - 将 WHERE 条件转换为 LEFT JOIN

MySQL Select from 列使用 ^ 作为分隔符