在Mysql中计算增长率

时间:2018-07-18 03:23:48

标签: mysql-workbench

您好,我有此查询来获取上一行中的费率:

SELECT
  t.*,
  100 * (Volumen - Volumen_a) / (Volumen_a) Subida
FROM (
  SELECT
   *,
   (
     SELECT Volumen
     FROM ejemplo t_ant
     WHERE
       t_ant.Sujeto = t_act.Sujeto AND t_ant.Id < t_act.Id
     ORDER BY Id DESC
     LIMIT 1
   ) Volumen_a
  FROM ejemplo t_act
  WHERE Sujeto = 'HGT'
  ORDER BY Fecha DESC
  LIMIT 1
) t
WHERE Volumen_a IS NOT NULL;

它只是显示最后一行...然后我需要它显示所有行。

最诚挚的问候

1 个答案:

答案 0 :(得分:1)

SELECT
  t.*,
  100 * (Volumen - Volumen_a) / (Volumen_a) Subida
FROM (
  SELECT
   *,
   (
     SELECT Volumen
     FROM ejemplo t_ant
     WHERE
       t_ant.Sujeto = t_act.Sujeto AND t_ant.Id < t_act.Id
     ORDER BY Id DESC
     LIMIT 1
   ) Volumen_a
  FROM ejemplo t_act
  ORDER BY Fecha DESC
  ) t
WHERE Volumen_a IS NOT NULL;

解决了删除最后一个限制1和名称clausule的问题。 谢谢