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 Antwort
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
beantwortet vor einem Jahr
  • 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

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen