1

我想用 MySQL 数据绘制一个饼图。我需要检索前 n 行并将其余行分组。
问题是第一个查询已经分组。

SELECT name AS especie, SUM(superficie) AS superficie
FROM ciclos
JOIN cultivos ON id_cultivo = idcultivo
JOIN tbl_especies ON id_especie = idespecie
WHERE fecha_cierre IS NULL
GROUP BY id_especie
ORDER BY superficie DESC

这就是我得到的:

+------------+------------+
|  Especie   | Superficie |
+------------+------------+
| Avena      | 50.0000    |
| Centeno    | 32.4000    |
| Trigo      | 18.0000    |
| Almendros  | 5.1100     |
| Olivos     | 4.7000     |
| Vid        | 1.8300     |
| Nogal      | 0.3500     |
| Cerezo     | 0.2500     |
+------------+------------+

这就是我需要的:

+------------+------------+
|  Especie   | Superficie |
+------------+------------+
| Avena      | 50.0000    |
| Centeno    | 32.4000    |
| Trigo      | 18.0000    |
| Almendros  | 5.1100     |
| Rest       | 7.1300     |
+------------+------------+

在这种情况下,我需要检索前 4 行并将其余行分组。

有没有办法通过一个查询来解决这个问题?

4

2 回答 2

0

你可以用一个查询来做到这一点,但它需要一个子查询(最后,不知何故,你必须对已经分组的数据进行分组)。这是一种特定于 MySQL 的方式。它使用变量在行上添加一个序列号,然后将其用于分组:

select (case when rn <= 4 then especie else 'otros' end) as grouping,
       sum(superficie) as superficie
from (SELECT name AS especie, SUM(superficie) AS superficie, @rn := @rn + 1 as rn
      FROM ciclos
      JOIN cultivos ON id_cultivo = idcultivo
      JOIN tbl_especies ON id_especie = idespecie
      cross join (select @rn := 0) const
      WHERE fecha_cierre IS NULL
      GROUP BY id_especie
      ORDER BY superficie DESC
     ) t
group by (case when rn <= 4 then especie else 'otros' end)
于 2013-05-07T01:54:11.787 回答
0

解决了:

我采用了@Gordon Linoff 的概念并将其与this混合在一起。
@Gordon Linoff 解决方案的问题是在订单期间添加了行号。

SELECT @rn := @rn + 1 AS rn, SUM(superficie) AS superficie, (CASE WHEN @rn <= 4 THEN name ELSE "Other" END) AS especie
FROM (
    SELECT name, SUM(superficie) AS superficie
    FROM ciclos
    JOIN cultivos ON id_cultivo = idcultivo
    JOIN tbl_especies ON id_especie = idespecie
    WHERE fecha_cierre IS NULL
    GROUP BY id_especie
    ORDER BY superficie DESC
) AS temp
CROSS JOIN (SELECT @rn := 0) AS const
GROUP BY (CASE WHEN @rn <= 4 THEN name ELSE "Other" END)
ORDER BY superficie DESC

希望这可以帮助某人。谢谢您的帮助。

于 2013-05-07T08:03:39.323 回答