Select does not return values correctly per month

Asked

Viewed 34 times

0

I have a table with a field where the value is (current balance) and in this table has a column (period), where has the date I want to check, however, when I run the Query below only returns the month 1 the other periods returns Null, which can be ?

Query I’m using below.

SELECT classificacao, descricao, saldo_anterior,
        (SELECT saldo_atual WHERE MONTH(periodo) = 1)  AS Janeiro,
        (SELECT saldo_atual WHERE MONTH(periodo) = 2) AS Feveiro,
        (SELECT saldo_atual WHERE MONTH(periodo) = 3) AS Marco,
        (SELECT saldo_atual WHERE MONTH(periodo) = 4) AS Abril,
        (SELECT saldo_atual WHERE MONTH(periodo) = 5) AS Maio,
        (SELECT saldo_atual WHERE MONTH(periodo) = 6) AS Junho,
        (SELECT saldo_atual WHERE MONTH(periodo) = 7) AS Julho,
        (SELECT saldo_atual WHERE MONTH(periodo) = 8) AS Agosto,
        (SELECT saldo_atual WHERE MONTH(periodo) = 9) AS Setembro,
        (SELECT saldo_atual WHERE MONTH(periodo) = 10) AS Outubro,
        (SELECT saldo_atual WHERE MONTH(periodo) = 11) AS Novembro,
        (SELECT saldo_atual WHERE MONTH(periodo) = 12) AS Dezembro
    FROM tbl_balancete
    GROUP BY classificacao, descricao
    ;

Upshot

inserir a descrição da imagem aqui

Seat structure and error simulation

https://www.db-fiddle.com/f/wKGRhVU5bZntL1KCiA34fy/0

2 answers

1


You can use CASE When Then ELSE END

SELECT classificacao, descricao, saldo_anterior,
         CASE WHEN MONTH(periodo)=1 THEN saldo_atual  ELSE  0  END AS Janeiro,
         CASE WHEN MONTH(periodo)=2 THEN saldo_atual  ELSE  0  END AS Fevereiro,
         CASE WHEN MONTH(periodo)=3 THEN saldo_atual  ELSE  0  END AS Marco,
         CASE WHEN MONTH(periodo)=4 THEN saldo_atual  ELSE  0  END AS AS Abril,
         CASE WHEN MONTH(periodo)=4 THEN saldo_atual  ELSE  0  END AS Maio,
         CASE WHEN MONTH(periodo)=6 THEN saldo_atual  ELSE  0  END AS Junho,
         CASE WHEN MONTH(periodo)=7 THEN saldo_atual  ELSE  0  END AS Julho,
         CASE WHEN MONTH(periodo)=8 THEN saldo_atual  ELSE  0  END AS Agosto,
         CASE WHEN MONTH(periodo)=9 THEN saldo_atual  ELSE  0  END AS Setembro,
         CASE WHEN MONTH(periodo)=10 THEN saldo_atual  ELSE  0  END AS Outubro,
         CASE WHEN MONTH(periodo)=11 THEN saldo_atual  ELSE  0  END AS Novembro,
         CASE WHEN MONTH(periodo)=12 THEN saldo_atual  ELSE  0  END AS Dezembro
    FROM tbl_balancete
    GROUP BY classificacao, descricao
    ;

UPDATE 1:

Based on the structure later supplied I extrapolated a possible solution with the summations per year.

first subquery obtain a single ranking list per year, description and current balance

2nd subquery does the pivot by rating year and months

The last query does the left Join of the previous two aggregating by year, classification, description and previous balance and sum the values obtained for each month

SELECT plano.ano,
       plano.classificacao,
       plano.descricao,
       plano.saldo_anterior,
       SUM(Janeiro) AS Janeiro,
       SUM(Fevereiro) AS Fevereiro,
       SUM(Marco) AS Marco,
       SUM(Abril) AS Abril,
       SUM(Maio) AS Maio,
       SUM(Junho) AS Junho,
       SUM(Julho) AS Julho,
       SUM(Agosto) AS Agosto,
       SUM(Setembro) AS Setembro,
       SUM(Outubro) AS Outubro,
       SUM(Novembro) AS Novembro,
       SUM(Dezembro) AS Dezembro
FROM
  (SELECT DISTINCT classificacao,
                   descricao,
                   saldo_anterior,
                   year(periodo) AS Ano
   FROM tbl_balancete) AS plano
LEFT JOIN
  (SELECT classificacao,
          year(periodo) AS Ano,
          CASE
              WHEN MONTH(periodo)=1 THEN saldo_atual
              ELSE 0
          END AS Janeiro,
          CASE
              WHEN MONTH(periodo)=2 THEN saldo_atual
              ELSE 0
          END AS Fevereiro,
          CASE
              WHEN MONTH(periodo)=3 THEN saldo_atual
              ELSE 0
          END AS Marco,
          CASE
              WHEN MONTH(periodo)=4 THEN saldo_atual
              ELSE 0
          END AS Abril,
          CASE
              WHEN MONTH(periodo)=4 THEN saldo_atual
              ELSE 0
          END AS Maio,
          CASE
              WHEN MONTH(periodo)=6 THEN saldo_atual
              ELSE 0
          END AS Junho,
          CASE
              WHEN MONTH(periodo)=7 THEN saldo_atual
              ELSE 0
          END AS Julho,
          CASE
              WHEN MONTH(periodo)=8 THEN saldo_atual
              ELSE 0
          END AS Agosto,
          CASE
              WHEN MONTH(periodo)=9 THEN saldo_atual
              ELSE 0
          END AS Setembro,
          CASE
              WHEN MONTH(periodo)=10 THEN saldo_atual
              ELSE 0
          END AS Outubro,
          CASE
              WHEN MONTH(periodo)=11 THEN saldo_atual
              ELSE 0
          END AS Novembro,
          CASE
              WHEN MONTH(periodo)=12 THEN saldo_atual
              ELSE 0
          END AS Dezembro
   FROM tbl_balancete) AS acumulados ON plano.classificacao = acumulados.classificacao
AND plano.ano = acumulados.ano
GROUP BY plano.ano,
         plano.classificacao,
         plano.descricao,
         plano.saldo_anterior
  • Now instead of returning Null returns 0, but continues without returning the following months

  • Publish the table structure

  • Ready put.

  • See the update

0

You will not have a current balance in a past month, the balance is current. In your table, when you search for an accounting account running only this:

SELECT saldo_atual WHERE MONTH(periodo) = 1

What he returns to you?

  • Returns the column January that is in the image of the question in my case this current and different balance for each month so the criterion Month(period)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.