variacion porcentual entre dos columnas

0

Hello, I made a query to separate the number of roles per year, leaving 3 columns: Commune, initial (number of roles year 2011), final (number of roles year 2021) and now I want to make the variation (final-initial)/final, But it's not working.

Use this code to separate the 3 columns select c.comuna, count(t.numero_linea) inicial, (select count(t.numero_linea) from const t where t.periodo=('1-2021') and t.cod_com=c.cod_com) final

from codigo_comuna_region as c, const t where (t.periodo=('1-2011') and t.cod_com=c.cod_com)

group by c.comuna, c.cod_com

And use this code to make the variation but it doesn't work for me: select ((final-inicial)/final) variacion,c.comuna, (select c.comuna, count(t.numero_linea) inicial, (select count(t.numero_linea) from const t where t.periodo=('1-2021') and t.cod_com=c.cod_com) final

from codigo_comuna_region as c, const t
where (t.periodo=('1-2011') and t.cod_com=c.cod_com))

from codigo_comuna_region as c, const t

group by c.comuna, c.cod_com order by variacion desc limit 10

  • The title language seems to be Spanish, so modify it in English so your might get an answer sooner.

1 個回答
0

I hope this is helpful for you.

SELECT 
  (final - inicial) / final AS variacion, 
  c.comuna, 
  inicial, 
  final
FROM 
  codigo_comuna_region AS c 
  INNER JOIN (
    SELECT 
      cod_com, 
      count(numero_linea) AS inicial, 
      (
        SELECT 
          count(numero_linea) 
        FROM 
          const 
        WHERE 
          periodo = '1-2021' AND 
          cod_com = c1.cod_com
      ) AS final
    FROM 
      const AS c1 
    WHERE 
      periodo = '1-2011' 
    GROUP BY 
      cod_com
  ) AS d ON c.cod_com = d.cod_com 
ORDER BY 
  variacion DESC 
LIMIT 
  10;

profile pictureAWS
nemf
已回答 1 年前
  • It doesn't work for me, I get a column filled with 0 in variation, and it doesn't order it in DESC by variation because when I run the code a second time it gives me a different result than the first.

  • my "initial and final column" is not a column from the database, it is a column that I did through a select, because the data of the initial and final column are together in a "period" column

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南